Passed
Push — master ( 21057b...383a4e )
by Alexander
01:22
created

RouterFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Yiisoft\Router;
4
5
use Psr\Container\ContainerInterface;
6
7
final class RouterFactory
8
{
9
    private $engineFactory;
10
    private $routes;
11
12
    public function __construct(callable $engineFactory, $routes = [])
13
    {
14
        $this->engineFactory = $engineFactory;
15
        $this->routes = $routes;
16
    }
17
18
    public function __invoke(ContainerInterface $container): RouterInterface
19
    {
20
        $factory = $this->engineFactory;
21
        /* @var $router RouterInterface */
22
        $router = $factory($container);
23
        foreach ($this->routes as $route) {
24
            if ($route instanceof Route) {
25
                $router->addRoute($route);
26
            } elseif (\is_array($route) && \count($route) === 2 && \is_string($route[0]) && \is_callable($route[1])) {
27
                $router->addGroup($route[0], $route[1]);
28
            } else {
29
                throw new \InvalidArgumentException('Routes should be eithe instances of Route or group arrays');
30
            }
31
        }
32
        return $router;
33
    }
34
}
35