Passed
Pull Request — master (#38)
by Kevin
18:30
created

FactoryCollection   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
eloc 16
c 1
b 0
f 1
dl 0
loc 49
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A create() 0 7 1
A all() 0 7 1
A factory() 0 3 1
1
<?php
2
3
namespace Zenstruck\Foundry;
4
5
/**
6
 * @author Kevin Bond <[email protected]>
7
 */
8
final class FactoryCollection
9
{
10
    /** @var Factory */
11
    private $factory;
12
13
    /** @var int */
14
    private $count;
15
16
    public function __construct(Factory $factory, int $count)
17
    {
18
        if ($count < 1) {
19
            throw new \InvalidArgumentException('Count must be greater than 0.');
20
        }
21
22
        $this->factory = $factory;
23
        $this->count = $count;
24
    }
25
26
    /**
27
     * @param array|callable $attributes
28
     *
29
     * @return Proxy[]|object[]
30
     */
31
    public function create($attributes = []): array
32
    {
33
        return \array_map(
34
            static function(Factory $factory) use ($attributes) {
35
                return $factory->create($attributes);
36
            },
37
            $this->all()
38
        );
39
    }
40
41
    /**
42
     * @return Factory[]
43
     */
44
    public function all(): array
45
    {
46
        return \array_map(
47
            function() {
48
                return clone $this->factory;
49
            },
50
            \array_fill(0, $this->count, null)
51
        );
52
    }
53
54
    public function factory(): Factory
55
    {
56
        return $this->factory;
57
    }
58
}
59