Completed
Push — master ( 806b60...d34afe )
by Rougin
01:48
created

RoutingProvider::zapheus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 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 Royce Gutib <[email protected]>
18
 */
19
class RoutingProvider implements ProviderInterface
20
{
21
    const SLYTHERIN_CONTAINER = 'Rougin\Slytherin\Container\Container';
22
23
    const SLYTHERIN_ROUTER = 'Rougin\Slytherin\Routing\RouterInterface';
24
25
    const ZAPHEUS_ROUTER = 'Zapheus\Routing\RouterInterface';
26
27
    /**
28
     * @var \Rougin\Slytherin\Routing\RouterInterface|null
29
     */
30
    protected $router;
31
32
    /**
33
     * Initializes the provider instance.
34
     *
35
     * @param \Rougin\Slytherin\Routing\RouterInterface|null $router
36
     */
37 6
    public function __construct(RouterInterface $router = null)
38
    {
39 6
        $this->router = $router;
40 6
    }
41
42
    /**
43
     * Registers the bindings in the container.
44
     *
45
     * @param  \Zapheus\Container\WritableInterface $container
46
     * @return \Zapheus\Container\ContainerInterface
47
     */
48 6
    public function register(WritableInterface $container)
49
    {
50 6
        $slytherin = $this->slytherin($container);
51
52 6
        $zapheus = $this->zapheus($container);
53
54 6
        foreach ((array) $slytherin->routes() as $route) {
55 3
            list($method, $uri, $handler) = (array) $route;
56
57 3
            $zapheus->add(new Route($method, $uri, $handler));
58 4
        }
59
60 6
        return $container->set(self::ZAPHEUS_ROUTER, $zapheus);
61
    }
62
63
    /**
64
     * Returns the Slytherin router if it does exists from container.
65
     *
66
     * @param  \Zapheus\Container\ContainerInterface $container
67
     * @return \Zapheus\Routing\RouterInterface
68
     */
69 6
    protected function slytherin(ContainerInterface $container)
70
    {
71 6
        $exists = $container->has(self::SLYTHERIN_CONTAINER);
72
73 6
        $router = $this->router;
74
75 6
        if ($this->router === null && $exists === true) {
76 3
            $slytherin = $container->get(self::SLYTHERIN_CONTAINER);
77
78 3
            return $slytherin->get((string) self::SLYTHERIN_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