XmlDriver::addMetadataFor()   D
last analyzed

Complexity

Conditions 13
Paths 15

Size

Total Lines 86
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 86
rs 4.9922
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->className(),
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
                if (isset($fieldMapping['id']) && $fieldMapping['id'] !== false) {
60
                    throw MappingException::inField(
61
                        'The cubiche:collection configuration is only for field tags that is not an id',
62
                        $classMetadata->className(),
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->className(),
73
                        $fieldName
74
                    );
75
                }
76
77
                $propertyMetadata = new PropertyMetadata($classMetadata->className(), $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
                if (isset($fieldMapping['field'])) {
88
                    $field = $fieldMapping['field'];
89
                } else {
90
                    throw MappingException::inField(
91
                        'Cannot infer a field',
92
                        $classMetadata->className(),
93
                        $fieldName
94
                    );
95
                }
96
97
                $propertyMetadata = new PropertyMetadata($classMetadata->className(), $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->className(),
109
                    $fieldName
110
                );
111
            }
112
        }
113
    }
114
}
115