Passed
Push — master ( 70c68f...c76078 )
by Alexander
06:58
created

RouterFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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