Completed
Push — master ( a70ca2...b8c2a5 )
by Daniel
14:22
created

MethodRouter   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 173
Duplicated Lines 9.25 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 25
c 1
b 1
f 0
lcom 1
cbo 6
dl 16
loc 173
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B setOptions() 0 26 4
A setOption() 8 8 2
A getOption() 8 8 2
A getMethodCollection() 0 8 2
A setContext() 0 8 2
A getContext() 0 4 1
A setConfigCacheFactory() 0 4 1
A warmUp() 0 10 1
A match() 0 4 1
B getMatcher() 0 31 4
A getGeneratorDumperInstance() 0 4 1
A getMatcherDumperInstance() 0 4 1
A getConfigCacheFactory() 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 Cmobi\RabbitmqBundle\Routing;
4
5
use Cmobi\RabbitmqBundle\Routing\Matcher\MethodMatcherInterface;
6
use Symfony\Component\DependencyInjection\ContainerInterface;
7
use Symfony\Component\Config\ConfigCacheInterface;
8
use Symfony\Component\Config\ConfigCacheFactoryInterface;
9
use Symfony\Component\Config\ConfigCacheFactory;
10
use Symfony\Component\Routing\RequestContext;
11
12
class MethodRouter
13
{
14
    protected $matcher;
15
    protected $context;
16
    protected $loader;
17
    protected $collection;
18
    protected $resource;
19
    protected $options = [];
20
    private $configCacheFactory;
21
22
    public function __construct(ContainerInterface $loader, $resource, array $options = [])
23
    {
24
        $this->loader = $loader;
25
        $this->resource = $resource;
26
        $this->context = new Method(null, '');
27
        $this->setOptions($options);
28
    }
29
30
    public function setOptions(array $options)
31
    {
32
        $this->options = [
33
            'cache_dir' => null,
34
            'debug' => false,
35
            'matcher_class' => 'Cmobi\\RabbitmqBundle\\Routing\\Matcher\\MethodMatcher',
36
            'matcher_dumper_class' => 'Cmobi\\RabbitmqBundle\\Routing\\Matcher\\Dumper\\PhpMatcherDumper',
37
            'matcher_cache_class' => 'ProjectMethodMatcher',
38
            'resource_type' => null,
39
            'strict_requirements' => true,
40
        ];
41
        $invalid = [];
42
43
        foreach ($options as $key => $value) {
44
45
            if (array_key_exists($key, $this->options)) {
46
                $this->options[$key] = $value;
47
            } else {
48
                $invalid[] = $key;
49
            }
50
        }
51
52
        if ($invalid) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $invalid of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
53
            throw new \InvalidArgumentException(sprintf('The Router does not support the following options: "%s".', implode('", "', $invalid)));
54
        }
55
    }
56
57 View Code Duplication
    public function setOption($key, $value)
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...
58
    {
59
        if (!array_key_exists($key, $this->options)) {
60
            throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key));
61
        }
62
63
        $this->options[$key] = $value;
64
    }
65
66 View Code Duplication
    public function getOption($key)
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...
67
    {
68
        if (!array_key_exists($key, $this->options)) {
69
            throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key));
70
        }
71
72
        return $this->options[$key];
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getMethodCollection()
79
    {
80
        if (null === $this->collection) {
81
            $this->collection = $this->loader->get('routing.loader')->load($this->resource, $this->options['resource_type']);
82
        }
83
84
        return $this->collection;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function setContext(RequestContext $context)
91
    {
92
        $this->context = $context;
93
94
        if (null !== $this->matcher) {
95
            $this->getMatcher()->setContext($context);
96
        }
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function getContext()
103
    {
104
        return $this->context;
105
    }
106
107
    public function setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory)
108
    {
109
        $this->configCacheFactory = $configCacheFactory;
110
    }
111
112
    public function warmUp($cacheDir)
113
    {
114
        $currentDir = $this->getOption('cache_dir');
115
116
        // force cache generation
117
        $this->setOption('cache_dir', $cacheDir);
118
        $this->getMatcher();
119
120
        $this->setOption('cache_dir', $currentDir);
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function match($pathinfo)
127
    {
128
        return $this->getMatcher()->match($pathinfo);
129
    }
130
131
    /**
132
     * @return MethodMatcherInterface A MethodMatcherInterface instance
133
     */
134
    public function getMatcher()
135
    {
136
        if (null !== $this->matcher) {
137
            return $this->matcher;
138
        }
139
140
        if (null === $this->options['cache_dir'] || null === $this->options['matcher_cache_class']) {
141
            $this->matcher = new $this->options['matcher_class']($this->getMethodCollection(), $this->context);
142
143
            return $this->matcher;
144
        }
145
146
        $class = $this->options['matcher_cache_class'];
147
        $that = $this;
148
149
        $cache = $this->getConfigCacheFactory()->cache($this->options['cache_dir'].'/'.$class.'.php',
150
            function (ConfigCacheInterface $cache) use ($that, $class) {
151
                $dumper = $that->getMatcherDumperInstance();
152
153
                $options = array(
154
                    'class' => $class
155
                );
156
157
                $cache->write($dumper->dump($options), $that->getMethodCollection()->getResources());
158
            }
159
        );
160
161
        require_once $cache->getPath();
162
163
        return $this->matcher = new $class($this->context);
164
    }
165
166
    public function getGeneratorDumperInstance()
167
    {
168
        return new $this->options['generator_dumper_class']($this->getMethodCollection());
169
    }
170
171
    public function getMatcherDumperInstance()
172
    {
173
        return new $this->options['matcher_dumper_class']($this->getMethodCollection());
174
    }
175
176
    private function getConfigCacheFactory()
177
    {
178
        if (null === $this->configCacheFactory) {
179
            $this->configCacheFactory = new ConfigCacheFactory($this->options['debug']);
180
        }
181
182
        return $this->configCacheFactory;
183
    }
184
}
185