Completed
Push — master ( 4fecc2...aa5437 )
by Fabian
07:26
created

ConfigurationFactory::factory()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 14
cts 14
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 5
nop 1
crap 4
1
<?php
2
3
/**
4
 * Copyright 2015 Fabian Grutschus. All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without modification,
7
 * are permitted provided that the following conditions are met:
8
 *
9
 * 1. Redistributions of source code must retain the above copyright notice, this
10
 *   list of conditions and the following disclaimer.
11
 *
12
 * 2. Redistributions in binary form must reproduce the above copyright notice,
13
 *   this list of conditions and the following disclaimer in the documentation
14
 *   and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 *
27
 * The views and conclusions contained in the software and documentation are those
28
 * of the authors and should not be interpreted as representing official policies,
29
 * either expressed or implied, of the copyright holders.
30
 *
31
 * @author    Fabian Grutschus <[email protected]>
32
 * @copyright 2015 Fabian Grutschus. All rights reserved.
33
 * @license   BSD-2-Clause
34
 */
35
36
namespace Fabiang\DoctrineDynamic;
37
38
use Zend\Stdlib\ArrayUtils;
39
use Fabiang\DoctrineDynamic\Exception\UnexpectedValueException;
40
use Fabiang\DoctrineDynamic\Exception\RuntimeException;
41
42
class ConfigurationFactory
43
{
44
    /**
45
     * @var array
46
     */
47
    private $mappings = [
48
        'oneToOne'   => Configuration\Mapping\OneToOne::class,
49
        'manyToOne'  => Configuration\Mapping\ManyToOne::class,
50
        'oneToMany'  => Configuration\Mapping\OneToMany::class,
51
        'manyToMany' => Configuration\Mapping\ManyToMany::class,
52
    ];
53
54
    /**
55
     * @param array|\Traversable $configuration
56
     * @return Configuration
57
     */
58 1
    public function factory($configuration)
59
    {
60 1
        $configurationArray = ArrayUtils::iteratorToArray($configuration, true);
61
62 1
        $configurationObject = new Configuration;
63 1
        foreach ($configurationArray as $entityName => $entityConfig) {
64 1
            $entity = new Configuration\Entity($entityName);
65
66 1
            if (isset($entityConfig['options'])) {
67 1
                $this->configureOptions($entity, $entityConfig['options']);
68 1
            }
69
70 1
            if (isset($entityConfig['fields'])) {
71 1
                $this->configureFields($entity, $entityConfig['fields']);
72 1
            }
73 1
            $configurationObject->add($entity);
74 1
        }
75 1
        return $configurationObject;
76
    }
77
78
    /**
79
     * @param \Fabiang\DoctrineDynamic\Configuration\Entity $entity
80
     * @param array $entityConfig
81
     */
82 1
    private function configureFields(Configuration\Entity $entity, array $entityConfig)
83
    {
84 1
        foreach ($entityConfig as $fieldName => $fieldConfig) {
85 1
            $field = new Configuration\Field($fieldName);
86 1
            $this->configureMappings(
87 1
                $field,
88 1
                $fieldConfig,
89 1
                $entity->getName()
90 1
            );
91 1
            $entity->addField($field);
92 1
        }
93 1
    }
94
95 1
    private function configureOptions(Configuration\Entity $entity, array $options)
96
    {
97 1
        foreach ($options as $option => $value) {
98 1
            $setter = 'set' . ucfirst($option);
99 1
            $entity->{$setter}($value);
100 1
        }
101 1
    }
102
103
    /**
104
     * @param \Fabiang\DoctrineDynamic\Configuration\Field $field
105
     * @param array $fieldConfig
106
     * @param string $entityName
107
     * @throws UnexpectedValueException
108
     */
109 1
    private function configureMappings(Configuration\Field $field, array $fieldConfig, $entityName)
110
    {
111 1
        foreach ($this->mappings as $mappingType => $mappingClassName) {
112 1
            if (isset($fieldConfig[$mappingType])) {
113 1
                if (!ArrayUtils::isList($fieldConfig[$mappingType])) {
114
                    throw new UnexpectedValueException(sprintf(
115
                        'Mapping definition for field "%s" at entity "%s" of mapping type "%s" must be an list',
116
                        $field->getName(),
117
                        $entityName,
118
                        $mappingType
119
                    ));
120
                }
121
122 1
                $mappingConfigList = $fieldConfig[$mappingType];
123
124 1
                $mappingMethod = 'configure' . ucfirst($mappingType);
125 1
                $fieldMethod   = 'add' . ucfirst($mappingType);
126 1
                foreach ($mappingConfigList as $mappingConfig) {
127 1
                    $field->$fieldMethod($this->$mappingMethod($mappingConfig));
128 1
                }
129 1
            }
130 1
        }
131 1
    }
132
133
    /**
134
     * @param array $mappingConfig
135
     * @return \Fabiang\DoctrineDynamic\Configuration\Mapping\OneToOne
136
     */
137 1 View Code Duplication
    private function configureOneToOne(array $mappingConfig)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
    {
139 1
        $oneToOne = new Configuration\Mapping\OneToOne;
140 1
        $oneToOne->setTargetEntity($this->getOption($mappingConfig, 'targetEntity', true));
141 1
        $oneToOne->setInversedBy($this->getOption($mappingConfig, 'inversedBy'));
142 1
        $oneToOne->setMappedBy($this->getOption($mappingConfig, 'mappedBy'));
143
144 1
        if (isset($mappingConfig['joinColumns'])) {
145 1
            $oneToOne->setJoinColumn($this->configureJoinColumn($mappingConfig['joinColumns']));
146 1
        }
147
148 1
        return $oneToOne;
149
    }
150
151 1
    private function configureManyToOne(array $mappingConfig)
152
    {
153 1
        $manyToOne = new Configuration\Mapping\ManyToOne;
154 1
        $manyToOne->setTargetEntity($this->getOption($mappingConfig, 'targetEntity', true));
155 1
        $manyToOne->setInversedBy($this->getOption($mappingConfig, 'inversedBy'));
156
157 1
        if (isset($mappingConfig['joinColumns'])) {
158 1
            $manyToOne->setJoinColumn($this->configureJoinColumn($mappingConfig['joinColumns']));
159 1
        }
160
161 1
        return $manyToOne;
162
    }
163
164 1
    private function configureOneToMany(array $mappingConfig)
165
    {
166 1
        $oneToMany = new Configuration\Mapping\OneToMany;
167 1
        $oneToMany->setTargetEntity($this->getOption($mappingConfig, 'targetEntity', true));
168 1
        $oneToMany->setMappedBy($this->getOption($mappingConfig, 'mappedBy'));
169 1
        return $oneToMany;
170
    }
171
172 1 View Code Duplication
    private function configureManyToMany(array $mappingConfig)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
173
    {
174 1
        $manyToMany = new Configuration\Mapping\ManyToMany;
175 1
        $manyToMany->setTargetEntity($this->getOption($mappingConfig, 'targetEntity', true));
176 1
        $manyToMany->setInversedBy($this->getOption($mappingConfig, 'inversedBy'));
177 1
        $manyToMany->setMappedBy($this->getOption($mappingConfig, 'mappedBy'));
178
179 1
        if (isset($mappingConfig['joinTable'])) {
180 1
            $manyToMany->setJoinTable($this->configureJoinTable($mappingConfig['joinTable']));
181 1
        }
182
183 1
        return $manyToMany;
184
    }
185
186
    /**
187
     * @param array $joinColumnConfig
188
     * @return \Fabiang\DoctrineDynamic\Configuration\Mapping\JoinColumn
189
     */
190 1
    private function configureJoinColumn(array $joinColumnConfig)
191
    {
192 1
        $name                 = $this->getOption($joinColumnConfig, 'name', true);
193 1
        $referencedColumnName = $this->getOption($joinColumnConfig, 'referencedColumnName', true);
194
195 1
        $joinColumn = new Configuration\Mapping\JoinColumn($name, $referencedColumnName);
196 1
        return $joinColumn;
197
    }
198
199 1
    private function configureJoinTable(array $joinTableConfig)
200
    {
201 1
        $name = $this->getOption($joinTableConfig, 'name', true);
202
203 1
        $joinTable = new Configuration\Mapping\JoinTable($name);
204 1
        return $joinTable;
205
    }
206
207
    /**
208
     * @param array $config
209
     * @param string $option
210
     * @param boolean $required
211
     * @return array
212
     * @throws RuntimeException
213
     */
214 1
    private function getOption(array $config, $option, $required = false)
215
    {
216 1
        if (!isset($config[$option])) {
217 1
            if ($required) {
218
                throw new RuntimeException(sprintf(
219
                    'Configuration for field "%s" is required',
220
                    $option
221
                ));
222
            }
223
224 1
            return null;
225
        }
226
227 1
        return $config[$option];
228
    }
229
}
230