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

Configuration   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
eloc 27
c 0
b 0
f 0
dl 0
loc 81
rs 10
ccs 30
cts 32
cp 0.9375
wmc 13

9 Methods

Rating   Name   Duplication   Size   Complexity  
A stories() 0 3 1
A objectManagerFor() 0 9 3
A setFaker() 0 5 1
A setManagerRegistry() 0 5 1
A setInstantiator() 0 5 1
A repositoryFor() 0 11 3
A instantiator() 0 3 1
A __construct() 0 6 1
A faker() 0 3 1
1
<?php
2
3
namespace Zenstruck\Foundry;
4
5
use Doctrine\Persistence\ManagerRegistry;
6
use Doctrine\Persistence\ObjectManager;
7
use Faker;
8
9
/**
10
 * @author Kevin Bond <[email protected]>
11
 */
12
final class Configuration
13
{
14
    private ManagerRegistry $managerRegistry;
15
    private StoryManager $stories;
16
    private Faker\Generator $faker;
17
18
    /** @var callable */
19
    private $instantiator;
20
21 266
    public function __construct(ManagerRegistry $managerRegistry, StoryManager $storyManager)
22
    {
23 266
        $this->managerRegistry = $managerRegistry;
24 266
        $this->stories = $storyManager;
25 266
        $this->faker = Faker\Factory::create();
26 266
        $this->instantiator = new Instantiator();
27 266
    }
28
29 154
    public function stories(): StoryManager
30
    {
31 154
        return $this->stories;
32
    }
33
34 186
    public function faker(): Faker\Generator
35
    {
36 186
        return $this->faker;
37
    }
38
39 226
    public function instantiator(): callable
40
    {
41 226
        return $this->instantiator;
42
    }
43
44 182
    public function setManagerRegistry(ManagerRegistry $managerRegistry): self
45
    {
46 182
        $this->managerRegistry = $managerRegistry;
47
48 182
        return $this;
49
    }
50
51 82
    public function setInstantiator(callable $instantiator): self
52
    {
53 82
        $this->instantiator = $instantiator;
54
55 82
        return $this;
56
    }
57
58 82
    public function setFaker(Faker\Generator $faker): self
59
    {
60 82
        $this->faker = $faker;
61
62 82
        return $this;
63
    }
64
65
    /**
66
     * @param object|string $objectOrClass
67
     */
68 84
    public function repositoryFor($objectOrClass): RepositoryProxy
69
    {
70 84
        if ($objectOrClass instanceof Proxy) {
71
            $objectOrClass = $objectOrClass->object();
72
        }
73
74 84
        if (!\is_string($objectOrClass)) {
75 4
            $objectOrClass = \get_class($objectOrClass);
76
        }
77
78 84
        return new RepositoryProxy($this->managerRegistry->getRepository($objectOrClass));
79
    }
80
81
    /**
82
     * @param object|string $objectOrClass
83
     */
84 178
    public function objectManagerFor($objectOrClass): ObjectManager
85
    {
86 178
        $class = \is_string($objectOrClass) ? $objectOrClass : \get_class($objectOrClass);
87
88 178
        if (!$objectManager = $this->managerRegistry->getManagerForClass($class)) {
89
            throw new \RuntimeException(\sprintf('No object manager registered for "%s".', $class));
90
        }
91
92 178
        return $objectManager;
93
    }
94
}
95