Completed
Push — master ( 53f80b...806b60 )
by Rougin
01:36
created

RoutingProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 50
ccs 15
cts 15
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A register() 0 20 3
1
<?php
2
3
namespace Zapheus\Bridge\Slytherin;
4
5
use Rougin\Slytherin\Routing\Router;
6
use Rougin\Slytherin\Routing\RouterInterface;
7
use Zapheus\Container\WritableInterface;
8
use Zapheus\Provider\ProviderInterface;
9
use Zapheus\Routing\Route;
10
use Zapheus\Routing\Router as ZapheusRouter;
11
12
/**
13
 * Routing Provider
14
 *
15
 * @package App
16
 * @author  Rougin Royce Gutib <[email protected]>
17
 */
18
class RoutingProvider implements ProviderInterface
19
{
20
    const SLYTHERIN_CONTAINER = 'Rougin\Slytherin\Container\Container';
21
22
    const SLYTHERIN_ROUTER = 'Rougin\Slytherin\Routing\RouterInterface';
23
24
    const ZAPHEUS_ROUTER = 'Zapheus\Routing\RouterInterface';
25
26
    /**
27
     * @var \Rougin\Slytherin\Routing\RouterInterface|null
28
     */
29
    protected $router;
30
31
    /**
32
     * Initializes the provider instance.
33
     *
34
     * @param \Rougin\Slytherin\Routing\RouterInterface|null $router
35
     */
36 3
    public function __construct(RouterInterface $router = null)
37
    {
38 3
        $this->router = $router;
39 3
    }
40
41
    /**
42
     * Registers the bindings in the container.
43
     *
44
     * @param  \Zapheus\Container\WritableInterface $container
45
     * @return \Zapheus\Container\ContainerInterface
46
     */
47 3
    public function register(WritableInterface $container)
48
    {
49 3
        $zapheus = new ZapheusRouter;
50
51 3
        $router = $this->router;
52
53 3
        if ($this->router === null) {
54 3
            $slytherin = $container->get(self::SLYTHERIN_CONTAINER);
55
56 3
            $router = $slytherin->get(self::SLYTHERIN_ROUTER);
57 2
        }
58
59 3
        foreach ((array) $router->routes() as $route) {
60 3
            list($method, $uri, $handler) = (array) $route;
61
62 3
            $zapheus->add(new Route($method, $uri, $handler));
63 2
        }
64
65 3
        return $container->set(self::ZAPHEUS_ROUTER, $zapheus);
66
    }
67
}
68