Completed
Push — master ( 88d4e7...879ddc )
by Pierre
17:36 queued 14:17
created

Routes   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 58
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 7 3
A set() 0 5 1
A get() 0 3 1
A __construct() 0 6 2
1
<?php
2
3
namespace App\Component\Http;
4
5
use App\Component\Http\Interfaces\IRoutes;
6
7
class Routes implements IRoutes
8
{
9
    /**
10
     * route list as array
11
     *
12
     * @var array
13
     */
14
    private $routes = [];
15
16
    /**
17
     * __construct
18
     *
19
     * @param array $routes
20
     * @return Routes
21
     */
22 4
    public function __construct(array $routes = [])
23
    {
24 4
        if (!empty($routes)) {
25 4
            $this->set($routes);
26
        }
27 4
        return $this;
28
    }
29
30
    /**
31
     * returns routes as array
32
     *
33
     * @return array
34
     */
35 2
    public function get(): array
36
    {
37 2
        return $this->routes;
38
    }
39
40
    /**
41
     * set routes as array and returns Routes
42
     *
43
     * @param array $routes
44
     * @return Routes
45
     */
46 4
    public function set(array $routes): Routes
47
    {
48 4
        $this->routes = $routes;
49 4
        $this->validate();
50 4
        return $this;
51
    }
52
53
    /**
54
     * validate routes to be an array of regexp string
55
     *
56
     * @throws Exception
57
     */
58 2
    protected function validate()
59
    {
60 2
        $count = count($this->routes);
61 2
        for ($c = 0; $c < $count; $c++) {
62 2
            $route = $this->routes[$c];
63 2
            if (@preg_match($route, null) === false) {
64 1
                throw new \Exception('Route invalid ' . $route);
65
            }
66
        }
67
    }
68
}
69