RoutingLoader::supports()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright (c) 2017
4
 *
5
 * @package   Majima
6
 * @author    David Neustadt <[email protected]>
7
 * @copyright 2017 David Neustadt
8
 * @license   MIT
9
 */
10
11
namespace Majima\Services;
12
13
use Symfony\Component\Config\Loader\LoaderInterface;
14
use Symfony\Component\Config\Loader\LoaderResolverInterface;
15
use Symfony\Component\DependencyInjection\Container;
16
use Symfony\Component\Routing\Route;
17
use Symfony\Component\Routing\RouteCollection;
18
19
/**
20
 * Class RoutingLoader
21
 * @package Majima\Services
22
 */
23
class RoutingLoader implements LoaderInterface
24
{
25
    /**
26
     * @var bool
27
     */
28
    private $loaded = false;
29
30
    /**
31
     * @var Container
32
     */
33
    private $container;
34
35
    /**
36
     * @var RoutesService
37
     */
38
    private $routesService;
39
40
    public function __construct(Container $container, RoutesService $routesService)
41
    {
42
        $this->container = $container;
43
        $this->routesService = $routesService;
44
    }
45
46
    /**
47
     * @param mixed $resource
48
     * @param string|null $type
49
     * @return RouteCollection
50
     */
51
    public function load($resource, $type = null)
52
    {
53
        if (true === $this->loaded) {
54
            throw new \RuntimeException('Do not add the "extra" loader twice');
55
        }
56
57
        $collection = new RouteCollection();
58
59
        $routes = $this->container->get('majima.routes')->getRoutes();
60
61
        foreach ($routes as $route) {
62
            $collection->add(
63
                $route['name'],
64
                new Route(
65
                    $route['slug'],
66
                    $route['defaults'],
67
                    $route['params']
68
                )
69
            );
70
        }
71
72
        return $collection;
73
    }
74
75
    /**
76
     * @param mixed $resource
77
     * @param string|null $type
78
     * @return bool
79
     */
80
    public function supports($resource, $type = null)
81
    {
82
        return 'majima' === $type;
83
    }
84
85
    public function getResolver()
86
    {
87
        // needed, but can be blank, unless you want to load other resources
88
        // and if you do, using the Loader base class is easier (see below)
89
    }
90
91
    /**
92
     * @param LoaderResolverInterface $resolver
93
     */
94
    public function setResolver(LoaderResolverInterface $resolver)
95
    {
96
        // same as above
97
    }
98
}