Completed
Push — fetcher_factories ( 10a56f...fd93ea )
by David
13:44
created

FilterUtils::getFilters()   C

Complexity

Conditions 7
Paths 24

Size

Total Lines 37
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 37
c 3
b 0
f 0
rs 6.7272
cc 7
eloc 19
nc 24
nop 2
1
<?php
2
3
namespace Mouf\Mvc\Splash\Services;
4
5
/*
6
 * Utility class for filters.
7
 */
8
use Doctrine\Common\Annotations\AnnotationReader;
9
use Mouf\Mvc\Splash\Controllers\Controller;
10
use ReflectionMethod;
11
12
class FilterUtils
13
{
14
    /**
15
     * Returns a list of filters instances, order by priority (higher priority first).
16
     * Note: a filter is an annotation that is also a middleware.
17
     *
18
     * The middleware must have a __invoke method with signature:
19
     *
20
     *     __invoke(ServerRequestInterface $request, $next, ContainerInterface $container)
21
     *
22
     * @param ReflectionMethod $refMethod  the reference method extended object.
23
     * @param AnnotationReader $reader
24
     *
25
     * @return array Array of filter instances sorted by priority.
26
     */
27
    public static function getFilters(ReflectionMethod $refMethod, AnnotationReader $reader) : array
28
    {
29
        $filterArray = array();
30
31
        $refClass = $refMethod->getDeclaringClass();
32
33
        $parentsArray = array();
34
        $parentClass = $refClass;
35
        while ($parentClass !== false) {
36
            $parentsArray[] = $parentClass;
37
            $parentClass = $parentClass->getParentClass();
38
        }
39
40
        // Start with the most parent class and goes to the target class:
41
        for ($i = count($parentsArray) - 1; $i >= 0; --$i) {
42
            $class = $parentsArray[$i];
43
            /* @var $class ReflectionClass */
44
            $annotations = $reader->getClassAnnotations($class);
45
46
            foreach ($annotations as $annotation) {
47
                if (is_callable($annotation)) {
48
                    $filterArray[] = $annotation;
49
                }
50
            }
51
        }
52
53
        // Continue with the method (and eventually override class parameters)
54
        $annotations = $reader->getMethodAnnotations($refMethod);
55
56
        foreach ($annotations as $annotation) {
57
            if (is_callable($annotation)) {
58
                $filterArray[] = $annotation;
59
            }
60
        }
61
62
        return $filterArray;
63
    }
64
}
65