Passed
Push — master ( ddb2f3...fd433b )
by Kevin
03:42
created

Configuration::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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