Passed
Pull Request — master (#38)
by Kevin
16:32
created

ModelFactory::collection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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