Completed
Push — master ( f56041...758eef )
by Fabian
02:38
created

ConfigurationFactory::configureManyToOne()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 6
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
    public function factory($configuration)
59
    {
60
        $configurationArray = ArrayUtils::iteratorToArray($configuration, true);
61
62
        $configurationObject = new Configuration;
63
        foreach ($configurationArray as $entityName => $entityConfig) {
64
            $entity = new Configuration\Entity($entityName);
65
66
            if (isset($entityConfig['options'])) {
67
                $this->configureOptions($entity, $entityConfig['options']);
68
            }
69
70
            if (isset($entityConfig['fields'])) {
71
                $this->configureFields($entity, $entityConfig['fields']);
72
            }
73
            $configurationObject->add($entity);
74
        }
75
        return $configurationObject;
76
    }
77
78
    /**
79
     * @param \Fabiang\DoctrineDynamic\Configuration\Entity $entity
80
     * @param array $entityConfig
81
     */
82
    private function configureFields(Configuration\Entity $entity, array $entityConfig)
83
    {
84
        foreach ($entityConfig as $fieldName => $fieldConfig) {
85
            $field = new Configuration\Field($fieldName);
86
            $this->configureMappings(
87
                $field,
88
                $fieldConfig,
89
                $entity->getName()
90
            );
91
            $entity->addField($field);
92
        }
93
    }
94
95
    private function configureOptions(Configuration\Entity $entity, array $options)
96
    {
97
        foreach ($options as $option => $value) {
98
            $setter = 'set' . ucfirst($option);
99
            $entity->{$setter}($value);
100
        }
101
    }
102
103
    /**
104
     * @param \Fabiang\DoctrineDynamic\Configuration\Field $field
105
     * @param array $fieldConfig
106
     * @param string $entityName
107
     * @throws UnexpectedValueException
108
     */
109
    private function configureMappings(Configuration\Field $field, array $fieldConfig, $entityName)
110
    {
111
        foreach ($this->mappings as $mappingType => $mappingClassName) {
112
            if (isset($fieldConfig[$mappingType])) {
113
                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
                $mappingConfigList = $fieldConfig[$mappingType];
123
124
                $mappingMethod = 'configure' . ucfirst($mappingType);
125
                $fieldMethod   = 'add' . ucfirst($mappingType);
126
                foreach ($mappingConfigList as $mappingConfig) {
127
                    $field->$fieldMethod($this->$mappingMethod($mappingConfig));
128
                }
129
            }
130
        }
131
    }
132
133
    /**
134
     * @param array $mappingConfig
135
     * @return \Fabiang\DoctrineDynamic\Configuration\Mapping\OneToOne
136
     */
137 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
        $oneToOne = new Configuration\Mapping\OneToOne;
140
        $oneToOne->setTargetEntity($this->getOption($mappingConfig, 'targetEntity', true));
141
        $oneToOne->setInversedBy($this->getOption($mappingConfig, 'inversedBy'));
142
        $oneToOne->setMappedBy($this->getOption($mappingConfig, 'mappedBy'));
143
144
        if (isset($mappingConfig['joinColumns'])) {
145
            $oneToOne->setJoinColumn($this->configureJoinColumn($mappingConfig['joinColumns']));
146
        }
147
148
        return $oneToOne;
149
    }
150
151
    private function configureManyToOne(array $mappingConfig)
152
    {
153
        $manyToOne = new Configuration\Mapping\ManyToOne;
154
        $manyToOne->setTargetEntity($this->getOption($mappingConfig, 'targetEntity', true));
155
        $manyToOne->setInversedBy($this->getOption($mappingConfig, 'inversedBy'));
156
157
        if (isset($mappingConfig['joinColumns'])) {
158
            $manyToOne->setJoinColumn($this->configureJoinColumn($mappingConfig['joinColumns']));
159
        }
160
161
        return $manyToOne;
162
    }
163
164
    private function configureOneToMany(array $mappingConfig)
165
    {
166
        $oneToMany = new Configuration\Mapping\OneToMany;
167
        $oneToMany->setTargetEntity($this->getOption($mappingConfig, 'targetEntity', true));
168
        $oneToMany->setMappedBy($this->getOption($mappingConfig, 'mappedBy'));
169
        return $oneToMany;
170
    }
171
172 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
        $manyToMany = new Configuration\Mapping\ManyToMany;
175
        $manyToMany->setTargetEntity($this->getOption($mappingConfig, 'targetEntity', true));
176
        $manyToMany->setInversedBy($this->getOption($mappingConfig, 'inversedBy'));
177
        $manyToMany->setMappedBy($this->getOption($mappingConfig, 'mappedBy'));
178
179
        if (isset($mappingConfig['joinTable'])) {
180
            $manyToMany->setJoinTable($this->configureJoinTable($mappingConfig['joinTable']));
181
        }
182
183
        return $manyToMany;
184
    }
185
186
    /**
187
     * @param array $joinColumnConfig
188
     * @return \Fabiang\DoctrineDynamic\Configuration\Mapping\JoinColumn
189
     */
190
    private function configureJoinColumn(array $joinColumnConfig)
191
    {
192
        $name                 = $this->getOption($joinColumnConfig, 'name', true);
193
        $referencedColumnName = $this->getOption($joinColumnConfig, 'referencedColumnName', true);
194
195
        $joinColumn = new Configuration\Mapping\JoinColumn($name, $referencedColumnName);
196
        return $joinColumn;
197
    }
198
199
    private function configureJoinTable(array $joinTableConfig)
200
    {
201
        $name = $this->getOption($joinTableConfig, 'name', true);
202
203
        $joinTable = new Configuration\Mapping\JoinTable($name);
204
        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
    private function getOption(array $config, $option, $required = false)
215
    {
216
        if (!isset($config[$option])) {
217
            if ($required) {
218
                throw new RuntimeException(sprintf(
219
                    'Configuration for field "%s" is required',
220
                    $option
221
                ));
222
            }
223
224
            return null;
225
        }
226
227
        return $config[$option];
228
    }
229
}
230