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

RouterFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
eloc 17
c 3
b 0
f 0
dl 0
loc 35
ccs 14
cts 15
cp 0.9333
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 18 5
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