PeceeRouter::buildRoutes()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 11
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 Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
22
use Pecee\SimpleRouter\SimpleRouter;
23
24
class PeceeRouter extends AbstractRouter
25
{
26
    protected SimpleRouter $router;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function testStatic(): bool
32
    {
33
        $methods = $this->generator->getMethods();
34
35
        foreach ($methods as $method) {
36
            $path = ($this->strategy)($method);
37
38
            try {
39
                $request = $this->router::request();
40
                $request->setUrl((new \Pecee\Http\Url($path)));
41
                $request->setMethod(\strtolower($method));
42
43
                \ob_start();
44
                $this->router->start();
45
                \ob_get_clean();
46
            } catch (NotFoundHttpException $e) {
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
64
            try {
65
                $request = $this->router::request();
66
                $request->setUrl((new \Pecee\Http\Url($path . 'pecee_router')));
67
                $request->setMethod(\strtolower($method));
68
69
                \ob_start();
70
                $this->router->start();
71
                \ob_get_clean();
72
            } catch (NotFoundHttpException $e) {
73
                return false;
74
            }
75
        }
76
77
        return true;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function buildRoutes(array $routes): void
84
    {
85
        $router = new SimpleRouter();
86
87
        foreach ($routes as $route) {
88
            foreach ($route['methods'] as $method) {
89
                $router->match([\strtolower($method)], $route['pattern'], fn () => '');
90
            }
91
        }
92
93
        $this->router = $router;
94
    }
95
}
96