Collector::map()   A
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 14
ccs 10
cts 10
cp 1
rs 9.6111
cc 5
nc 9
nop 4
crap 5
1
<?php declare(strict_types=1);
2
3
namespace Phact\Router;
4
5
use FastRoute\DataGenerator;
6
use FastRoute\RouteCollector as FastRouteCollector;
7
use FastRoute\RouteParser;
8
9
class Collector extends FastRouteCollector
10
{
11
    /**
12
     * @var ReverserDataGenerator
13
     */
14
    protected $reverserDataGenerator;
15
16
    /**
17
     * @var string
18
     */
19
    protected $currentGroupName = '';
20
21
    /**
22
     * Collector constructor.
23
     * @param RouteParser $routeParser
24
     * @param DataGenerator $dataGenerator
25
     * @param ReverserDataGenerator $reverserDataGenerator
26
     */
27 30
    public function __construct(
28
        RouteParser $routeParser,
29
        DataGenerator $dataGenerator,
30
        ReverserDataGenerator $reverserDataGenerator
31
    ) {
32 30
        parent::__construct($routeParser, $dataGenerator);
33 30
        $this->reverserDataGenerator = $reverserDataGenerator;
34 30
    }
35
36
    /**
37
     * @return string
38
     */
39 12
    public function getCurrentGroupName(): string
40
    {
41 12
        return $this->currentGroupName;
42
    }
43
44
    /**
45
     * Add route with name for reversing.
46
     *
47
     * @param string|string[] $httpMethod
48
     * @param string $route
49
     * @param mixed $handler
50
     * @param string $name
51
     */
52 23
    public function map($httpMethod, $route, $handler, ?string $name = null): void
53
    {
54 23
        $route = $this->currentGroupPrefix . $route;
55 23
        $routeDatas = $this->routeParser->parse($route);
56
57 23
        foreach ((array) $httpMethod as $method) {
58 23
            foreach ($routeDatas as $routeData) {
59 23
                $this->dataGenerator->addRoute($method, $routeData, $handler);
60
            }
61
        }
62 23
        if ($name !== null) {
63 17
            $name = $this->currentGroupName . $name;
64 17
            foreach ($routeDatas as $routeData) {
65 17
                $this->reverserDataGenerator->addRoute($name, $routeData);
66
            }
67
        }
68 23
    }
69
70
    /**
71
     * Create a route group with a common prefix and name
72
     *
73
     * All routes created in the passed callback will have the given group prefix and name prepended
74
     *
75
     * @param $prefix
76
     * @param callable $callback
77
     * @param string|null $name
78
     * @param null $callbackScope
79
     */
80 14
    public function group($prefix, callable $callback, ?string $name = null, $callbackScope = null): void
81
    {
82 14
        $previousGroupName = $this->currentGroupName;
83 14
        if ($name !== null) {
84 11
            $this->currentGroupName = $previousGroupName . $name;
85
        }
86 14
        $previousGroupPrefix = $this->currentGroupPrefix;
87 14
        $this->currentGroupPrefix = $previousGroupPrefix . $prefix;
88 14
        $callback($callbackScope ?: $this);
89 14
        $this->currentGroupPrefix = $previousGroupPrefix;
90 14
        $this->currentGroupName = $previousGroupName;
91 14
    }
92
93
    /**
94
     * @return array
95
     */
96 18
    public function getReverserData(): array
97
    {
98 18
        return $this->reverserDataGenerator->getData();
99
    }
100
}
101