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

getMethodAnnotationFromController()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 8
cp 0.875
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 2
nop 2
crap 4.0312
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