Completed
Push — standalone ( 83bb11...ac8c4f )
by Philip
03:51
created

AnnotationDriver   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 82.26%

Importance

Changes 0
Metric Value
wmc 25
c 0
b 0
f 0
lcom 1
cbo 6
dl 0
loc 129
ccs 51
cts 62
cp 0.8226
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
F loadMetadataForClass() 0 110 24
1
<?php
2
namespace Dontdrinkandroot\RestBundle\Metadata\Driver;
3
4
use Doctrine\Common\Annotations\Reader;
5
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Excluded;
6
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Includable;
7
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Postable;
8
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Puttable;
9
use Dontdrinkandroot\RestBundle\Metadata\Annotation\RootResource;
10
use Dontdrinkandroot\RestBundle\Metadata\Annotation\SubResource;
11
use Dontdrinkandroot\RestBundle\Metadata\ClassMetadata;
12
use Dontdrinkandroot\RestBundle\Metadata\PropertyMetadata;
13
use Metadata\Driver\DriverInterface;
14
15
class AnnotationDriver implements DriverInterface
16
{
17
    private $reader;
18
19
    /**
20
     * @var DriverInterface
21
     */
22
    private $doctrineDriver;
23
24 26
    public function __construct(Reader $reader, DriverInterface $doctrineDriver)
25
    {
26 26
        $this->reader = $reader;
27 26
        $this->doctrineDriver = $doctrineDriver;
28 26
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 26
    public function loadMetadataForClass(\ReflectionClass $class)
34
    {
35
        /** @var ClassMetadata $ddrRestClassMetadata */
36 26
        $ddrRestClassMetadata = $this->doctrineDriver->loadMetadataForClass($class);
37 26
        if (null === $ddrRestClassMetadata) {
38
            $ddrRestClassMetadata = new ClassMetadata($class->getName());
1 ignored issue
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
39
        }
40
41
        /** @var RootResource $restResourceAnnotation */
42 26
        $restResourceAnnotation = $this->reader->getClassAnnotation($class, RootResource::class);
43 26
        if (null !== $restResourceAnnotation) {
44
45 26
            $ddrRestClassMetadata->setRestResource(true);
46
47 26
            if (null !== $restResourceAnnotation->namePrefix) {
48
                $ddrRestClassMetadata->setNamePrefix($restResourceAnnotation->namePrefix);
49
            }
50
51 26
            if (null !== $restResourceAnnotation->pathPrefix) {
52 16
                $ddrRestClassMetadata->setPathPrefix($restResourceAnnotation->pathPrefix);
53
            }
54
55 26
            if (null !== $restResourceAnnotation->service) {
56
                $ddrRestClassMetadata->setService($restResourceAnnotation->service);
57
            }
58
59 26
            if (null !== $restResourceAnnotation->controller) {
60
                $ddrRestClassMetadata->setController($restResourceAnnotation->controller);
61
            }
62
63 26
            if (null !== $restResourceAnnotation->listRight) {
64 16
                $ddrRestClassMetadata->setListRight($restResourceAnnotation->listRight);
65
            }
66
67 26
            if (null !== $restResourceAnnotation->postRight) {
68
                $ddrRestClassMetadata->setPostRight($restResourceAnnotation->postRight);
69
            }
70
71 26
            if (null !== $restResourceAnnotation->getRight) {
72 16
                $ddrRestClassMetadata->setGetRight($restResourceAnnotation->getRight);
73
            }
74
75 26
            if (null !== $restResourceAnnotation->putRight) {
76 16
                $ddrRestClassMetadata->setPutRight($restResourceAnnotation->putRight);
77
            }
78
79 26
            if (null !== $restResourceAnnotation->deleteRight) {
80
                $ddrRestClassMetadata->setDeleteRight($restResourceAnnotation->deleteRight);
81
            }
82
83 26
            if (null !== $restResourceAnnotation->methods) {
84
                $ddrRestClassMetadata->setMethods($restResourceAnnotation->methods);
85
            }
86
        }
87
88 26
        foreach ($class->getProperties() as $reflectionProperty) {
89
90 26
            $propertyMetadata = $ddrRestClassMetadata->getPropertyMetadata($reflectionProperty->getName());
91 26
            if (null === $propertyMetadata) {
92 16
                $propertyMetadata = new PropertyMetadata($class->getName(), $reflectionProperty->getName());
1 ignored issue
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
93
            }
94
95 26
            $puttableAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Puttable::class);
96 26
            if (null !== $puttableAnnotation) {
97 16
                $propertyMetadata->setPuttable(true);
98
            }
99
100 26
            $postableAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Postable::class);
101 26
            if (null !== $postableAnnotation) {
102
                $propertyMetadata->setPostable(true);
103
            }
104
105 26
            $includableAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Includable::class);
106 26
            if (null !== $includableAnnotation) {
107 16
                $paths = $includableAnnotation->paths;
108 16
                if (null === $paths) {
109 16
                    $paths = [$reflectionProperty->name];
110
                }
111 16
                $propertyMetadata->setIncludable(true);
112 16
                $propertyMetadata->setIncludablePaths($paths);
113
            }
114
115 26
            $excludedAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Excluded::class);
116 26
            if (null !== $excludedAnnotation) {
117 16
                $propertyMetadata->setExcluded(true);
118
            }
119
120
            /** @var SubResource $subResourceAnnotation */
121 26
            $subResourceAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, SubResource::class);
122 26
            if (null !== $subResourceAnnotation) {
123
124 16
                $propertyMetadata->setSubResource(true);
125 16
                if (null !== $subResourceAnnotation->listRight) {
126
                    $propertyMetadata->setSubResourceListRight($subResourceAnnotation->listRight);
127
                }
128
129 16
                if (null !== $subResourceAnnotation->path) {
130
                    $propertyMetadata->setSubResourcePath($subResourceAnnotation->path);
131
                }
132
133 16
                if (null !== $subResourceAnnotation->postRight) {
134
                    $propertyMetadata->setSubResourcePostRight($subResourceAnnotation->postRight);
135
                }
136
            }
137
138 26
            $ddrRestClassMetadata->addPropertyMetadata($propertyMetadata);
139
        }
140
141 26
        return $ddrRestClassMetadata;
142
    }
143
}
144