AnnotationDriver   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 69
c 0
b 0
f 0
wmc 14
lcom 1
cbo 7
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C loadMetadataForClass() 0 63 14
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\Core\Metadata\Tests\Fixtures\Driver;
13
14
use Cubiche\Core\Metadata\ClassMetadata;
15
use Cubiche\Core\Metadata\Driver\AbstractAnnotationDriver;
16
use Cubiche\Core\Metadata\Exception\MappingException;
17
use Cubiche\Core\Metadata\PropertyMetadata;
18
use Cubiche\Core\Metadata\Tests\Fixtures\Annotations\AggregateRoot;
19
use Cubiche\Core\Metadata\Tests\Fixtures\Annotations\Entity;
20
use Cubiche\Core\Metadata\Tests\Fixtures\Annotations\Field;
21
use Cubiche\Core\Metadata\Tests\Fixtures\Annotations\Id;
22
23
/**
24
 * AnnotationDriver class.
25
 *
26
 * @author Ivannis Suárez Jerez <[email protected]>
27
 */
28
class AnnotationDriver extends AbstractAnnotationDriver
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function loadMetadataForClass($className)
34
    {
35
        try {
36
            $hasMetadata = false;
37
            $reflectionClass = new \ReflectionClass($className);
38
39
            foreach ($this->reader->getClassAnnotations($reflectionClass) as $annotation) {
40
                if ($annotation instanceof AggregateRoot) {
41
                    $hasMetadata = true;
42
                } elseif ($annotation instanceof Entity) {
43
                    $hasMetadata = true;
44
                }
45
            }
46
47
            if ($hasMetadata) {
48
                $classMetadata = new ClassMetadata($className);
49
                foreach ($classMetadata->reflection()->getProperties() as $propertyReflection) {
50
                    foreach ($this->reader->getPropertyAnnotations($propertyReflection) as $annotation) {
51
                        if ($annotation instanceof Field) {
52
                            $propertyMetadata = new PropertyMetadata(
53
                                $classMetadata->className(),
54
                                $propertyReflection->getName()
55
                            );
56
57
                            $propertyMetadata->addMetadata('identifier', $annotation->id);
58
                            if (!empty($annotation->name)) {
59
                                $propertyMetadata->addMetadata('name', $annotation->name);
60
                            }
61
62
                            if (!empty($annotation->type)) {
63
                                $propertyMetadata->addMetadata('type', $annotation->type);
64
                                if (!empty($annotation->type)) {
65
                                    $propertyMetadata->addMetadata('of', $annotation->of);
66
                                }
67
                            }
68
69
                            $classMetadata->addPropertyMetadata($propertyMetadata);
70
                        } elseif ($annotation instanceof Id) {
71
                            $propertyMetadata = new PropertyMetadata(
72
                                $classMetadata->className(),
73
                                $propertyReflection->getName()
74
                            );
75
76
                            $propertyMetadata->addMetadata('identifier', true);
77
                            $propertyMetadata->addMetadata('name', '_id');
78
79
                            if (!empty($annotation->type)) {
80
                                $propertyMetadata->addMetadata('type', $annotation->type);
81
                            }
82
83
                            $classMetadata->addPropertyMetadata($propertyMetadata);
84
                        }
85
                    }
86
                }
87
88
                return $classMetadata;
89
            }
90
91
            return;
92
        } catch (\ReflectionException $e) {
93
            throw MappingException::classNotFound($className);
94
        }
95
    }
96
}
97