Completed
Push — master ( d8e77e...e71ae9 )
by Ivannis Suárez
05:32
created

XmlDriver::addMetadataFor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 9
rs 9.6666
c 1
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
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\Infrastructure\Doctrine\ODM\MongoDB\Metadata\Exception\MappingException;
14
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Metadata\Driver\XmlDriver as BaseXmlDriver;
15
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Metadata\PropertyMetadata;
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
        $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, MergeableClassMetadata $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->name,
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->name,
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->name,
79
                        $fieldName
80
                    );
81
                }
82
83
                $propertyMetadata = new PropertyMetadata($classMetadata->name, $fieldName, 'enum');
84
                $propertyMetadata->setType($enumType);
85
86
                $classMetadata->addPropertyMetadata($propertyMetadata);
87
            } else {
88
                throw MappingException::inField(
89
                    'The cubiche:enum configuration is only for id fields',
90
                    $classMetadata->name,
91
                    $fieldName
92
                );
93
            }
94
        }
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    protected function addMetadataForType($type, \SimpleXMLElement $xmlRoot, MergeableClassMetadata $classMetadata)
101
    {
102
        foreach ($xmlRoot->xpath('//cubiche:'.$type) as $item) {
103
            // get the field tag
104
            $field = $item->xpath('..')[0];
105
            $fieldMapping = $this->getMappingAttributes($field);
106
            $fieldName = $fieldMapping['name'];
107
108
            if ($field->getName() == 'field') {
109 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...
110
                    throw MappingException::inField(
111
                        'The cubiche:'.$type.' configuration is only for field tags that is not an id',
112
                        $classMetadata->name,
113
                        $fieldName
114
                    );
115
                }
116
117 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...
118
                    (isset($fieldMapping['type']) && $fieldMapping['type'] !== 'CubicheType')
119
                ) {
120
                    throw MappingException::inField(
121
                        'The cubiche:'.$type.' parent should have a "type" value equal to CubicheType',
122
                        $classMetadata->name,
123
                        $fieldName
124
                    );
125
                }
126
127
                $propertyMetadata = new PropertyMetadata($classMetadata->name, $fieldName, $type);
128
129
                $classMetadata->addPropertyMetadata($propertyMetadata);
130
            } else {
131
                throw MappingException::inField(
132
                    'The cubiche:'.$type.' configuration is only for id fields',
133
                    $classMetadata->name,
134
                    $fieldName
135
                );
136
            }
137
        }
138
    }
139
}
140