DriverChain   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 121
ccs 0
cts 64
cp 0
rs 10
c 0
b 0
f 0
wmc 19

7 Methods

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