Completed
Push — standalone ( 49bbc6...8da518 )
by Philip
09:14
created

AnnotationDriver   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 59.57%

Importance

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