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

RouteReader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 18
c 1
b 0
f 0
dl 0
loc 40
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B getRoutes() 0 27 7
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