ModelFactoryManager   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 10
c 1
b 0
f 0
dl 0
loc 24
ccs 10
cts 10
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 12 4
1
<?php
2
3
namespace Zenstruck\Foundry;
4
5
/**
6
 * @internal
7
 *
8
 * @author Kevin Bond <[email protected]>
9
 */
10
final class ModelFactoryManager
11
{
12
    private $factories;
13
14
    /**
15
     * @param ModelFactory[] $factories
16
     */
17 968
    public function __construct(iterable $factories)
18
    {
19 968
        $this->factories = $factories;
20 968
    }
21
22 607
    public function create(string $class): ModelFactory
23
    {
24 607
        foreach ($this->factories as $factory) {
25 240
            if ($class === \get_class($factory)) {
26 96
                return $factory;
27
            }
28
        }
29
30
        try {
31 585
            return new $class();
32 6
        } catch (\ArgumentCountError $e) {
33 6
            throw new \RuntimeException('Model Factories with dependencies (Model Factory services) cannot be used without the foundry bundle.', 0, $e);
34
        }
35
    }
36
}
37