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

RouterFactory::__invoke()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 10
c 2
b 0
f 0
dl 0
loc 15
ccs 0
cts 14
cp 0
rs 8.8333
cc 7
nc 4
nop 1
crap 56
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