AuraRouter   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 95
rs 10
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testStatic() 0 15 3
A buildRoutes() 0 17 3
A testSubDomain() 0 24 5
A testPath() 0 15 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of DivineNii opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 DivineNii (https://divinenii.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace App\BenchMark\Routers;
19
20
use App\BenchMark\AbstractRouter;
21
use Aura\Router\Matcher;
22
use Aura\Router\Route;
23
use Aura\Router\RouterContainer;
24
use Laminas\Diactoros\ServerRequest;
25
use Laminas\Diactoros\Uri;
26
27
class AuraRouter extends AbstractRouter
28
{
29
    protected Matcher $router;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function testStatic(): bool
35
    {
36
        $methods = $this->generator->getMethods();
37
38
        foreach ($methods as $method) {
39
            $path    = ($this->strategy)($method);
40
            $request = new ServerRequest([], [], $path, $method);
41
            $route   = $this->router->match($request);
42
43
            if (!$route instanceof Route) {
44
                return false;
45
            }
46
        }
47
48
        return true;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function testPath(): bool
55
    {
56
        $methods = $this->generator->getMethods();
57
58
        foreach ($methods as $method) {
59
            $path    = ($this->strategy)($method);
60
            $request = new ServerRequest([], [], $path . 'aura_router', $method);
61
            $route   = $this->router->match($request);
62
63
            if (!$route instanceof Route) {
64
                return false;
65
            }
66
        }
67
68
        return true;
69
    }
70
71
    /**
72
     * Test Sub Domain with route path
73
     *
74
     * @return bool
75
     */
76
    public function testSubDomain(): bool
77
    {
78
        $hosts = $this->generator->getHosts();
79
80
        foreach ($hosts as $host) {
81
            $methods = $this->generator->getMethods();
82
83
            foreach ($methods as $method) {
84
                $path = ($this->strategy)($method, $host);
85
                $uri  = new Uri($path . 'aura_router');
86
87
                if ($host !== '*') {
88
                    $uri = $uri->withHost($host . 'aura');
89
                }
90
91
                $request = new ServerRequest([], [], $uri, $method);
92
93
                if (!$this->router->match($request) instanceof Route) {
94
                    return false;
95
                }
96
            }
97
        }
98
99
        return true;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function buildRoutes(array $routes): void
106
    {
107
        $routerContainer = new RouterContainer();
108
        $collection      = $routerContainer->getMap();
109
110
        foreach ($routes as $route) {
111
            $auraRoute = $collection->route($route['name'], $route['pattern'], fn () => 'Hello');
112
113
            if ($route['host'] !== '*') {
114
                $auraRoute->host($route['host']);
115
            }
116
117
            $auraRoute->tokens($route['constraints']);
118
            $auraRoute->allows($route['methods']);
119
        }
120
121
        $this->router = $routerContainer->getMatcher();
122
    }
123
}
124