Routes::validate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nymfonya\Component\Http;
6
7
use Nymfonya\Component\Http\Interfaces\RoutesInterface;
8
use Nymfonya\Component\Http\Route;
9
10
class Routes implements RoutesInterface
11
{
12
13
    /**
14
     * route list as array
15
     *
16
     * @var array
17
     */
18
    private $routes = [];
19
20
    /**
21
     * __construct
22
     *
23
     * @param array $routesConfig
24
     */
25 5
    public function __construct(array $routesConfig = [])
26
    {
27 5
        if (!empty($routesConfig)) {
28 5
            $this->set($routesConfig);
29
        }
30
    }
31
32
    /**
33
     * set routes as array and stack Route collection
34
     *
35
     * @param array $routesConfig
36
     * @return RoutesInterface
37
     */
38 5
    public function set(array $routesConfig): RoutesInterface
39
    {
40 5
        $this->routes = [];
41 5
        $this->prepare($routesConfig);
42 5
        $this->validate();
43 5
        return $this;
44
    }
45
46
    /**
47
     * returns routes as array
48
     *
49
     * @return array
50
     */
51 1
    public function get(): array
52
    {
53 1
        return $this->routes;
54
    }
55
56
    /**
57
     * returns routes as array
58
     *
59
     * @return array
60
     */
61 2
    public function getExpr(): array
62
    {
63 2
        $patterns = array_map(
64
            function (Route $i) {
65 2
                return $i->getExpr();
66 2
            },
67 2
            $this->routes
68
        );
69 2
        return $patterns;
70
    }
71
72
    /**
73
     * stacks routes as Route object collection from routes config
74
     *
75
     * @param array $routesConfig
76
     * @return RoutesInterface
77
     */
78 1
    protected function prepare(array $routesConfig): RoutesInterface
79
    {
80 1
        $count = count($routesConfig);
81 1
        for ($c = 0; $c < $count; $c++) {
82 1
            $this->routes[] = new Route($routesConfig[$c]);
83
        }
84 1
        return $this;
85
    }
86
87
    /**
88
     * validate routes to be an array of regexp string
89
     *
90
     * @throws Exception
91
     */
92 2
    protected function validate()
93
    {
94 2
        $count = count($this->routes);
95 2
        for ($c = 0; $c < $count; $c++) {
96 2
            $route = $this->routes[$c]->getExpr();
97 2
            if ($this->isInvalidRegexp($route)) {
98 1
                throw new \Exception('Route invalid expr ' . $route);
99
            }
100
        }
101
    }
102
103
     /**
104
      * return true if regexp is invalid
105
      *
106
      * @param string $regExp
107
      * @return boolean
108
      */
109
    protected function isInvalidRegexp(string $regExp): bool
110
    {
111
        @preg_match($regExp, '');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
112
        return (preg_last_error() != PREG_NO_ERROR);
113
    }
114
}
115