XmlDriver::addMetadataFor()   C
last analyzed

Complexity

Conditions 8
Paths 5

Size

Total Lines 40
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 40
rs 5.3846
cc 8
eloc 25
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\Core\Metadata\ClassMetadataInterface;
14
use Cubiche\Core\Metadata\PropertyMetadata;
15
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Metadata\Exception\MappingException;
16
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Metadata\Driver\XmlDriver as BaseXmlDriver;
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: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->className(),
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->className(),
51
                        $fieldName
52
                    );
53
                }
54
55
                $propertyMetadata = new PropertyMetadata($classMetadata->className(), $fieldName);
56
                $propertyMetadata->addMetadata('namespace', 'coordinate');
57
58
                $classMetadata->addPropertyMetadata($propertyMetadata);
59
            } else {
60
                throw MappingException::inField(
61
                    'The cubiche:coordinate configuration is only for field tags that is not an id',
62
                    $classMetadata->className(),
63
                    $fieldName
64
                );
65
            }
66
        }
67
    }
68
}
69