Passed
Pull Request — master (#151)
by
unknown
02:46
created

ChainManagerRegistry   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
eloc 26
c 1
b 0
f 0
dl 0
loc 91
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A getManagerForClass() 0 9 3
A getDefaultConnectionName() 0 3 1
A getDefaultManagerName() 0 3 1
A getConnections() 0 3 1
A getRepository() 0 9 3
A getConnection() 0 3 1
A getConnectionNames() 0 3 1
A getManagerNames() 0 3 1
A resetManager() 0 3 1
A getManagers() 0 8 1
A getManager() 0 3 1
A getAliasNamespace() 0 3 1
1
<?php
2
3
namespace Zenstruck\Foundry;
4
5
use Doctrine\Persistence\ManagerRegistry;
6
use Doctrine\Persistence\ObjectManager;
7
use Doctrine\Persistence\ObjectRepository;
8
9
class ChainManagerRegistry implements ManagerRegistry
10
{
11
    /** @var array<ManagerRegistry> */
12
    private $managerRegistries;
13
14
    /** @param array<ManagerRegistry> $managerRegistries */
15
    public function __construct(array $managerRegistries)
16
    {
17
        if (count($managerRegistries) === 0) {
18
            throw new \InvalidArgumentException('no manager registry provided');
19
        }
20
21
        $this->managerRegistries = $managerRegistries;
22
    }
23
24
    public function getRepository($class, $persistentManagerName = null): ObjectRepository
25
    {
26
        foreach ($this->managerRegistries as $managerRegistry) {
27
            if ($repository = $managerRegistry->getRepository($class)) {
28
                return $repository;
29
            }
30
        }
31
32
        throw new \LogicException("Cannot find repository for class $class");
33
    }
34
35
    public function getManagerForClass($class): ?ObjectManager
36
    {
37
        foreach ($this->managerRegistries as $managerRegistry) {
38
            if ($managerForClass = $managerRegistry->getManagerForClass($class)) {
39
                return $managerForClass;
40
            }
41
        }
42
43
        return null;
44
    }
45
46
    public function getManagers(): array
47
    {
48
        return array_reduce(
49
            $this->managerRegistries,
50
            static function (array $carry, ManagerRegistry $managerRegistry) {
51
                return array_merge($carry, $managerRegistry->getManagers());
52
            },
53
            []
54
        );
55
    }
56
57
    public function getDefaultConnectionName()
58
    {
59
        throw new \BadMethodCallException('Not available in '.self::class);
60
    }
61
62
    public function getConnection($name = null)
63
    {
64
        throw new \BadMethodCallException('Not available in '.self::class);
65
    }
66
67
    public function getConnections()
68
    {
69
        throw new \BadMethodCallException('Not available in '.self::class);
70
    }
71
72
    public function getConnectionNames()
73
    {
74
        throw new \BadMethodCallException('Not available in '.self::class);
75
    }
76
77
    public function getDefaultManagerName()
78
    {
79
        throw new \BadMethodCallException('Not available in '.self::class);
80
    }
81
82
    public function getManager($name = null)
83
    {
84
        throw new \BadMethodCallException('Not available in '.self::class);
85
    }
86
87
    public function resetManager($name = null)
88
    {
89
        throw new \BadMethodCallException('Not available in '.self::class);
90
    }
91
92
    public function getAliasNamespace($alias)
93
    {
94
        throw new \BadMethodCallException('Not available in '.self::class);
95
    }
96
97
    public function getManagerNames()
98
    {
99
        throw new \BadMethodCallException('Not available in '.self::class);
100
    }
101
}
102