Passed
Push — master ( 6bd319...2d574a )
by Kevin
04:56
created

Configuration::defaultProxyAutoRefresh()   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
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
9
10
/**
11
 * @internal
12
 *
13
 * @author Kevin Bond <[email protected]>
14
 */
15
final class Configuration
16
{
17
    /** @var ManagerRegistry|null */
18
    private $managerRegistry;
19
20
    /** @var StoryManager */
21
    private $stories;
22
23
    /** @var ModelFactoryManager */
24
    private $factories;
25
26
    /** @var Faker\Generator */
27
    private $faker;
28
29
    /** @var callable */
30
    private $instantiator;
31
32
    /** @var bool */
33
    private $defaultProxyAutoRefresh = false;
34
35 968
    public function __construct()
36
    {
37 968
        $this->stories = new StoryManager([]);
38 968
        $this->factories = new ModelFactoryManager([]);
39 968
        $this->faker = Faker\Factory::create();
40 968
        $this->instantiator = new Instantiator();
41 968
    }
42
43 405
    public function stories(): StoryManager
44
    {
45 405
        return $this->stories;
46
    }
47
48 607
    public function factories(): ModelFactoryManager
49
    {
50 607
        return $this->factories;
51
    }
52
53 964
    public function faker(): Faker\Generator
54
    {
55 964
        return $this->faker;
56
    }
57
58 793
    public function instantiator(): callable
59
    {
60 793
        return $this->instantiator;
61
    }
62
63 857
    public function defaultProxyAutoRefresh(): bool
64
    {
65 857
        return $this->defaultProxyAutoRefresh;
66
    }
67
68 708
    public function setManagerRegistry(ManagerRegistry $managerRegistry): self
69
    {
70 708
        $this->managerRegistry = $managerRegistry;
71
72 708
        return $this;
73
    }
74
75 270
    public function setInstantiator(callable $instantiator): self
76
    {
77 270
        $this->instantiator = $instantiator;
78
79 270
        return $this;
80
    }
81
82 260
    public function setStoryManager(StoryManager $manager): self
83
    {
84 260
        $this->stories = $manager;
85
86 260
        return $this;
87
    }
88
89 260
    public function setModelFactoryManager(ModelFactoryManager $manager): self
90
    {
91 260
        $this->factories = $manager;
92
93 260
        return $this;
94
    }
95
96 270
    public function setFaker(Faker\Generator $faker): self
97
    {
98 270
        $this->faker = $faker;
99
100 270
        return $this;
101
    }
102
103
    public function alwaysAutoRefreshProxies(): self
104
    {
105
        $this->defaultProxyAutoRefresh = true;
106
107
        return $this;
108
    }
109
110
    /**
111
     * @param object|string $objectOrClass
112
     *
113
     * @psalm-suppress InvalidReturnType
114
     * @psalm-suppress InvalidReturnStatement
115
     * @template TObject of object
116
     * @psalm-param Proxy<TObject>|TObject|class-string<TObject> $objectOrClass
117
     * @psalm-return RepositoryProxy<TObject>
118
     */
119 360
    public function repositoryFor($objectOrClass): RepositoryProxy
120
    {
121 360
        if ($objectOrClass instanceof Proxy) {
122
            $objectOrClass = $objectOrClass->object();
123
        }
124
125 360
        if (!\is_string($objectOrClass)) {
126 10
            $objectOrClass = \get_class($objectOrClass);
127
        }
128
129 360
        return new RepositoryProxy($this->managerRegistry()->getRepository($objectOrClass));
130
    }
131
132
    /**
133
     * @param object|string $objectOrClass
134
     */
135 645
    public function objectManagerFor($objectOrClass): ObjectManager
136
    {
137 645
        $class = \is_string($objectOrClass) ? $objectOrClass : \get_class($objectOrClass);
138
139 645
        if (!$objectManager = $this->managerRegistry()->getManagerForClass($class)) {
140
            throw new \RuntimeException(\sprintf('No object manager registered for "%s".', $class));
141
        }
142
143 635
        return $objectManager;
144
    }
145
146
    /** @psalm-assert !null $this->managerRegistry */
147 855
    public function hasManagerRegistry(): bool
148
    {
149 855
        return null !== $this->managerRegistry;
150
    }
151
152 695
    private function managerRegistry(): ManagerRegistry
153
    {
154 695
        if (!$this->hasManagerRegistry()) {
155
            /** @psalm-suppress MissingDependency */
156 10
            throw new \RuntimeException('Foundry was booted without doctrine. Ensure your TestCase extends '.KernelTestCase::class);
157
        }
158
159 685
        return $this->managerRegistry;
160
    }
161
}
162