Completed
Push — standalone ( 9d5fac...be1b50 )
by Philip
02:51
created

AnnotationDriver   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 84.85%

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 6
dl 0
loc 137
ccs 56
cts 66
cp 0.8485
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
F loadMetadataForClass() 0 118 26
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 35
    public function __construct(Reader $reader, DriverInterface $doctrineDriver)
25
    {
26 35
        $this->reader = $reader;
27 35
        $this->doctrineDriver = $doctrineDriver;
28 35
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 16
    public function loadMetadataForClass(\ReflectionClass $class)
34
    {
35
        /** @var ClassMetadata $ddrRestClassMetadata */
36 16
        $ddrRestClassMetadata = $this->doctrineDriver->loadMetadataForClass($class);
37 16
        if (null === $ddrRestClassMetadata) {
38
            $ddrRestClassMetadata = new ClassMetadata($class->getName());
39
        }
40
41
        /** @var RootResource $restResourceAnnotation */
42 16
        $restResourceAnnotation = $this->reader->getClassAnnotation($class, RootResource::class);
43 16
        if (null !== $restResourceAnnotation) {
44
45 15
            $ddrRestClassMetadata->setRestResource(true);
46
47 15
            if (null !== $restResourceAnnotation->namePrefix) {
48
                $ddrRestClassMetadata->setNamePrefix($restResourceAnnotation->namePrefix);
49
            }
50
51 15
            if (null !== $restResourceAnnotation->pathPrefix) {
52 10
                $ddrRestClassMetadata->setPathPrefix($restResourceAnnotation->pathPrefix);
53
            }
54
55 15
            if (null !== $restResourceAnnotation->service) {
56
                $ddrRestClassMetadata->setService($restResourceAnnotation->service);
57
            }
58
59 15
            if (null !== $restResourceAnnotation->controller) {
60
                $ddrRestClassMetadata->setController($restResourceAnnotation->controller);
61
            }
62
63 15
            if (null !== $restResourceAnnotation->listRight) {
64 10
                $ddrRestClassMetadata->setListRight($restResourceAnnotation->listRight);
65
            }
66
67 15
            if (null !== $restResourceAnnotation->postRight) {
68
                $ddrRestClassMetadata->setPostRight($restResourceAnnotation->postRight);
69
            }
70
71 15
            if (null !== $restResourceAnnotation->getRight) {
72 10
                $ddrRestClassMetadata->setGetRight($restResourceAnnotation->getRight);
73
            }
74
75 15
            if (null !== $restResourceAnnotation->putRight) {
76 10
                $ddrRestClassMetadata->setPutRight($restResourceAnnotation->putRight);
77
            }
78
79 15
            if (null !== $restResourceAnnotation->deleteRight) {
80
                $ddrRestClassMetadata->setDeleteRight($restResourceAnnotation->deleteRight);
81
            }
82
83 15
            if (null !== $restResourceAnnotation->methods) {
84
                $ddrRestClassMetadata->setMethods($restResourceAnnotation->methods);
85
            }
86
        }
87
88 16
        foreach ($class->getProperties() as $reflectionProperty) {
89
90 16
            $propertyMetadata = $ddrRestClassMetadata->getPropertyMetadata($reflectionProperty->getName());
91 16
            if (null === $propertyMetadata) {
92 11
                $propertyMetadata = new PropertyMetadata($class->getName(), $reflectionProperty->getName());
93
            }
94
95 16
            $puttableAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Puttable::class);
96 16
            if (null !== $puttableAnnotation) {
97 10
                $propertyMetadata->setPuttable(true);
98
            }
99
100 16
            $postableAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Postable::class);
101 16
            if (null !== $postableAnnotation) {
102
                $propertyMetadata->setPostable(true);
103
            }
104
105 16
            $includableAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Includable::class);
106 16
            if (null !== $includableAnnotation) {
107 10
                $paths = $includableAnnotation->paths;
108 10
                if (null === $paths) {
109 10
                    $paths = [$reflectionProperty->name];
110
                }
111 10
                $propertyMetadata->setIncludable(true);
112 10
                $propertyMetadata->setIncludablePaths($paths);
113
            }
114
115 16
            $excludedAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Excluded::class);
116 16
            if (null !== $excludedAnnotation) {
117 12
                $propertyMetadata->setExcluded(true);
118
            }
119
120
            /** @var SubResource $subResourceAnnotation */
121 16
            $subResourceAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, SubResource::class);
122 16
            if (null !== $subResourceAnnotation) {
123
124 10
                $propertyMetadata->setSubResource(true);
125 10
                if (null !== $subResourceAnnotation->listRight) {
126
                    $propertyMetadata->setSubResourceListRight($subResourceAnnotation->listRight);
127
                }
128
129 10
                if (null !== $subResourceAnnotation->path) {
130
                    $propertyMetadata->setSubResourcePath($subResourceAnnotation->path);
131
                }
132
133 10
                if (null !== $subResourceAnnotation->postRight) {
134 10
                    $propertyMetadata->setSubResourcePostRight($subResourceAnnotation->postRight);
135
                }
136
137 10
                if (null !== $subResourceAnnotation->putRight) {
138 10
                    $propertyMetadata->setSubResourcePutRight($subResourceAnnotation->putRight);
139
                }
140
141 10
                if (null !== $subResourceAnnotation->deleteRight) {
142 10
                    $propertyMetadata->setSubResourceDeleteRight($subResourceAnnotation->deleteRight);
143
                }
144
            }
145
146 16
            $ddrRestClassMetadata->addPropertyMetadata($propertyMetadata);
147
        }
148
149 16
        return $ddrRestClassMetadata;
150
    }
151
}
152