XmlDriver   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
C addMetadataFor() 0 56 10
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\Core\Metadata\ClassMetadataInterface;
15
use Cubiche\Core\Metadata\PropertyMetadata;
16
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Metadata\Exception\MappingException;
17
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Metadata\Driver\XmlDriver as BaseXmlDriver;
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, ClassMetadataInterface $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->className(),
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->className(),
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->className(),
66
                        $fieldName
67
                    );
68
                }
69
70
                $propertyMetadata = new PropertyMetadata($classMetadata->className(), $fieldName);
71
72
                $propertyMetadata->addMetadata('namespace', 'valueobject');
73
                $propertyMetadata->addMetadata('type', $valueObjectType);
74
75
                $classMetadata->addPropertyMetadata($propertyMetadata);
76
            } else {
77
                throw MappingException::inField(
78
                    'The cubiche:valueobject configuration is only for field tags that is not an id',
79
                    $classMetadata->className(),
80
                    $fieldName
81
                );
82
            }
83
        }
84
    }
85
}
86