Failed Conditions
Push — master ( ec2bd1...5afe97 )
by Jonathan
41s
created

MappingDriverChain   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Test Coverage

Coverage 90.24%

Importance

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

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