NetteRouter::testStatic()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 15
rs 10
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 Nette\Http\Request;
22
use Nette\Http\UrlScript;
23
use Nette\Routing\RouteList;
24
use Nette\Routing\Router;
25
26
class NetteRouter extends AbstractRouter
27
{
28
    public const PATH = '<world>/<method>';
29
30
    public const HOST = '<extension>';
31
32
    protected Router $router;
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function testStatic(): bool
38
    {
39
        $methods = $this->generator->getMethods();
40
41
        foreach ($methods as $method) {
42
            $path    = ($this->strategy)($method);
43
            $request = new Request(new UrlScript($path), null, null, null, null, $method);
44
            $route   = $this->router->match($request);
45
46
            if (null === $route) {
47
                return false;
48
            }
49
        }
50
51
        return true;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function testPath(): bool
58
    {
59
        $methods = $this->generator->getMethods();
60
61
        foreach ($methods as $method) {
62
            $path = ($this->strategy)($method);
63
            $request  = new Request(new UrlScript($path . 'nette' . $method), null, null, null, null, $method);
64
65
            if (null === $this->router->match($request)) {
66
                return false;
67
            }
68
        }
69
70
        return true;
71
    }
72
73
    /**
74
     * Test Sub Domain with route path
75
     *
76
     * @return bool
77
     */
78
    public function testSubDomain(): bool
79
    {
80
        $hosts = $this->generator->getHosts();
81
82
        foreach ($hosts as $host) {
83
            $methods = $this->generator->getMethods();
84
85
            foreach ($methods as $method) {
86
                $path = ($this->strategy)($method, $host);
87
                $uri      = new UrlScript($path . 'nette' . $method);
88
89
                if ($host !== '*') {
90
                    $uri = $uri->withHost($host . 'nette');
91
                }
92
93
                $request = new Request($uri, null, null, null, null, $method);
94
95
                if (null === $this->router->match($request)) {
96
                    return false;
97
                }
98
            }
99
        }
100
101
        return true;
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function buildRoutes(array $routes): void
108
    {
109
        $router = new RouteList();
110
111
        foreach ($routes as $route) {
112
            if ('*' !== $route['host']) {
113
                $router->withDomain($route['host']);
114
            }
115
116
            foreach ($route['methods'] as $method) {
117
                $router->addRoute($route['pattern'], ['world' => 'Hello', 'method' => $method]);
118
            }
119
        }
120
121
        $this->router = $router;
122
    }
123
}
124