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

Configuration::instantiator()   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 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 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
    /** @var ManagerRegistry */
15
    private $managerRegistry;
16
17
    /** @var StoryManager */
18
    private $stories;
19
20
    /** @var Faker\Generator */
21
    private $faker;
22
23
    /** @var callable */
24
    private $instantiator;
25
26 278
    public function __construct(ManagerRegistry $managerRegistry, StoryManager $storyManager)
27
    {
28 278
        $this->managerRegistry = $managerRegistry;
29 278
        $this->stories = $storyManager;
30 278
        $this->faker = Faker\Factory::create();
31 278
        $this->instantiator = new Instantiator();
32 278
    }
33
34 162
    public function stories(): StoryManager
35
    {
36 162
        return $this->stories;
37
    }
38
39 194
    public function faker(): Faker\Generator
40
    {
41 194
        return $this->faker;
42
    }
43
44 234
    public function instantiator(): callable
45
    {
46 234
        return $this->instantiator;
47
    }
48
49 190
    public function setManagerRegistry(ManagerRegistry $managerRegistry): self
50
    {
51 190
        $this->managerRegistry = $managerRegistry;
52
53 190
        return $this;
54
    }
55
56 86
    public function setInstantiator(callable $instantiator): self
57
    {
58 86
        $this->instantiator = $instantiator;
59
60 86
        return $this;
61
    }
62
63 86
    public function setFaker(Faker\Generator $faker): self
64
    {
65 86
        $this->faker = $faker;
66
67 86
        return $this;
68
    }
69
70
    /**
71
     * @param object|string $objectOrClass
72
     */
73 92
    public function repositoryFor($objectOrClass): RepositoryProxy
74
    {
75 92
        if ($objectOrClass instanceof Proxy) {
76
            $objectOrClass = $objectOrClass->object();
77
        }
78
79 92
        if (!\is_string($objectOrClass)) {
80 4
            $objectOrClass = \get_class($objectOrClass);
81
        }
82
83 92
        return new RepositoryProxy($this->managerRegistry->getRepository($objectOrClass));
84
    }
85
86
    /**
87
     * @param object|string $objectOrClass
88
     */
89 186
    public function objectManagerFor($objectOrClass): ObjectManager
90
    {
91 186
        $class = \is_string($objectOrClass) ? $objectOrClass : \get_class($objectOrClass);
92
93 186
        if (!$objectManager = $this->managerRegistry->getManagerForClass($class)) {
94
            throw new \RuntimeException(\sprintf('No object manager registered for "%s".', $class));
95
        }
96
97 186
        return $objectManager;
98
    }
99
}
100