Passed
Push — master ( c0bab9...74339c )
by Divine Niiquaye
02:31
created

SunriseRouter::testStatic()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
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 16
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 Psr\Http\Message\ResponseInterface;
22
use Psr\Http\Message\ServerRequestInterface;
23
use Sunrise\Http\Message\ResponseFactory;
24
use Sunrise\Http\Router\Exception\RouteNotFoundException;
25
use Sunrise\Http\Router\RequestHandler\CallableRequestHandler;
26
use Sunrise\Http\Router\Route;
27
use Sunrise\Http\Router\Router;
28
use Sunrise\Http\ServerRequest\ServerRequestFactory;
29
30
class SunriseRouter extends AbstractRouter
31
{
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function testStatic(): bool
36
    {
37
        /** @var Router $router */
38
        list($router, $strategy) = $this->getStrategy(true);
39
40
        foreach ($this->generator->getMethods() as $method) {
41
            [, $path] = $strategy($method);
42
43
            try {
44
                $router->handle((new ServerRequestFactory())->createServerRequest($method, $path));
45
            } catch (RouteNotFoundException $e) {
46
                return false;
47
            }
48
        }
49
50
        return true;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function testPath(): bool
57
    {
58
        $this->generator->setTemplate(self::PATH, ['world' => '[^/]+']);
59
60
        /** @var Router $router */
61
        list($router, $strategy) = $this->getStrategy();
62
63
        foreach ($this->generator->getMethods() as $method) {
64
            [, $path] = $strategy($method);
65
66
            try {
67
                $router->handle((new ServerRequestFactory())->createServerRequest(
68
                    $method,
69
                    $path . 'sunrise_router'
70
                ));
71
            } catch (RouteNotFoundException $e) {
72
                return false;
73
            }
74
        }
75
76
        return true;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    protected function buildRoutes(array $routes): Router
83
    {
84
        $router = new Router();
85
86
        foreach ($routes as $route) {
87
            $pattern = $route['pattern'];
88
89
            if ('*' !== $route['host']) {
90
                $pattern = $route['host'] . $pattern;
91
            }
92
93
            $frRoute = new Route(
94
                '_' . $route['name'],
95
                $pattern,
96
                (array) $route['methods'],
97
                new CallableRequestHandler(
98
                    function (ServerRequestInterface $request): ResponseInterface {
99
                        return (new ResponseFactory())->createJsonResponse(200, [
100
                            'status' => 'ok',
101
                            'method' => $request->getMethod(),
102
                        ]);
103
                    }
104
                )
105
            );
106
107
            $router->addRoute($frRoute);
108
        }
109
110
        return $router;
111
    }
112
}
113