Completed
Pull Request — 1.3.x (#71)
by Grégoire
06:05
created

MappingDriverChain::getAllClassNames()   B

Complexity

Conditions 7
Paths 14

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 29
ccs 15
cts 15
cp 1
rs 8.8333
cc 7
nc 14
nop 0
crap 7
1
<?php
2
3
namespace Doctrine\Persistence\Mapping\Driver;
4
5
use Doctrine\Persistence\Mapping\ClassMetadata;
6
use Doctrine\Persistence\Mapping\MappingException;
7
use function array_keys;
8
use function class_exists;
9
use function spl_object_hash;
10
use function strpos;
11
12
/**
13
 * The DriverChain allows you to add multiple other mapping drivers for
14
 * certain namespaces.
15
 */
16
class MappingDriverChain implements MappingDriver
17
{
18
    /**
19
     * The default driver.
20
     *
21
     * @var MappingDriver|null
22
     */
23
    private $defaultDriver;
24
25
    /** @var MappingDriver[] */
26
    private $drivers = [];
27
28
    /**
29
     * Gets the default driver.
30
     *
31
     * @return MappingDriver|null
32
     */
33 1
    public function getDefaultDriver()
34
    {
35 1
        return $this->defaultDriver;
36
    }
37
38
    /**
39
     * Set the default driver.
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
145
class_exists(\Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain::class);
146