MappingDriverChain::getAllClassNames()   B
last analyzed

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