Completed
Push — master ( 2c9995...0e68f4 )
by Dawid
02:01
created

ControllerAnnotationExtractorTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 80.95%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
getAnnotationReader() 0 1 ?
A getMethodAnnotationFromController() 0 12 4
B getObjectToReflect() 0 10 5
A getClassAnnotationFromController() 0 11 2
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 2
    protected function getClassAnnotationFromController(?callable $controller, string $annotationClass)
20
    {
21 2
        $objectToReflect = $this->getObjectToReflect($controller);
22
23 2
        if (is_object($objectToReflect)) {
24 2
            return $this->getAnnotationReader()->getClassAnnotation(
25 2
                new \ReflectionObject($objectToReflect),
26 2
                $annotationClass
27
            );
28
        }
29
    }
30
31
    /**
32
     * @param null|callable $controller
33
     * @param string        $annotationClass
34
     *
35
     * @return null|object
36
     */
37 2
    protected function getMethodAnnotationFromController(?callable $controller, string $annotationClass)
38
    {
39 2
        if (is_array($controller)
40 2
            && isset($controller[1])
41 2
            && is_object($objectToReflect = $this->getObjectToReflect($controller))
42
        ) {
43 2
            return $this->getAnnotationReader()->getMethodAnnotation(
44 2
                new \ReflectionMethod($objectToReflect, $controller[1]),
45 2
                $annotationClass
46
            );
47
        }
48
    }
49
50
    /**
51
     * @param null|callable $controller
52
     *
53
     * @return null|object
54
     */
55 2
    protected function getObjectToReflect(?callable $controller)
56
    {
57 2
        if (is_object($controller)) {
58
            return $controller;
59
        }
60
61 2
        if (is_array($controller) && isset($controller[0]) && is_object($controller[0])) {
62 2
            return $controller[0];
63
        }
64
    }
65
}
66