Cancelled
Pull Request — master (#1)
by Kevin
09:12 queued 01:58
created

Configuration::objectManagerFor()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

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