Passed
Pull Request — master (#153)
by Kevin
02:52
created

ChainManagerRegistry::getConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 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
/**
10
 * @internal
11
 */
12
final class ChainManagerRegistry implements ManagerRegistry
13
{
14
    /** @var list<ManagerRegistry> */
0 ignored issues
show
Bug introduced by
The type Zenstruck\Foundry\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
    private $managerRegistries;
16
17
    /** @param list<ManagerRegistry> $managerRegistries */
18
    public function __construct(array $managerRegistries)
19
    {
20
        if (0 === \count($managerRegistries)) {
21
            throw new \InvalidArgumentException('no manager registry provided');
22
        }
23
24
        $this->managerRegistries = $managerRegistries;
0 ignored issues
show
Documentation Bug introduced by
It seems like $managerRegistries of type array is incompatible with the declared type Zenstruck\Foundry\list of property $managerRegistries.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
25
    }
26
27
    public function getRepository($persistentObject, $persistentManagerName = null): ObjectRepository
28
    {
29
        foreach ($this->managerRegistries as $managerRegistry) {
30
            if ($repository = $managerRegistry->getRepository($persistentObject, $persistentManagerName)) {
31
                return $repository;
32
            }
33
        }
34
35
        throw new \LogicException("Cannot find repository for class {$persistentObject}");
36
    }
37
38
    public function getManagerForClass($class): ?ObjectManager
39
    {
40
        foreach ($this->managerRegistries as $managerRegistry) {
41
            if ($managerForClass = $managerRegistry->getManagerForClass($class)) {
42
                return $managerForClass;
43
            }
44
        }
45
46
        return null;
47
    }
48
49
    public function getManagers(): array
50
    {
51
        return \array_reduce(
52
            $this->managerRegistries,
0 ignored issues
show
Bug introduced by
$this->managerRegistries of type Zenstruck\Foundry\list is incompatible with the type array expected by parameter $array of array_reduce(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
            /** @scrutinizer ignore-type */ $this->managerRegistries,
Loading history...
53
            static function(array $carry, ManagerRegistry $managerRegistry) {
54
                return \array_merge($carry, $managerRegistry->getManagers());
55
            },
56
            []
57
        );
58
    }
59
60
    public function getDefaultConnectionName()
61
    {
62
        throw new \BadMethodCallException('Not available in '.self::class);
63
    }
64
65
    public function getConnection($name = null)
66
    {
67
        throw new \BadMethodCallException('Not available in '.self::class);
68
    }
69
70
    public function getConnections()
71
    {
72
        throw new \BadMethodCallException('Not available in '.self::class);
73
    }
74
75
    public function getConnectionNames()
76
    {
77
        throw new \BadMethodCallException('Not available in '.self::class);
78
    }
79
80
    public function getDefaultManagerName()
81
    {
82
        throw new \BadMethodCallException('Not available in '.self::class);
83
    }
84
85
    public function getManager($name = null)
86
    {
87
        throw new \BadMethodCallException('Not available in '.self::class);
88
    }
89
90
    public function resetManager($name = null)
91
    {
92
        throw new \BadMethodCallException('Not available in '.self::class);
93
    }
94
95
    public function getAliasNamespace($alias)
96
    {
97
        throw new \BadMethodCallException('Not available in '.self::class);
98
    }
99
100
    public function getManagerNames()
101
    {
102
        throw new \BadMethodCallException('Not available in '.self::class);
103
    }
104
}
105