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

LazyLoader   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 84
Duplicated Lines 35.71 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 30
loc 84
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A load() 15 15 4
B supports() 15 15 5
A getResolver() 0 4 1
A setResolver() 0 4 1
A getLoader() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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