Completed
Push — master ( b721f8...ec0bb0 )
by Pascal
04:03
created

ArrayLoader::setResolver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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 loader that can load multiple resources.
12
 *
13
 * Array of resources can be:
14
 * - Associative arrays with resource path as key and resource type as value
15
 * - Numerical arrays with resource path as value
16
 */
17
class ArrayLoader implements LoaderInterface
18
{
19
    private $container;
20
    private $serviceName;
21
    /**
22
     * @var LoaderInterface
23
     */
24
    private $loader;
25
26
    /**
27
     * ArrayLoader constructor.
28
     *
29
     * @param Container $container
30
     * @param string    $serviceName
31
     */
32 27
    public function __construct(Container $container, $serviceName)
33
    {
34 27
        $this->container = $container;
35 27
        $this->serviceName = $serviceName;
36 27
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 20 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...
42
    {
43 20
        if (!is_array($resource)) {
44 19
            return $this->getLoader()->load($resource, $type);
45
        }
46
47 1
        $loader = $this->getLoader();
48 1
        $collection = new RouteCollection();
49
50 1
        foreach ($resource as $key => $value) {
51 1
            $collection->addCollection(is_int($key) ? $loader->load($value) : $loader->load($key, $value));
52 1
        }
53
54 1
        return $collection;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 3 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...
61
    {
62 3
        if (!is_array($resource)) {
63 1
            return $this->getLoader()->supports($resource, $type);
64
        }
65
66 2
        $loader = $this->getLoader();
67 2
        $supported = true;
68
69 2
        foreach ($resource as $key => $value) {
70 2
            $supported = $supported && (is_int($key) ? $loader->supports($value) : $loader->supports($key, $value));
71 2
        }
72
73 2
        return $supported;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 1
    public function getResolver()
80
    {
81 1
        return $this->getLoader()->getResolver();
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 1
    public function setResolver(LoaderResolverInterface $resolver)
88
    {
89 1
        return $this->getLoader()->setResolver($resolver);
90
    }
91
92 25
    protected function getLoader()
93
    {
94 25
        if (null === $this->loader) {
95 25
            $this->loader = $this->container[$this->serviceName];
96 25
        }
97
98 25
        return $this->loader;
99
    }
100
}
101