Completed
Push — master ( 63fe9e...7c1db9 )
by Gerrit
02:35
created

ChoiceMapping   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 92
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 25 3
A getChoices() 0 4 1
A collectDBALColumns() 0 18 2
A getDeterminatorColumnName() 0 4 1
A addChoice() 0 4 1
A describeOrigin() 0 4 1
A getDeterminatorColumn() 0 4 1
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;
12
13
use Addiks\RDMBundle\Mapping\ChoiceMappingInterface;
14
use Addiks\RDMBundle\Mapping\MappingInterface;
15
use Doctrine\DBAL\Schema\Column;
16
use Doctrine\DBAL\Types\Type;
17
use Doctrine\DBAL\Types\TextType;
18
use Addiks\RDMBundle\Exception\InvalidMappingException;
19
20
final class ChoiceMapping implements ChoiceMappingInterface
21
{
22
23
    /**
24
     * @var Column
25
     */
26
    private $determinatorColumn;
27
28
    /**
29
     * @var array<MappingInterface>
30
     */
31
    private $choiceMappings = array();
32
33
    /**
34
     * @var string
35
     */
36
    private $originDescription;
37
38
    /**
39
     * @param string|Column $determinatorColumn
40
     */
41 18
    public function __construct(
42
        $determinatorColumn,
43
        array $choiceMappings,
44
        string $originDescription = ""
45
    ) {
46 18
        if (!$determinatorColumn instanceof Column) {
47 10
            $determinatorColumn = new Column(
48 10
                (string)$determinatorColumn,
49 10
                Type::getType('string'),
50
                [
51 10
                    'notnull' => false,
52
                    'length'  => 255,
53
                ]
54
            );
55
        }
56
57 18
        $this->determinatorColumn = clone $determinatorColumn;
58 18
        $this->originDescription = $originDescription;
59
60 18
        foreach ($choiceMappings as $determinator => $choiceMapping) {
61
            /** @var MappingInterface $choiceMapping */
62
63 13
            $this->addChoice($determinator, $choiceMapping);
64
        }
65 18
    }
66
67 6
    public function getChoices(): array
68
    {
69 6
        return $this->choiceMappings;
70
    }
71
72 1
    public function describeOrigin(): string
73
    {
74 1
        return $this->originDescription;
75
    }
76
77 4
    public function collectDBALColumns(): array
78
    {
79
        /** @var array<Column> $additionalMappings */
80
        $additionalMappings = array(
81 4
            clone $this->determinatorColumn
82
        );
83
84 4
        foreach ($this->choiceMappings as $choiceMapping) {
85
            /** @var MappingInterface $choiceMapping */
86
87 2
            $additionalMappings = array_merge(
88 2
                $additionalMappings,
89 2
                $choiceMapping->collectDBALColumns()
90
            );
91
        }
92
93 4
        return $additionalMappings;
94
    }
95
96 1
    public function getDeterminatorColumn(): Column
97
    {
98 1
        return clone $this->determinatorColumn;
99
    }
100
101 6
    public function getDeterminatorColumnName(): string
102
    {
103 6
        return $this->determinatorColumn->getName();
104
    }
105
106 13
    private function addChoice(string $determinator, MappingInterface $choiceMapping): void
107
    {
108 13
        $this->choiceMappings[$determinator] = $choiceMapping;
109 13
    }
110
111
}
112