RoutingProvider   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
eloc 20
c 2
b 0
f 0
dl 0
loc 78
ccs 23
cts 23
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A zapheus() 0 7 2
A slytherin() 0 14 4
A register() 0 14 2
1
<?php
2
3
namespace Zapheus\Bridge\Slytherin;
4
5
use Rougin\Slytherin\Routing\Router as SlytherinRouter;
6
use Rougin\Slytherin\Routing\RouterInterface;
7
use Zapheus\Container\ContainerInterface;
8
use Zapheus\Container\WritableInterface;
9
use Zapheus\Provider\ProviderInterface;
10
use Zapheus\Routing\Route;
11
use Zapheus\Routing\Router as ZapheusRouter;
12
13
/**
14
 * Routing Provider
15
 *
16
 * @package App
17
 * @author  Rougin Gutib <[email protected]>
18
 */
19
class RoutingProvider implements ProviderInterface
20
{
21
    const ROUTER = 'Rougin\Slytherin\Routing\RouterInterface';
22
23
    const ZAPHEUS_ROUTER = 'Zapheus\Routing\RouterInterface';
24
25
    /**
26
     * @var \Rougin\Slytherin\Routing\RouterInterface|null
27
     */
28
    protected $router;
29
30
    /**
31
     * Initializes the provider instance.
32
     *
33
     * @param \Rougin\Slytherin\Routing\RouterInterface|null $router
34
     */
35 6
    public function __construct(RouterInterface $router = null)
36
    {
37 6
        $this->router = $router;
38 6
    }
39
40
    /**
41
     * Registers the bindings in the container.
42
     *
43
     * @param  \Zapheus\Container\WritableInterface $container
44
     * @return \Zapheus\Container\ContainerInterface
45
     */
46 6
    public function register(WritableInterface $container)
47
    {
48 6
        $slytherin = $this->slytherin($container);
49
50 6
        $zapheus = $this->zapheus($container);
51
52 6
        foreach ((array) $slytherin->routes() as $route)
53
        {
54 3
            list($method, $uri, $handler) = (array) $route;
55
56 3
            $zapheus->add(new Route($method, $uri, $handler));
57 2
        }
58
59 6
        return $container->set(self::ZAPHEUS_ROUTER, $zapheus);
60
    }
61
62
    /**
63
     * Returns the Slytherin router if it does exists from container.
64
     *
65
     * @param  \Zapheus\Container\ContainerInterface $container
66
     * @return \Zapheus\Routing\RouterInterface
67
     */
68 6
    protected function slytherin(ContainerInterface $container)
69
    {
70 6
        $exists = $container->has(BridgeProvider::CONTAINER);
71
72 6
        $router = $this->router;
73
74 6
        if ($this->router === null && $exists === true)
75 2
        {
76 3
            $slytherin = $container->get(BridgeProvider::CONTAINER);
77
78 3
            return $slytherin->get((string) self::ROUTER);
79
        }
80
81 3
        return $router === null ? new SlytherinRouter : $router;
82
    }
83
84
    /**
85
     * Returns the Zapheus router if it does exists from container.
86
     *
87
     * @param  \Zapheus\Container\ContainerInterface $container
88
     * @return \Zapheus\Routing\RouterInterface
89
     */
90 6
    protected function zapheus(ContainerInterface $container)
91
    {
92 6
        $exists = $container->has((string) self::ZAPHEUS_ROUTER);
93
94 6
        $router = new ZapheusRouter;
95
96 6
        return $exists ? $container->get(self::ZAPHEUS_ROUTER) : $router;
97
    }
98
}
99