Completed
Push — master ( 578000...8d28bc )
by Dawid
07:57
created

ControllerAnnotationExtractorTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 52.38%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 57
ccs 11
cts 21
cp 0.5238
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
getAnnotationReader() 0 1 ?
A getClassAnnotationFromController() 0 11 2
A getMethodAnnotationFromController() 0 12 4
B getObjectToReflect() 0 10 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiechu\SymfonyCommonsBundle\Annotation\Controller;
6
7
use Doctrine\Common\Annotations\Reader;
8
9
trait ControllerAnnotationExtractorTrait
10
{
11
    abstract protected function getAnnotationReader(): Reader;
12
13
    /**
14
     * @param null|callable $controller
15
     * @param string        $annotationClass
16
     *
17
     * @return null|object
18
     */
19
    protected function getClassAnnotationFromController(callable $controller = null, string $annotationClass)
20
    {
21
        $objectToReflect = $this->getObjectToReflect($controller);
22
23
        if (is_object($objectToReflect)) {
24
            return $this->getAnnotationReader()->getClassAnnotation(
25
                new \ReflectionObject($objectToReflect),
26
                $annotationClass
27
            );
28
        }
29
    }
30
31
    /**
32
     * @param null|callable $controller
33
     * @param string        $annotationClass
34
     *
35
     * @return null|object
36
     */
37 1
    protected function getMethodAnnotationFromController(callable $controller = null, string $annotationClass)
38
    {
39 1
        if (is_array($controller)
40 1
            && isset($controller[1])
41 1
            && is_object($objectToReflect = $this->getObjectToReflect($controller))
42
        ) {
43 1
            return $this->getAnnotationReader()->getMethodAnnotation(
44 1
                new \ReflectionMethod($objectToReflect, $controller[1]),
45 1
                $annotationClass
46
            );
47
        }
48
    }
49
50
    /**
51
     * @param null|callable $controller
52
     *
53
     * @return null|object
54
     */
55 1
    protected function getObjectToReflect(callable $controller = null)
56
    {
57 1
        if (is_object($controller)) {
58
            return $controller;
59
        }
60
61 1
        if (is_array($controller) && isset($controller[0]) && is_object($controller[0])) {
62 1
            return $controller[0];
63
        }
64
    }
65
}
66