Passed
Pull Request — master (#53)
by Kevin
16:24
created

Configuration::setFaker()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Zenstruck\Foundry;
4
5
use Doctrine\Persistence\ManagerRegistry;
6
use Doctrine\Persistence\ObjectManager;
7
use Faker;
8
9
/**
10
 * @internal
11
 *
12
 * @author Kevin Bond <[email protected]>
13
 */
14
final class Configuration
15
{
16
    /** @var ManagerRegistry */
17
    private $managerRegistry;
18
19
    /** @var StoryManager */
20
    private $stories;
21
22
    /** @var ModelFactoryRegistry */
23
    private $factories;
24
25
    /** @var Faker\Generator */
26
    private $faker;
27
28
    /** @var callable */
29
    private $instantiator;
30
31 686
    /** @var bool */
32
    private $defaultProxyAutoRefresh = false;
33 686
34 686
    public function __construct(ManagerRegistry $managerRegistry, StoryManager $storyManager, ModelFactoryRegistry $factories)
35 686
    {
36 686
        $this->managerRegistry = $managerRegistry;
37 686
        $this->stories = $storyManager;
38
        $this->factories = $factories;
39 235
        $this->faker = Faker\Factory::create();
40
        $this->instantiator = new Instantiator();
41 235
    }
42
43
    public function stories(): StoryManager
44 491
    {
45
        return $this->stories;
46 491
    }
47
48
    public function factories(): ModelFactoryRegistry
49 551
    {
50
        return $this->factories;
51 551
    }
52
53
    public function faker(): Faker\Generator
54 603
    {
55
        return $this->faker;
56 603
    }
57
58
    public function instantiator(): callable
59 483
    {
60
        return $this->instantiator;
61 483
    }
62
63 483
    public function defaultProxyAutoRefresh(): bool
64
    {
65
        return $this->defaultProxyAutoRefresh;
66 173
    }
67
68 173
    public function setManagerRegistry(ManagerRegistry $managerRegistry): self
69
    {
70 173
        $this->managerRegistry = $managerRegistry;
71
72
        return $this;
73 173
    }
74
75 173
    public function setInstantiator(callable $instantiator): self
76
    {
77 173
        $this->instantiator = $instantiator;
78
79
        return $this;
80
    }
81
82
    public function setFaker(Faker\Generator $faker): self
83
    {
84
        $this->faker = $faker;
85
86
        return $this;
87
    }
88
89
    public function alwaysAutoRefreshProxies(): self
90 248
    {
91
        $this->defaultProxyAutoRefresh = true;
92 248
93
        return $this;
94
    }
95
96 248
    /**
97 8
     * @param object|string $objectOrClass
98
     */
99
    public function repositoryFor($objectOrClass): RepositoryProxy
100 248
    {
101
        if ($objectOrClass instanceof Proxy) {
102
            $objectOrClass = $objectOrClass->object();
103
        }
104
105
        if (!\is_string($objectOrClass)) {
106 431
            $objectOrClass = \get_class($objectOrClass);
107
        }
108 431
109
        return new RepositoryProxy($this->managerRegistry->getRepository($objectOrClass));
110 431
    }
111
112
    /**
113
     * @param object|string $objectOrClass
114 431
     */
115
    public function objectManagerFor($objectOrClass): ObjectManager
116
    {
117
        $class = \is_string($objectOrClass) ? $objectOrClass : \get_class($objectOrClass);
118
119
        if (!$objectManager = $this->managerRegistry->getManagerForClass($class)) {
120
            throw new \RuntimeException(\sprintf('No object manager registered for "%s".', $class));
121
        }
122
123
        return $objectManager;
124
    }
125
}
126