Passed
Push — master ( 064bed...d815ea )
by Gerrit
04:18
created

MappingDriverChain   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 46
ccs 15
cts 15
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A loadRDMMetadataForClass() 0 24 4
A addInnerMetadataDriver() 0 3 1
A __construct() 0 6 2
1
<?php
2
/**
3
 * Copyright (C) 2018 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 * @license GPL-3.0
8
 * @author Gerrit Addiks <[email protected]>
9
 */
10
11
namespace Addiks\RDMBundle\Mapping\Drivers;
12
13
use Addiks\RDMBundle\Mapping\Drivers\MappingDriverInterface;
14
use Addiks\RDMBundle\Mapping\EntityMappingInterface;
15
use Addiks\RDMBundle\Mapping\MappingInterface;
16
use Addiks\RDMBundle\Mapping\EntityMapping;
17
18
final class MappingDriverChain implements MappingDriverInterface
19
{
20
21
    /**
22
     * @var array<MappingDriverInterface>
23
     */
24
    private $innerDrivers = array();
25
26 8
    public function __construct(array $innerDrivers = array())
27
    {
28 8
        foreach ($innerDrivers as $innerDriver) {
29
            /** @var MappingDriverInterface $innerDriver */
30
31 8
            $this->addInnerMetadataDriver($innerDriver);
32
        }
33
    }
34
35 8
    public function loadRDMMetadataForClass(string $className): ?EntityMappingInterface
36
    {
37
        /** @var ?EntityMappingInterface $mapping */
38 8
        $mapping = null;
39
40
        /** @var array<MappingInterface> $fieldMappings */
41 8
        $fieldMappings = array();
42
43 8
        foreach ($this->innerDrivers as $innerDriver) {
44
            /** @var MappingDriverInterface $innerDriver */
45
46
            /** @var ?EntityMappingInterface $driverMapping */
47 8
            $driverMapping = $innerDriver->loadRDMMetadataForClass($className);
48
49 8
            if ($driverMapping instanceof EntityMappingInterface) {
50 7
                $fieldMappings = array_merge($fieldMappings, $driverMapping->getFieldMappings());
51
            }
52
        }
53
54 8
        if (!empty($fieldMappings)) {
55 7
            $mapping = new EntityMapping($className, $fieldMappings);
56
        }
57
58 8
        return $mapping;
59
    }
60
61 8
    private function addInnerMetadataDriver(MappingDriverInterface $innerDriver): void
62
    {
63 8
        $this->innerDrivers[] = $innerDriver;
64
    }
65
66
}
67