Passed
Pull Request — master (#19)
by Kevin
03:46
created

Configuration::alwaysAutoRefreshProxies()   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
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
ccs 0
cts 3
cp 0
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 Faker\Generator */
23
    private $faker;
24
25
    /** @var callable */
26
    private $instantiator;
27
28
    /** @var bool */
29
    private $defaultProxyAutoRefresh = false;
30
31 324
    public function __construct(ManagerRegistry $managerRegistry, StoryManager $storyManager)
32
    {
33 324
        $this->managerRegistry = $managerRegistry;
34 324
        $this->stories = $storyManager;
35 324
        $this->faker = Faker\Factory::create();
36 324
        $this->instantiator = new Instantiator();
37 324
    }
38
39 182
    public function stories(): StoryManager
40
    {
41 182
        return $this->stories;
42
    }
43
44 214
    public function faker(): Faker\Generator
45
    {
46 214
        return $this->faker;
47
    }
48
49 254
    public function instantiator(): callable
50
    {
51 254
        return $this->instantiator;
52
    }
53
54 278
    public function defaultProxyAutoRefresh(): bool
55
    {
56 278
        return $this->defaultProxyAutoRefresh;
57
    }
58
59 210
    public function setManagerRegistry(ManagerRegistry $managerRegistry): self
60
    {
61 210
        $this->managerRegistry = $managerRegistry;
62
63 210
        return $this;
64
    }
65
66 122
    public function setInstantiator(callable $instantiator): self
67
    {
68 122
        $this->instantiator = $instantiator;
69
70 122
        return $this;
71
    }
72
73 122
    public function setFaker(Faker\Generator $faker): self
74
    {
75 122
        $this->faker = $faker;
76
77 122
        return $this;
78
    }
79
80
    public function alwaysAutoRefreshProxies(): self
81
    {
82
        $this->defaultProxyAutoRefresh = true;
83
84
        return $this;
85
    }
86
87
    /**
88
     * @param object|string $objectOrClass
89
     */
90 92
    public function repositoryFor($objectOrClass): RepositoryProxy
91
    {
92 92
        if ($objectOrClass instanceof Proxy) {
93
            $objectOrClass = $objectOrClass->object();
94
        }
95
96 92
        if (!\is_string($objectOrClass)) {
97 4
            $objectOrClass = \get_class($objectOrClass);
98
        }
99
100 92
        return new RepositoryProxy($this->managerRegistry->getRepository($objectOrClass));
101
    }
102
103
    /**
104
     * @param object|string $objectOrClass
105
     */
106 206
    public function objectManagerFor($objectOrClass): ObjectManager
107
    {
108 206
        $class = \is_string($objectOrClass) ? $objectOrClass : \get_class($objectOrClass);
109
110 206
        if (!$objectManager = $this->managerRegistry->getManagerForClass($class)) {
111
            throw new \RuntimeException(\sprintf('No object manager registered for "%s".', $class));
112
        }
113
114 206
        return $objectManager;
115
    }
116
}
117