Completed
Push — master ( 7ccce4...5719a4 )
by Ivannis Suárez
08:49
created

XmlDriver::addMetadataFor()   C

Complexity

Conditions 8
Paths 5

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 39
rs 5.3846
cc 8
eloc 24
nc 5
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\Geolocation\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
        foreach ($xmlRoot->xpath('//cubiche:coordinate') as $item) {
31
            // get the field tag
32
            $field = $item->xpath('..')[0];
33
            $fieldMapping = $this->getMappingAttributes($field);
34
            $fieldName = $fieldMapping['name'];
35
36
            if ($field->getName() == 'field') {
37
                if (isset($fieldMapping['id']) && $fieldMapping['id'] !== false) {
38
                    throw MappingException::inField(
39
                        'The cubiche:coordinate configuration is only for field tags that is not an id',
40
                        $classMetadata->name,
41
                        $fieldName
42
                    );
43
                }
44
45
                if (!isset($fieldMapping['type']) ||
46
                    (isset($fieldMapping['type']) && $fieldMapping['type'] !== 'CubicheType')
47
                ) {
48
                    throw MappingException::inField(
49
                        'The cubiche:coordinate parent should have a "type" value equal to CubicheType',
50
                        $classMetadata->name,
51
                        $fieldName
52
                    );
53
                }
54
55
                $propertyMetadata = new PropertyMetadata($classMetadata->name, $fieldName, 'coordinate');
56
57
                $classMetadata->addPropertyMetadata($propertyMetadata);
58
            } else {
59
                throw MappingException::inField(
60
                    'The cubiche:coordinate configuration is only for field tags that is not an id',
61
                    $classMetadata->name,
62
                    $fieldName
63
                );
64
            }
65
        }
66
    }
67
}
68