Completed
Push — master ( ae7699...183a1b )
by Ivannis Suárez
02:08
created

XmlDriver::addMetadataFor()   D

Complexity

Conditions 13
Paths 15

Size

Total Lines 86
Code Lines 57

Duplication

Lines 16
Ratio 18.6 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 16
loc 86
rs 4.9922
c 1
b 0
f 0
cc 13
eloc 57
nc 15
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\Collections\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\Driver\XmlDriver as BaseXmlDriver;
16
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Metadata\Exception\MappingException;
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
        foreach ($xmlRoot->xpath('//cubiche:collection') as $item) {
31
            // get the field tag
32
            $field = $item->xpath('..')[0];
33
            $fieldMapping = $this->getMappingAttributes($field);
34
            $fieldName = isset($fieldMapping['name']) ? $fieldMapping['name'] : $fieldMapping['field'];
35
36
            $itemMapping = $this->getMappingAttributes($item, array('of' => null));
37
            if (!isset($itemMapping['type'])) {
38
                throw MappingException::inField(
39
                    'The cubiche:collection definition should have a "type" value',
40
                    $classMetadata->name,
41
                    $fieldName
42
                );
43
            }
44
45
            $collectionType = $itemMapping['type'];
46
            $collectionOf = $itemMapping['of'];
47
48
            $typeClassName = sprintf(
49
                'Cubiche\\Infrastructure\\Collections\\Doctrine\\ODM\\MongoDB\\Types\\%sType',
50
                $collectionType
51
            );
52
53
            $persistenClassName = sprintf(
54
                'Cubiche\\Infrastructure\\Collections\\Doctrine\\Common\\Collections\\Persistent%s',
55
                $collectionType
56
            );
57
58
            if ($field->getName() == 'field') {
59 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...
60
                    throw MappingException::inField(
61
                        'The cubiche:collection configuration is only for field tags that is not an id',
62
                        $classMetadata->name,
63
                        $fieldName
64
                    );
65
                }
66
67
                if (!isset($fieldMapping['type']) ||
68
                    (isset($fieldMapping['type']) && $fieldMapping['type'] !== 'CubicheType')
69
                ) {
70
                    throw MappingException::inField(
71
                        'The cubiche:collection parent should have a "type" value equal to CubicheType',
72
                        $classMetadata->name,
73
                        $fieldName
74
                    );
75
                }
76
77
                $propertyMetadata = new PropertyMetadata($classMetadata->name, $fieldName);
78
79
                $propertyMetadata->addMetadata('namespace', 'collection');
80
                $propertyMetadata->addMetadata('type', $collectionType);
81
                $propertyMetadata->addMetadata('of', $collectionOf);
82
                $propertyMetadata->addMetadata('typeClassName', $typeClassName);
83
                $propertyMetadata->addMetadata('persistenClassName', $persistenClassName);
84
85
                $classMetadata->addPropertyMetadata($propertyMetadata);
86
            } elseif ($field->getName() == 'embed-many' || $field->getName() == 'reference-many') {
87 View Code Duplication
                if (isset($fieldMapping['field'])) {
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...
88
                    $field = $fieldMapping['field'];
89
                } else {
90
                    throw MappingException::inField(
91
                        'Cannot infer a field',
92
                        $classMetadata->name,
93
                        $fieldName
94
                    );
95
                }
96
97
                $propertyMetadata = new PropertyMetadata($classMetadata->name, $field);
98
99
                $propertyMetadata->addMetadata('namespace', 'collection');
100
                $propertyMetadata->addMetadata('type', $collectionType);
101
                $propertyMetadata->addMetadata('typeClassName', $typeClassName);
102
                $propertyMetadata->addMetadata('persistenClassName', $persistenClassName);
103
104
                $classMetadata->addPropertyMetadata($propertyMetadata);
105
            } else {
106
                throw MappingException::inField(
107
                    'The cubiche:collection configuration is only for field, embed-many or reference-many tags',
108
                    $classMetadata->name,
109
                    $fieldName
110
                );
111
            }
112
        }
113
    }
114
}
115