XmlDriver::addMetadataForEnum()   C
last analyzed

Complexity

Conditions 10
Paths 11

Size

Total Lines 57
Code Lines 35

Duplication

Lines 16
Ratio 28.07 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 16
loc 57
rs 6.7123
cc 10
eloc 35
nc 11
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of the Cubiche package.
4
 *
5
 * Copyright (c) Cubiche
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cubiche\Infrastructure\System\Doctrine\ODM\MongoDB\Metadata\Driver;
12
13
use Cubiche\Core\Metadata\ClassMetadataInterface;
14
use Cubiche\Core\Metadata\PropertyMetadata;
15
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Metadata\Exception\MappingException;
16
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Metadata\Driver\XmlDriver as BaseXmlDriver;
17
18
/**
19
 * XmlDriver class.
20
 *
21
 * @author Ivannis Suárez Jerez <[email protected]>
22
 */
23
class XmlDriver extends BaseXmlDriver
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function addMetadataFor(\SimpleXMLElement $xmlRoot, ClassMetadataInterface $classMetadata)
29
    {
30
        $this->addMetadataForEnum($xmlRoot, $classMetadata);
31
32
        $this->addMetadataForType('decimal', $xmlRoot, $classMetadata);
33
        $this->addMetadataForType('integer', $xmlRoot, $classMetadata);
34
        $this->addMetadataForType('real', $xmlRoot, $classMetadata);
35
        $this->addMetadataForType('string', $xmlRoot, $classMetadata);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function addMetadataForEnum(\SimpleXMLElement $xmlRoot, ClassMetadataInterface $classMetadata)
42
    {
43
        foreach ($xmlRoot->xpath('//cubiche:enum') as $item) {
44
            // get the field tag
45
            $field = $item->xpath('..')[0];
46
            $fieldMapping = $this->getMappingAttributes($field);
47
            $fieldName = $fieldMapping['name'];
48
49
            $itemMapping = $this->getMappingAttributes($item);
50
            foreach ($item->attributes() as $key => $value) {
51
                $itemMapping[$key] = (string) $value;
52
            }
53
54
            if (!isset($itemMapping['type'])) {
55
                throw MappingException::inField(
56
                    'The cubiche:enum definition should have a "type" value',
57
                    $classMetadata->className(),
58
                    $fieldName
59
                );
60
            }
61
62
            $enumType = $itemMapping['type'];
63
64
            if ($field->getName() == 'field') {
65 View Code Duplication
                if (isset($fieldMapping['id']) && $fieldMapping['id'] !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
66
                    throw MappingException::inField(
67
                        'The cubiche:enum configuration is only for field tags that is not an id',
68
                        $classMetadata->className(),
69
                        $fieldName
70
                    );
71
                }
72
73 View Code Duplication
                if (!isset($fieldMapping['type']) ||
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
74
                    (isset($fieldMapping['type']) && $fieldMapping['type'] !== 'CubicheType')
75
                ) {
76
                    throw MappingException::inField(
77
                        'The cubiche:enum parent should have a "type" value equal to CubicheType',
78
                        $classMetadata->className(),
79
                        $fieldName
80
                    );
81
                }
82
83
                $propertyMetadata = new PropertyMetadata($classMetadata->className(), $fieldName);
84
85
                $propertyMetadata->addMetadata('namespace', 'enum');
86
                $propertyMetadata->addMetadata('type', $enumType);
87
88
                $classMetadata->addPropertyMetadata($propertyMetadata);
89
            } else {
90
                throw MappingException::inField(
91
                    'The cubiche:enum configuration is only for id fields',
92
                    $classMetadata->className(),
93
                    $fieldName
94
                );
95
            }
96
        }
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    protected function addMetadataForType($type, \SimpleXMLElement $xmlRoot, ClassMetadataInterface $classMetadata)
103
    {
104
        foreach ($xmlRoot->xpath('//cubiche:'.$type) as $item) {
105
            // get the field tag
106
            $field = $item->xpath('..')[0];
107
            $fieldMapping = $this->getMappingAttributes($field);
108
            $fieldName = $fieldMapping['name'];
109
110
            if ($field->getName() == 'field') {
111 View Code Duplication
                if (isset($fieldMapping['id']) && $fieldMapping['id'] !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
112
                    throw MappingException::inField(
113
                        'The cubiche:'.$type.' configuration is only for field tags that is not an id',
114
                        $classMetadata->className(),
115
                        $fieldName
116
                    );
117
                }
118
119 View Code Duplication
                if (!isset($fieldMapping['type']) ||
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
120
                    (isset($fieldMapping['type']) && $fieldMapping['type'] !== 'CubicheType')
121
                ) {
122
                    throw MappingException::inField(
123
                        'The cubiche:'.$type.' parent should have a "type" value equal to CubicheType',
124
                        $classMetadata->className(),
125
                        $fieldName
126
                    );
127
                }
128
129
                $propertyMetadata = new PropertyMetadata($classMetadata->className(), $fieldName);
130
                $propertyMetadata->addMetadata('namespace', $type);
131
132
                $classMetadata->addPropertyMetadata($propertyMetadata);
133
            } else {
134
                throw MappingException::inField(
135
                    'The cubiche:'.$type.' configuration is only for id fields',
136
                    $classMetadata->className(),
137
                    $fieldName
138
                );
139
            }
140
        }
141
    }
142
}
143