XmlDriver   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
C addMetadataFor() 0 49 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\Identity\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:id') as $item) {
31
            // get the field tag
32
            $field = $item->xpath('..')[0];
33
            $fieldMapping = $this->getMappingAttributes($field);
34
            $fieldName = $fieldMapping['name'];
35
36
            $itemMapping = $this->getMappingAttributes($item);
37
            foreach ($item->attributes() as $key => $value) {
38
                $itemMapping[$key] = (string) $value;
39
            }
40
41
            if (!isset($itemMapping['type'])) {
42
                throw MappingException::inField(
43
                    'The cubiche:id definition should have a "type" value',
44
                    $classMetadata->className(),
45
                    $fieldName
46
                );
47
            }
48
49
            $idType = $itemMapping['type'];
50
51
            if ($field->getName() == 'field') {
52
                if (!isset($fieldMapping['type']) ||
53
                    (isset($fieldMapping['type']) && $fieldMapping['type'] !== 'Identifier')
54
                ) {
55
                    throw MappingException::inField(
56
                        'The cubiche:id configuration is only for id fields',
57
                        $classMetadata->className(),
58
                        $fieldName
59
                    );
60
                }
61
62
                $propertyMetadata = new PropertyMetadata($classMetadata->className(), $fieldName);
63
64
                $propertyMetadata->addMetadata('namespace', 'id');
65
                $propertyMetadata->addMetadata('type', $idType);
66
67
                $classMetadata->addPropertyMetadata($propertyMetadata);
68
            } else {
69
                throw MappingException::inField(
70
                    'The cubiche:id configuration is only for id fields',
71
                    $classMetadata->className(),
72
                    $fieldName
73
                );
74
            }
75
        }
76
    }
77
}
78