Completed
Push — master ( e28974...eca6d5 )
by Ivannis Suárez
06:38
created

XmlDriver   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 58
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addMetadataFor() 0 9 1
C addMetadataForType() 0 39 8
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\Web\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->addMetadataForType('email', $xmlRoot, $classMetadata);
31
        $this->addMetadataForType('hostname', $xmlRoot, $classMetadata);
32
        $this->addMetadataForType('ip', $xmlRoot, $classMetadata);
33
        $this->addMetadataForType('path', $xmlRoot, $classMetadata);
34
        $this->addMetadataForType('port', $xmlRoot, $classMetadata);
35
        $this->addMetadataForType('url', $xmlRoot, $classMetadata);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function addMetadataForType($type, \SimpleXMLElement $xmlRoot, MergeableClassMetadata $classMetadata)
42
    {
43
        foreach ($xmlRoot->xpath('//cubiche:'.$type) as $item) {
44
            // get the field tag
45
            $field = $item->xpath('..')[0];
46
            $fieldMapping = $this->getMappingAttributes($field);
47
            $fieldName = $fieldMapping['name'];
48
49
            if ($field->getName() == 'field') {
50
                if (isset($fieldMapping['id']) && $fieldMapping['id'] !== false) {
51
                    throw MappingException::inField(
52
                        'The cubiche:'.$type.' configuration is only for field tags that is not an id',
53
                        $classMetadata->name,
54
                        $fieldName
55
                    );
56
                }
57
58
                if (!isset($fieldMapping['type']) ||
59
                    (isset($fieldMapping['type']) && $fieldMapping['type'] !== 'CubicheType')
60
                ) {
61
                    throw MappingException::inField(
62
                        'The cubiche:'.$type.' parent should have a "type" value equal to CubicheType',
63
                        $classMetadata->name,
64
                        $fieldName
65
                    );
66
                }
67
68
                $propertyMetadata = new PropertyMetadata($classMetadata->name, $fieldName, $type);
69
70
                $classMetadata->addPropertyMetadata($propertyMetadata);
71
            } else {
72
                throw MappingException::inField(
73
                    'The cubiche:'.$type.' configuration is only for id fields',
74
                    $classMetadata->name,
75
                    $fieldName
76
                );
77
            }
78
        }
79
    }
80
}
81