Failed Conditions
Pull Request — master (#2)
by Jonathan
03:29
created

MappingDriverChain   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 90.24%

Importance

Changes 0
Metric Value
wmc 19
dl 0
loc 118
rs 10
c 0
b 0
f 0
ccs 37
cts 41
cp 0.9024

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultDriver() 0 3 1
A isTransient() 0 14 4
A setDefaultDriver() 0 3 1
A addDriver() 0 3 1
C getAllClassNames() 0 29 7
A loadMetadataForClass() 0 16 4
A getDrivers() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Common\Persistence\Mapping\Driver;
6
7
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
8
use Doctrine\Common\Persistence\Mapping\MappingException;
9
use function array_keys;
10
use function spl_object_hash;
11
use function strpos;
12
13
/**
14
 * The DriverChain allows you to add multiple other mapping drivers for
15
 * certain namespaces.
16
 */
17
class MappingDriverChain implements MappingDriver
18
{
19
    /**
20
     * The default driver.
21
     *
22
     * @var MappingDriver|null
23
     */
24
    private $defaultDriver;
25
26
    /** @var MappingDriver[] */
27
    private $drivers = [];
28
29
    /**
30
     * Gets the default driver.
31
     */
32 1
    public function getDefaultDriver() : ?MappingDriver
33
    {
34 1
        return $this->defaultDriver;
35
    }
36
37
    /**
38
     * Set the default driver.
39
     */
40 2
    public function setDefaultDriver(MappingDriver $driver) : void
41
    {
42 2
        $this->defaultDriver = $driver;
43 2
    }
44
45
    /**
46
     * Adds a nested driver.
47
     */
48 5
    public function addDriver(MappingDriver $nestedDriver, string $namespace) : void
49
    {
50 5
        $this->drivers[$namespace] = $nestedDriver;
51 5
    }
52
53
    /**
54
     * Gets the array of nested drivers.
55
     *
56
     * @return MappingDriver[] $drivers
57
     */
58
    public function getDrivers() : array
59
    {
60
        return $this->drivers;
61
    }
62
63
    /**
64
     * {@inheritDoc}
65
     */
66 2
    public function loadMetadataForClass(string $className, ClassMetadata $metadata) : void
67
    {
68
        /** @var MappingDriver $driver */
69 2
        foreach ($this->drivers as $namespace => $driver) {
70 1
            if (strpos($className, $namespace) === 0) {
71 1
                $driver->loadMetadataForClass($className, $metadata);
72 1
                return;
73
            }
74
        }
75
76 1
        if ($this->defaultDriver !== null) {
77
            $this->defaultDriver->loadMetadataForClass($className, $metadata);
78
            return;
79
        }
80
81 1
        throw MappingException::classNotFoundInNamespaces($className, array_keys($this->drivers));
82
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87 2
    public function getAllClassNames() : array
88
    {
89 2
        $classNames    = [];
90 2
        $driverClasses = [];
91
92
        /** @var MappingDriver $driver */
93 2
        foreach ($this->drivers as $namespace => $driver) {
94 2
            $oid = spl_object_hash($driver);
95
96 2
            if (! isset($driverClasses[$oid])) {
97 2
                $driverClasses[$oid] = $driver->getAllClassNames();
98
            }
99
100 2
            foreach ($driverClasses[$oid] as $className) {
101 2
                if (strpos($className, $namespace) !== 0) {
102 1
                    continue;
103
                }
104
105 2
                $classNames[$className] = true;
106
            }
107
        }
108
109 2
        if ($this->defaultDriver !== null) {
110 1
            foreach ($this->defaultDriver->getAllClassNames() as $className) {
111 1
                $classNames[$className] = true;
112
            }
113
        }
114
115 2
        return array_keys($classNames);
116
    }
117
118
    /**
119
     * {@inheritDoc}
120
     */
121 3
    public function isTransient(string $className) : bool
122
    {
123
        /** @var MappingDriver $driver */
124 3
        foreach ($this->drivers as $namespace => $driver) {
125 3
            if (strpos($className, $namespace) === 0) {
126 3
                return $driver->isTransient($className);
127
            }
128
        }
129
130 2
        if ($this->defaultDriver !== null) {
131 1
            return $this->defaultDriver->isTransient($className);
132
        }
133
134 1
        return true;
135
    }
136
}
137