Completed
Push — master ( 25f441...bf5e1f )
by Ivannis Suárez
05:09
created

XmlDriver::addMetadataFor()   C

Complexity

Conditions 10
Paths 11

Size

Total Lines 54
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 6.8372
c 0
b 0
f 0
cc 10
eloc 34
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
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Cubiche\Infrastructure\Model\Doctrine\ODM\MongoDB\Metadata\Driver;
13
14
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Metadata\Exception\MappingException;
15
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Metadata\Driver\XmlDriver as BaseXmlDriver;
16
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Metadata\PropertyMetadata;
17
use Cubiche\Core\Metadata\MergeableClassMetadata;
18
19
/**
20
 * XmlDriver class.
21
 *
22
 * @author Ivannis Suárez Jerez <[email protected]>
23
 */
24
class XmlDriver extends BaseXmlDriver
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    protected function addMetadataFor(\SimpleXMLElement $xmlRoot, MergeableClassMetadata $classMetadata)
30
    {
31
        foreach ($xmlRoot->xpath('//cubiche:valueobject') as $item) {
32
            // get the field tag
33
            $field = $item->xpath('..')[0];
34
            $fieldMapping = $this->getMappingAttributes($field);
35
            $fieldName = $fieldMapping['name'];
36
37
            $itemMapping = $this->getMappingAttributes($item);
38
            foreach ($item->attributes() as $key => $value) {
39
                $itemMapping[$key] = (string) $value;
40
            }
41
42
            if (!isset($itemMapping['type'])) {
43
                throw MappingException::inField(
44
                    'The cubiche:valueobject definition should have a "type" value',
45
                    $classMetadata->name,
46
                    $fieldName
47
                );
48
            }
49
50
            $valueObjectType = $itemMapping['type'];
51
            if ($field->getName() == 'field') {
52
                if (isset($fieldMapping['id']) && $fieldMapping['id'] !== false) {
53
                    throw MappingException::inField(
54
                        'The cubiche:valueobject configuration is only for field tags that is not an id',
55
                        $classMetadata->name,
56
                        $fieldName
57
                    );
58
                }
59
60
                if (!isset($fieldMapping['type']) ||
61
                    (isset($fieldMapping['type']) && $fieldMapping['type'] !== 'CubicheType')
62
                ) {
63
                    throw MappingException::inField(
64
                        'The cubiche:valueobject parent should have a "type" value equal to CubicheType',
65
                        $classMetadata->name,
66
                        $fieldName
67
                    );
68
                }
69
70
                $propertyMetadata = new PropertyMetadata($classMetadata->name, $fieldName, 'valueobject');
71
                $propertyMetadata->setType($valueObjectType);
72
73
                $classMetadata->addPropertyMetadata($propertyMetadata);
74
            } else {
75
                throw MappingException::inField(
76
                    'The cubiche:valueobject configuration is only for field tags that is not an id',
77
                    $classMetadata->name,
78
                    $fieldName
79
                );
80
            }
81
        }
82
    }
83
}
84