|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace WayOfDev\Cycle\Bridge\Laravel\Factories; |
| 6 | + |
| 7 | +use Illuminate\Database\Eloquent\Factories\Factory as EloquentFactory; |
| 8 | +use Illuminate\Database\Eloquent\Model; |
| 9 | +use Illuminate\Support\Collection; |
| 10 | +use Illuminate\Support\Enumerable; |
| 11 | + |
| 12 | +use function array_map; |
| 13 | +use function method_exists; |
| 14 | +use function range; |
| 15 | + |
| 16 | +abstract class Factory extends EloquentFactory |
| 17 | +{ |
| 18 | + public static function factoryForEntity(string $modelName): EloquentFactory |
| 19 | + { |
| 20 | + /** @var EloquentFactory $factory */ |
| 21 | + $factory = static::resolveFactoryName($modelName); |
| 22 | + |
| 23 | + return $factory::new(); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Get the factory name for the given model name. |
| 28 | + */ |
| 29 | + public static function resolveFactoryName(string $modelName): string |
| 30 | + { |
| 31 | + if (method_exists($modelName, 'resolveFactoryName')) { |
| 32 | + return $modelName::resolveFactoryName(); |
| 33 | + } |
| 34 | + |
| 35 | + return parent::resolveFactoryName($modelName); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Create a collection of models and persist them to the database. |
| 40 | + * |
| 41 | + * @param mixed $attributes |
| 42 | + */ |
| 43 | + public function create($attributes = [], ?Model $parent = null) |
| 44 | + { |
| 45 | + if ([] !== $attributes) { |
| 46 | + return $this->state($attributes)->create([], $parent); |
| 47 | + } |
| 48 | + |
| 49 | + $results = $this->make($attributes, $parent); |
| 50 | + |
| 51 | + if ($results instanceof Model) { |
| 52 | + $this->store(collect([$results])); |
| 53 | + |
| 54 | + $this->callAfterCreating(collect([$results]), $parent); |
| 55 | + } else { |
| 56 | + $this->store($results); |
| 57 | + |
| 58 | + $this->callAfterCreating($results, $parent); |
| 59 | + } |
| 60 | + |
| 61 | + return $results; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Create a collection of models and persist them to the database. |
| 66 | + */ |
| 67 | + public function createMany(iterable $records): Collection |
| 68 | + { |
| 69 | + return new Collection( |
| 70 | + collect($records)->map(function ($record) { |
| 71 | + return $this->state($record)->create(); |
| 72 | + }) |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Create a collection of models. |
| 78 | + * |
| 79 | + * @param mixed $attributes |
| 80 | + */ |
| 81 | + public function make($attributes = [], ?Model $parent = null) |
| 82 | + { |
| 83 | + if ([] !== $attributes) { |
| 84 | + return $this->state($attributes)->make([], $parent); |
| 85 | + } |
| 86 | + |
| 87 | + if (null === $this->count) { |
| 88 | + return tap($this->makeInstance($parent), function ($instance): void { |
| 89 | + $this->callAfterMaking(collect([$instance])); |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + if (1 > $this->count) { |
| 94 | + return $this->newModel()->newCollection(); |
| 95 | + } |
| 96 | + |
| 97 | + $instances = $this->newModel()->newCollection(array_map(function () use ($parent) { |
| 98 | + return $this->makeInstance($parent); |
| 99 | + }, range(1, $this->count))); |
| 100 | + |
| 101 | + $this->callAfterMaking($instances); |
| 102 | + |
| 103 | + return $instances; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Set the connection name on the results and store them. |
| 108 | + */ |
| 109 | + protected function store(Collection $results): void |
| 110 | + { |
| 111 | + $results->each(function ($model): void { |
| 112 | + if (! isset($this->connection)) { |
| 113 | + $model->setConnection($model->newQueryWithoutScopes()->getConnection()->getName()); |
| 114 | + } |
| 115 | + |
| 116 | + $model->save(); |
| 117 | + |
| 118 | + foreach ($model->getRelations() as $name => $items) { |
| 119 | + if ($items instanceof Enumerable && $items->isEmpty()) { |
| 120 | + $model->unsetRelation($name); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + $this->createChildren($model); |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Make an instance of the model with the given attributes. |
| 130 | + */ |
| 131 | + protected function makeInstance(?Model $parent) |
| 132 | + { |
| 133 | + return Model::unguarded(function () use ($parent) { |
| 134 | + return tap($this->newModel($this->getExpandedAttributes($parent)), function ($instance): void { |
| 135 | + if (isset($this->connection)) { |
| 136 | + $instance->setConnection($this->connection); |
| 137 | + } |
| 138 | + }); |
| 139 | + }); |
| 140 | + } |
| 141 | +} |
0 commit comments