Completed
Push — master ( b9cd4a...bf5ad5 )
by Ivannis Suárez
06:04
created

XmlDriver::addMetadataFor()   C

Complexity

Conditions 13
Paths 15

Size

Total Lines 68
Code Lines 45

Duplication

Lines 16
Ratio 23.53 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 16
loc 68
rs 5.761
c 1
b 0
f 0
cc 13
eloc 45
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\Infrastructure\Collections\Doctrine\ODM\MongoDB\Metadata\CollectionPropertyMetadata;
14
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Metadata\Driver\XmlDriver as BaseXmlDriver;
15
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Metadata\Exception\MappingException;
16
use Cubiche\Core\Metadata\MergeableClassMetadata;
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, MergeableClassMetadata $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
            if ($field->getName() == 'field') {
49 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...
50
                    throw MappingException::inField(
51
                        'The cubiche:collection configuration is only for field tags that is not an id',
52
                        $classMetadata->name,
53
                        $fieldName
54
                    );
55
                }
56
57
                if (!isset($fieldMapping['type']) ||
58
                    (isset($fieldMapping['type']) && $fieldMapping['type'] !== 'CubicheType')
59
                ) {
60
                    throw MappingException::inField(
61
                        'The cubiche:collection parent should have a "type" value equal to CubicheType',
62
                        $classMetadata->name,
63
                        $fieldName
64
                    );
65
                }
66
67
                $propertyMetadata = new CollectionPropertyMetadata($classMetadata->name, $fieldName);
68
                $propertyMetadata->setType($collectionType);
69
                $propertyMetadata->setOf($collectionOf);
70
71
                $classMetadata->addPropertyMetadata($propertyMetadata);
72
            } elseif ($field->getName() == 'embed-many' || $field->getName() == 'reference-many') {
73 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...
74
                    $field = $fieldMapping['field'];
75
                } else {
76
                    throw MappingException::inField(
77
                        'Cannot infer a field',
78
                        $classMetadata->name,
79
                        $fieldName
80
                    );
81
                }
82
83
                $propertyMetadata = new CollectionPropertyMetadata($classMetadata->name, $field);
84
                $propertyMetadata->setType($collectionType);
85
86
                $classMetadata->addPropertyMetadata($propertyMetadata);
87
            } else {
88
                throw MappingException::inField(
89
                    'The cubiche:collection configuration is only for field, embed-many or reference-many tags',
90
                    $classMetadata->name,
91
                    $fieldName
92
                );
93
            }
94
        }
95
    }
96
}
97