Completed
Push — master ( 7e6070...203e38 )
by Gerrit
02:38
created

MappingYamlDriver   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 5
dl 0
loc 137
ccs 54
cts 60
cp 0.9
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B loadRDMMetadataForClass() 0 28 6
A readMappings() 0 9 3
A readChoices() 0 8 2
A readServices() 0 8 2
A readOneMapping() 0 11 3
B readChoice() 0 26 4
B readService() 0 25 3
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 ErrorException;
14
use Doctrine\Common\Persistence\Mapping\Driver\FileLocator;
15
use Symfony\Component\Yaml\Yaml;
16
use Addiks\RDMBundle\Mapping\Drivers\MappingDriverInterface;
17
use Addiks\RDMBundle\Mapping\EntityMappingInterface;
18
use Addiks\RDMBundle\Mapping\MappingInterface;
19
use Addiks\RDMBundle\Mapping\EntityMapping;
20
use Addiks\RDMBundle\Mapping\ServiceMapping;
21
use Addiks\RDMBundle\Mapping\ChoiceMapping;
22
23
final class MappingYamlDriver implements MappingDriverInterface
24
{
25
26
    /**
27
     * @var FileLocator
28
     */
29
    private $fileLocator;
30
31 3
    public function __construct(
32
        FileLocator $fileLocator
33
    ) {
34 3
        $this->fileLocator = $fileLocator;
35 3
    }
36
37 2
    public function loadRDMMetadataForClass(string $className): ?EntityMappingInterface
38
    {
39
        /** @var ?EntityMappingInterface $mapping */
0 ignored issues
show
Documentation introduced by
The doc-type ?EntityMappingInterface could not be parsed: Unknown type name "?EntityMappingInterface" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
40 2
        $mapping = null;
41
42
        /** @var array<MappingInterface> $fieldMappings */
43 2
        $fieldMappings = array();
44
45 2
        if ($this->fileLocator->fileExists($className)) {
46
            /** @var string $mappingFile */
47 2
            $mappingFile = $this->fileLocator->findMappingFile($className);
48
49 2
            if (file_exists($mappingFile)) {
50
                /** @var mixed $yaml */
51 2
                $yaml = Yaml::parse(file_get_contents($mappingFile));
52
53 2
                if (is_array($yaml) && isset($yaml[$className])) {
54 1
                    $this->readMappings($fieldMappings, $yaml[$className], $mappingFile);
55
                }
56
            }
57
        }
58
59 2
        if (!empty($fieldMappings)) {
60 1
            $mapping = new EntityMapping($className, $fieldMappings);
61
        }
62
63 2
        return $mapping;
64
    }
65
66 1
    private function readMappings(array &$fieldMappings, array $yaml, string $mappingFile): void
67
    {
68 1
        if (isset($yaml['choices'])) {
69 1
            $this->readChoices($fieldMappings, $yaml['choices'], $mappingFile);
70
        }
71 1
        if (isset($yaml['services'])) {
72 1
            $this->readServices($fieldMappings, $yaml['services'], $mappingFile);
73
        }
74 1
    }
75
76 1
    private function readChoices(array &$fieldMappings, array $servicesYaml, string $mappingFile): void
77
    {
78 1
        foreach ($servicesYaml as $fieldName => $choiceYaml) {
79
            /** @var array $yamlService */
80
81 1
            $fieldMappings[$fieldName] = $this->readChoice($choiceYaml, $fieldName, $mappingFile);
82
        }
83 1
    }
84
85 1
    private function readServices(array &$fieldMappings, array $servicesYaml, string $mappingFile): void
86
    {
87 1
        foreach ($servicesYaml as $fieldName => $serviceYaml) {
88
            /** @var array $serviceYaml */
89
90 1
            $fieldMappings[$fieldName] = $this->readService($serviceYaml, $fieldName, $mappingFile);
91
        }
92 1
    }
93
94 1
    private function readOneMapping(array $yaml, string $fieldName, string $mappingFile): ?MappingInterface
95
    {
96 1
        if (isset($yaml['choice'])) {
97
            return $this->readChoice($yaml['choice'], $fieldName, $mappingFile);
98
        }
99 1
        if (isset($yaml['service'])) {
100 1
            return $this->readService($yaml['service'], $fieldName, $mappingFile);
101
        }
102
103
        return null;
104
    }
105
106 1
    private function readChoice(array $choiceYaml, string $fieldName, string $mappingFile): ChoiceMapping
107
    {
108
        /** @var array<MappingInterface> $choiceMappings */
109 1
        $choiceMappings = array();
110
111
        /** @var string $determinatorColumnName */
112 1
        $determinatorColumnName = $fieldName;
113
114 1
        if ($choiceYaml['column']) {
115 1
            $determinatorColumnName = (string)$choiceYaml['column'];
116
        }
117
118 1
        foreach ($choiceYaml['choices'] as $determinator => $choiceYaml) {
119
            /** @var ?MappingInterface $mapping */
0 ignored issues
show
Documentation introduced by
The doc-type ?MappingInterface could not be parsed: Unknown type name "?MappingInterface" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
120 1
            $mapping = $this->readOneMapping($choiceYaml, $fieldName, $mappingFile);
121
122 1
            if ($mapping instanceof MappingInterface) {
123 1
                $choiceMappings[$determinator] = $mapping;
124
            }
125
        }
126
127 1
        return new ChoiceMapping($determinatorColumnName, $choiceMappings, sprintf(
128 1
            "in file '%s'",
129 1
            $mappingFile
130
        ));
131
    }
132
133 1
    private function readService(array $serviceYaml, string $fieldName, string $mappingFile): ServiceMapping
134
    {
135 1
        if (!isset($serviceYaml['id'])) {
136
            throw new ErrorException(sprintf(
137
                "Missing key 'id' on service-reference in file %s on field %s!",
138
                $mappingFile,
139
                $fieldName
140
            ));
141
        }
142
143
        /** @var string $serviceId */
144 1
        $serviceId = $serviceYaml['id'];
145
146
        /** @var bool $lax */
147 1
        $lax = false;
148
149 1
        if (isset($serviceYaml["lax"])) {
150 1
            $lax = (bool)$serviceYaml["lax"];
151
        }
152
153 1
        return new ServiceMapping($serviceId, $lax, sprintf(
154 1
            "in file '%s'",
155 1
            $mappingFile
156
        ));
157
    }
158
159
}
160