Failed Conditions
Pull Request — master (#1)
by Jonathan
03:48
created

MappingDriverChain   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

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