Passed
Pull Request — master (#74)
by Anatoly
10:52
created

RouterBuilder   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 35
c 1
b 0
f 0
dl 0
loc 136
ccs 35
cts 35
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A useConfigLoader() 0 6 1
A build() 0 26 6
A setMiddlewares() 0 5 1
A setContainer() 0 5 1
A useMetadataLoader() 0 6 1
A setCache() 0 5 1
A setHosts() 0 5 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Fenric <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Fenric
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
namespace Sunrise\Http\Router;
13
14
/**
15
 * Import classes
16
 */
17
use Psr\Container\ContainerInterface;
18
use Psr\Http\Server\MiddlewareInterface;
19
use Psr\SimpleCache\CacheInterface;
20
21
/**
22
 * RouterBuilder
23
 *
24
 * @since 2.9.0
25
 */
26
final class RouterBuilder
27
{
28
29
    /**
30
     * @var null|ContainerInterface
31
     */
32
    private $container = null;
33
34
    /**
35
     * @var null|CacheInterface
36
     */
37
    private $cache = null;
38
39
    /**
40
     * @var null|array<string, string[]>
41
     */
42
    private $hosts = null;
43
44
    /**
45
     * @var null|MiddlewareInterface[]
46
     */
47
    private $middlewares = null;
48
49
    /**
50
     * @var null|Loader\CollectableFileLoader
51
     */
52
    private $configLoader = null;
53
54
    /**
55
     * @var null|Loader\DescriptorDirectoryLoader
56
     */
57
    private $metadataLoader = null;
58
59
    /**
60
     * @param null|ContainerInterface $container
61
     *
62
     * @return self
63
     */
64 1
    public function setContainer(?ContainerInterface $container) : self
65
    {
66 1
        $this->container = $container;
67
68 1
        return $this;
69
    }
70
71
    /**
72
     * @param null|CacheInterface $cache
73
     *
74
     * @return self
75
     */
76 1
    public function setCache(?CacheInterface $cache) : self
77
    {
78 1
        $this->cache = $cache;
79
80 1
        return $this;
81
    }
82
83
    /**
84
     * @param string[] $resources
85
     *
86
     * @return self
87
     */
88 1
    public function useConfigLoader(array $resources) : self
89
    {
90 1
        $this->configLoader = new Loader\CollectableFileLoader();
91 1
        $this->configLoader->attachArray($resources);
92
93 1
        return $this;
94
    }
95
96
    /**
97
     * @param string[] $resources
98
     *
99
     * @return self
100
     */
101 1
    public function useMetadataLoader(array $resources) : self
102
    {
103 1
        $this->metadataLoader = new Loader\DescriptorDirectoryLoader();
104 1
        $this->metadataLoader->attachArray($resources);
105
106 1
        return $this;
107
    }
108
109
    /**
110
     * @param null|array<string, string[]> $hosts
111
     *
112
     * @return self
113
     */
114 1
    public function setHosts(?array $hosts) : self
115
    {
116 1
        $this->hosts = $hosts;
117
118 1
        return $this;
119
    }
120
121
    /**
122
     * @param null|MiddlewareInterface[] $middlewares
123
     *
124
     * @return self
125
     */
126 1
    public function setMiddlewares(?array $middlewares) : self
127
    {
128 1
        $this->middlewares = $middlewares;
129
130 1
        return $this;
131
    }
132
133
    /**
134
     * @return Router
135
     */
136 1
    public function build() : Router
137
    {
138 1
        $router = new Router();
139
140 1
        if (isset($this->configLoader)) {
141 1
            $this->configLoader->setContainer($this->container);
0 ignored issues
show
Bug introduced by
The method setContainer() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

141
            $this->configLoader->/** @scrutinizer ignore-call */ 
142
                                 setContainer($this->container);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
142 1
            $router->load($this->configLoader);
0 ignored issues
show
Bug introduced by
It seems like $this->configLoader can also be of type null; however, parameter $loaders of Sunrise\Http\Router\Router::load() does only seem to accept Sunrise\Http\Router\Loader\LoaderInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

142
            $router->load(/** @scrutinizer ignore-type */ $this->configLoader);
Loading history...
143
        }
144
145 1
        if (isset($this->metadataLoader)) {
146 1
            $this->metadataLoader->setContainer($this->container);
0 ignored issues
show
Bug introduced by
The method setContainer() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

146
            $this->metadataLoader->/** @scrutinizer ignore-call */ 
147
                                   setContainer($this->container);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
147 1
            $this->metadataLoader->setCache($this->cache);
148 1
            $router->load($this->metadataLoader);
149
        }
150
151 1
        if (!empty($this->hosts)) {
152 1
            foreach ($this->hosts as $alias => $hostnames) {
153 1
                $router->addHost($alias, ...$hostnames);
154
            }
155
        }
156
157 1
        if (!empty($this->middlewares)) {
158 1
            $router->addMiddleware(...$this->middlewares);
159
        }
160
161 1
        return $router;
162
    }
163
}
164