Completed
Push — standalone ( 8da518...787e4c )
by Philip
09:19
created

AnnotationDriver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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