Failed Conditions
Pull Request — master (#2)
by Jonathan
02:45
created

MappingDriverChain   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 90.24%

Importance

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