Passed
Push — develop ( ded98c...5fca67 )
by Mathieu
02:55
created

RouteReader::getRoutes()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 27
rs 8.8333
cc 7
nc 5
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace KunicMarko\SonataAnnotationBundle\Reader;
6
7
use KunicMarko\SonataAnnotationBundle\Annotation\AddRoute;
8
use KunicMarko\SonataAnnotationBundle\Annotation\RemoveRoute;
9
use KunicMarko\SonataAnnotationBundle\Exception\MissingAnnotationArgumentException;
10
use ReflectionClass;
11
12
/**
13
 * Route configuration reader.
14
 *
15
 * @author Marko Kunic <[email protected]>
16
 * @author Mathieu Wambre <[email protected]>
17
 */
18
final class RouteReader
19
{
20
21
    use AnnotationReaderTrait;
22
23
    /**
24
     * Get admin routes.
25
     *
26
     * @param ReflectionClass $class Entity class.
27
     *
28
     * @return array<array<string, object>> Route name => route annotations,
29
     *                             added route key 0, removed routes key 1.
30
     */
31
    public function getRoutes(ReflectionClass $class): array
32
    {
33
        $addRoutes = [];
34
        $removeRoutes = [];
35
36
        foreach ($this->getClassAnnotations($class) as $annotation) {
37
            if (($annotation instanceof AddRoute
38
                || $annotation instanceof RemoveRoute)
39
              && !isset($annotation->name)) {
40
                throw new MissingAnnotationArgumentException(
41
                  $annotation,
42
                  'name',
43
                  $class
44
                );
45
            }
46
47
            if ($annotation instanceof AddRoute) {
48
                $addRoutes[$annotation->name] = $annotation;
49
                continue;
50
            }
51
52
            if ($annotation instanceof RemoveRoute) {
53
                $removeRoutes[$annotation->name] = $annotation;
54
            }
55
        }
56
57
        return [$addRoutes, $removeRoutes];
58
    }
59
60
}
61