Test Setup Failed
Push — master ( 1a9491...b3bb4f )
by Pascal
09:30
created

LazyLoader::supports()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 5
nop 2
1
<?php
2
3
namespace Skalpa\Silex\Symfony\Routing\Loader;
4
5
use Pimple\Container;
6
use Symfony\Component\Config\Loader\LoaderInterface;
7
use Symfony\Component\Config\Loader\LoaderResolverInterface;
8
use Symfony\Component\Routing\RouteCollection;
9
10
/**
11
 * Lazy routes loader.
12
 *
13
 * Delegates loading to a Pimple service instantiated lazily. This loader
14
 * can also merge routes loaded from multiple resources if an array is
15
 * passed to load().
16
 *
17
 * Arrays of resources can be:
18
 * - Associative arrays with resource path as key and resource type as value
19
 * - Numerical arrays with resource path as value
20
 */
21
class LazyLoader implements LoaderInterface
22
{
23
    private $container;
24
    private $serviceName;
25
    /**
26
     * @var LoaderInterface
27
     */
28
    private $loader;
29
30
    /**
31
     * ArrayLoader constructor.
32
     *
33
     * @param Container $container
34
     * @param string    $serviceName
35
     */
36
    public function __construct(Container $container, $serviceName)
37
    {
38
        $this->container = $container;
39
        $this->serviceName = $serviceName;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 View Code Duplication
    public function load($resource, $type = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
    {
47
        if (!is_array($resource)) {
48
            return $this->getLoader()->load($resource, $type);
49
        }
50
51
        $loader = $this->getLoader();
52
        $collection = new RouteCollection();
53
54
        foreach ($resource as $key => $value) {
55
            $collection->addCollection(is_int($key) ? $loader->load($value) : $loader->load($key, $value));
56
        }
57
58
        return $collection;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 View Code Duplication
    public function supports($resource, $type = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66
        if (!is_array($resource)) {
67
            return $this->getLoader()->supports($resource, $type);
68
        }
69
70
        $loader = $this->getLoader();
71
        $supported = true;
72
73
        foreach ($resource as $key => $value) {
74
            $supported = $supported && (is_int($key) ? $loader->supports($value) : $loader->supports($key, $value));
75
        }
76
77
        return $supported;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function getResolver()
84
    {
85
        return $this->getLoader()->getResolver();
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function setResolver(LoaderResolverInterface $resolver)
92
    {
93
        return $this->getLoader()->setResolver($resolver);
94
    }
95
96
    protected function getLoader()
97
    {
98
        if (null === $this->loader) {
99
            $this->loader = $this->container[$this->serviceName];
100
        }
101
102
        return $this->loader;
103
    }
104
}
105