Passed
Pull Request — master (#1)
by Kevin
02:47
created

ModelFactory::createMany()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Zenstruck\Foundry;
4
5
/**
6
 * @author Kevin Bond <[email protected]>
7
 */
8
abstract class ModelFactory extends Factory
9
{
10 80
    private function __construct()
11
    {
12 80
        parent::__construct(static::getClass());
13 80
    }
14
15
    /**
16
     * @param array|callable|string $defaultAttributes If string, assumes state
17
     * @param string                ...$states         Optionally pass default states (these must be methods on your ObjectFactory with no arguments)
18
     */
19 80
    final public static function new($defaultAttributes = [], string ...$states): self
20
    {
21
        // todo - is this too magical?
22 80
        if (\is_string($defaultAttributes)) {
23 2
            $states = \array_merge([$defaultAttributes], $states);
24 2
            $defaultAttributes = [];
25
        }
26
27 80
        $factory = new static();
28
        $factory = $factory
29 80
            ->withAttributes([$factory, 'getDefaults'])
30 80
            ->withAttributes($defaultAttributes)
31 80
            ->initialize()
32
        ;
33
34 80
        if (!$factory instanceof static) {
0 ignored issues
show
introduced by
$factory is always a sub-type of static.
Loading history...
35 4
            throw new \TypeError(\sprintf('"%1$s::initialize()" must return an instance of "%1$s".', static::class));
36
        }
37
38 80
        foreach ($states as $state) {
39 2
            $factory = $factory->{$state}();
40
        }
41
42 80
        return $factory;
43
    }
44
45
    /**
46
     * Try and find existing object for the given $attributes. If not found,
47
     * instantiate and persist.
48
     *
49
     * @return Proxy|object
50
     */
51 2
    final public static function findOrCreate(array $attributes): Proxy
52
    {
53 2
        if ($found = self::repository()->find($attributes)) {
54 2
            return $found;
55
        }
56
57 2
        return self::new()->create($attributes);
58
    }
59
60
    /**
61
     * Get a random persisted object.
62
     *
63
     * @return Proxy|object
64
     */
65 2
    final public static function random(): Proxy
66
    {
67 2
        return self::repository()->random();
68
    }
69
70
    /**
71
     * @param int      $min The minimum number of objects to return (if max is null, will always return this amount)
72
     * @param int|null $max The max number of objects to return
73
     *
74
     * @return Proxy[]|object[]
75
     */
76 4
    final public static function randomSet(int $min, ?int $max = null): array
77
    {
78 4
        return self::repository()->randomSet($min, $max);
79
    }
80
81 16
    final public static function repository(): RepositoryProxy
82
    {
83 16
        return PersistenceManager::repositoryFor(static::getClass());
84
    }
85
86
    /**
87
     * Override to add default instantiator and default afterInstantiate/afterPersist events.
88
     *
89
     * @return static
90
     */
91 80
    protected function initialize()
92
    {
93 80
        return $this;
94
    }
95
96
    /**
97
     * @param array|callable $attributes
98
     */
99 6
    final protected function addState($attributes = []): self
100
    {
101 6
        return $this->withAttributes($attributes);
102
    }
103
104
    abstract protected static function getClass(): string;
105
106
    abstract protected function getDefaults(): array;
107
}
108