Test Failed
Push — master ( d89333...e2d202 )
by Alexey
03:26
created

RouteGroup   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 93
ccs 0
cts 40
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A collect() 0 7 1
A setHost() 0 6 1
A setPrefix() 0 6 1
A setScheme() 0 6 1
A addPathPrefix() 0 5 3
B all() 0 16 6
1
<?php declare(strict_types = 1);
2
3
namespace Venta\Routing;
4
5
use Venta\Contracts\Routing\RouteGroup as RouteGroupContract;
6
7
/**
8
 * Class RouteGroup
9
 *
10
 * @package Venta\Routing
11
 */
12
class RouteGroup extends RouteCollection implements RouteGroupContract
13
{
14
15
    /**
16
     * @var string
17
     */
18
    private $host = '';
19
20
    /**
21
     * @var string
22
     */
23
    private $prefix = '/';
24
25
    /**
26
     * @var string
27
     */
28
    private $scheme = '';
29
30
    /**
31
     * @param callable $callback
32
     * @return RouteGroupContract
33
     */
34
    public static function collect(callable $callback): RouteGroupContract
35
    {
36
        $group = new static();
37
        $callback($group);
38
39
        return $group;
40
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45
    public function all(): array
46
    {
47
        $routes = [];
48
        /** @var Route $route */
49
        foreach (parent::all() as $route) {
50
            if (!$route->host() && $this->host) {
51
                $route = $route->withHost($this->host);
52
            }
53
            if (!$route->scheme() && $this->scheme) {
54
                $route = $route->secure();
55
            }
56
            $routes[] = $route->withPath($this->addPathPrefix($route->path()));
57
        }
58
59
        return $routes;
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function setHost(string $host): RouteGroupContract
66
    {
67
        $this->host = $host;
68
69
        return $this;
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75
    public function setPrefix(string $prefix): RouteGroupContract
76
    {
77
        $this->prefix = '/' . trim($prefix, '/');
78
79
        return $this;
80
    }
81
82
    /**
83
     * @inheritDoc
84
     */
85
    public function setScheme(string $scheme): RouteGroupContract
86
    {
87
        $this->scheme = $scheme;
88
89
        return $this;
90
    }
91
92
    /**
93
     * Prepends group prefix to provided route $path.
94
     *
95
     * @param string $path
96
     * @return string
97
     */
98
    private function addPathPrefix(string $path): string
99
    {
100
        return $this->prefix == '/' || $this->prefix == '' ? $path :
101
            sprintf('/%s/%s', trim($this->prefix, '/'), ltrim($path, '/'));
102
    }
103
104
}