Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

DomainConfiguration   B

Complexity

Total Complexity 49

Size/Duplication

Total Lines 329
Duplicated Lines 24.01 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 49
lcom 1
cbo 6
dl 79
loc 329
ccs 105
cts 110
cp 0.9545
rs 8.48
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 25 5
A getHost() 0 13 3
A getHosts() 0 4 1
A getDefaultLocale() 9 9 2
A isMultiLanguage() 12 12 2
A getFrontendLocales() 10 10 2
A getBackendLocales() 10 10 2
A isMultiDomainHost() 0 6 1
A getRootNode() 0 16 3
A getExtraData() 10 10 2
A getLocalesExtraData() 10 10 2
A hasHostOverride() 9 9 4
A hasHostSwitched() 9 9 4
A getHostOverride() 0 8 3
A getHostSwitched() 0 12 2
A getFullHostConfig() 0 4 1
A getFullHost() 0 10 3
A getFullHostById() 0 12 4
A getHostBaseUrl() 0 6 1
A getRealHost() 0 8 2

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like DomainConfiguration often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use DomainConfiguration, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Kunstmaan\MultiDomainBundle\Helper;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Kunstmaan\AdminBundle\Helper\AdminRouteHelper;
7
use Kunstmaan\AdminBundle\Helper\DomainConfiguration as BaseDomainConfiguration;
8
use Kunstmaan\NodeBundle\Entity\Node;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
11
class DomainConfiguration extends BaseDomainConfiguration
12
{
13
    const OVERRIDE_HOST = '_override_host';
14
    const SWITCH_HOST = '_switch_host';
15
16
    /**
17
     * @var Node
18
     */
19
    protected $rootNode;
20
21
    /**
22
     * @var array
23
     */
24
    protected $hosts;
25
26
    /**
27
     * @var array
28
     */
29
    protected $aliases = array();
30
31
    /**
32
     * @var AdminRouteHelper
33
     */
34
    protected $adminRouteHelper;
35
36
    /** @var EntityManagerInterface */
37
    private $em;
38
39
    /**
40
     * @param ContainerInterface|string $multilanguage
0 ignored issues
show
Documentation introduced by Peter Zentjens
Should the type for parameter $multilanguage not be ContainerInterface|string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
41
     */
42 32
    public function __construct(/*ContainerInterface|RequestStack*/ $requestStack, $multilanguage = null, $defaultLocale = null, $requiredLocales = null, AdminRouteHelper $adminRouteHelper = null, EntityManagerInterface $em = null, array $hosts = null)
43
    {
44 32
        parent::__construct($requestStack, $multilanguage, $defaultLocale, $requiredLocales);
45
46 32
        if ($requestStack instanceof ContainerInterface) {
47
            @trigger_error('Container injection and the usage of the container is deprecated in KunstmaanNodeBundle 5.1 and will be removed in KunstmaanNodeBundle 6.0.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by Peter Zentjens
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
48
49
            $this->container = $requestStack;
50
            $this->adminRouteHelper = $this->container->get('kunstmaan_admin.adminroute.helper');
51
            $this->hosts = $this->container->getParameter('kunstmaan_multi_domain.hosts');
0 ignored issues
show
Documentation Bug introduced by Peter Zentjens
It seems like $this->container->getPar...an_multi_domain.hosts') of type * is incompatible with the declared type array of property $hosts.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
52
            $this->em = $this->container->get('doctrine.orm.entity_manager');
53
        } else {
54 32
            $this->adminRouteHelper = $adminRouteHelper;
55 32
            $this->hosts = $hosts;
0 ignored issues
show
Documentation Bug introduced by Peter Zentjens
It seems like $hosts can be null. However, the property $hosts is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
56 32
            $this->em = $em;
57
        }
58
59 32
        foreach ($this->hosts as $host => $hostInfo) {
60 32
            if (isset($hostInfo['aliases'])) {
61 32
                foreach ($hostInfo['aliases'] as $alias) {
62 32
                    $this->aliases[$alias] = $host;
63
                }
64
            }
65
        }
66 32
    }
67
68
    /**
69
     * @return string
70
     */
71 25
    public function getHost()
72
    {
73 25
        if ($this->hasHostOverride()) {
74 2
            return $this->getHostOverride();
75
        }
76
77 23
        $host = parent::getHost();
78 23
        if (isset($this->aliases[$host])) {
79 1
            $host = $this->aliases[$host];
80
        }
81
82 23
        return $host;
83
    }
84
85
    /**
86
     * @return array
0 ignored issues
show
Documentation introduced by Wim Vandersmissen
Consider making the return type a bit more specific; maybe use array<integer|string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
87
     */
88 1
    public function getHosts()
89
    {
90 1
        return array_keys($this->hosts);
91
    }
92
93
    /**
94
     * @return string
95
     */
96 2 View Code Duplication
    public function getDefaultLocale()
0 ignored issues
show
Duplication introduced by Wim Vandersmissen
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...
97
    {
98 2
        $host = $this->getHost();
99 2
        if (isset($this->hosts[$host]['default_locale'])) {
100 1
            return $this->hosts[$host]['default_locale'];
101
        }
102
103 1
        return parent::getDefaultLocale();
104
    }
105
106
    /**
107
     * @param string|null $host
108
     *
109
     * @return bool
110
     */
111 3 View Code Duplication
    public function isMultiLanguage($host = null)
0 ignored issues
show
Duplication introduced by Sander Goossens
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...
112
    {
113 3
        $host = $this->getRealHost($host);
114
115 3
        if (isset($this->hosts[$host])) {
116 2
            $hostInfo = $this->hosts[$host];
117
118 2
            return 'multi_lang' === $hostInfo['type'];
119
        }
120
121 1
        return parent::isMultiLanguage();
122
    }
123
124
    /**
125
     * @param string|null $host
126
     *
127
     * @return array
128
     */
129 3 View Code Duplication
    public function getFrontendLocales($host = null)
0 ignored issues
show
Duplication introduced by Sander Goossens
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...
130
    {
131 3
        $host = $this->getRealHost($host);
132
133 3
        if (isset($this->hosts[$host]['locales'])) {
134 2
            return array_keys($this->hosts[$host]['locales']);
135
        }
136
137 1
        return parent::getBackendLocales();
0 ignored issues
show
Comprehensibility Bug introduced by Wim Vandersmissen
It seems like you call parent on a different method (getBackendLocales() instead of getFrontendLocales()). Are you sure this is correct? If so, you might want to change this to $this->getBackendLocales().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
138
    }
139
140
    /**
141
     * @param string|null $host
142
     *
143
     * @return array
144
     */
145 3 View Code Duplication
    public function getBackendLocales($host = null)
0 ignored issues
show
Duplication introduced by Sander Goossens
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...
146
    {
147 3
        $host = $this->getRealHost($host);
148
149 3
        if (isset($this->hosts[$host]['locales'])) {
150 2
            return array_values($this->hosts[$host]['locales']);
151
        }
152
153 1
        return parent::getBackendLocales();
154
    }
155
156
    /**
157
     * @return bool
158
     */
159 5
    public function isMultiDomainHost()
160
    {
161 5
        $host = $this->getHost();
162
163 5
        return isset($this->hosts[$host]);
164
    }
165
166
    /**
167
     * Fetch the root node for the current host
168
     *
169
     * @param string|null $host
170
     *
171
     * @return Node|null
172
     */
173 2
    public function getRootNode($host = null)
174
    {
175 2
        if (!$this->isMultiDomainHost()) {
176 1
            return parent::getRootNode();
177
        }
178
179 1
        if (\is_null($this->rootNode)) {
180 1
            $host = $this->getRealHost($host);
181
182 1
            $internalName = $this->hosts[$host]['root'];
183 1
            $nodeRepo = $this->em->getRepository(Node::class);
184 1
            $this->rootNode = $nodeRepo->getNodeByInternalName($internalName);
185
        }
186
187 1
        return $this->rootNode;
188
    }
189
190
    /**
191
     * Return (optional) extra config settings for the current host
192
     */
193 2 View Code Duplication
    public function getExtraData()
0 ignored issues
show
Duplication introduced by Wim Vandersmissen
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...
194
    {
195 2
        $host = $this->getHost();
196
197 2
        if (!isset($this->hosts[$host]['extra'])) {
198 1
            return parent::getExtraData();
199
        }
200
201 1
        return $this->hosts[$host]['extra'];
202
    }
203
204
    /**
205
     * Return (optional) extra config settings for the locales for the current host
206
     */
207 1 View Code Duplication
    public function getLocalesExtraData()
0 ignored issues
show
Duplication introduced by Kristof Jochmans
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...
208
    {
209 1
        $host = $this->getHost();
210
211 1
        if (!isset($this->hosts[$host]['locales_extra'])) {
212 1
            return parent::getLocalesExtraData();
213
        }
214
215 1
        return $this->hosts[$host]['locales_extra'];
216
    }
217
218
    /**
219
     * @return bool
220
     */
221 25 View Code Duplication
    protected function hasHostOverride()
0 ignored issues
show
Duplication introduced by Wim Vandersmissen
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...
222
    {
223 25
        $request = $this->getMasterRequest();
224
225 25
        return !\is_null($request) &&
226 25
        $this->adminRouteHelper->isAdminRoute($request->getRequestUri()) &&
227 25
        $request->hasPreviousSession() &&
228 25
        $request->getSession()->has(self::OVERRIDE_HOST);
229
    }
230
231
    /**
232
     * @return bool
233
     */
234 2 View Code Duplication
    public function hasHostSwitched()
0 ignored issues
show
Duplication introduced by Sander Goossens
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...
235
    {
236 2
        $request = $this->getMasterRequest();
237
238 2
        return !\is_null($request) &&
239 2
        $this->adminRouteHelper->isAdminRoute($request->getRequestUri()) &&
240 2
        $request->hasPreviousSession() &&
241 2
        $request->getSession()->has(self::SWITCH_HOST);
242
    }
243
244
    /**
245
     * @return string|null
246
     */
247 3
    protected function getHostOverride()
248
    {
249 3
        if (null !== ($request = $this->getMasterRequest()) && $request->hasPreviousSession()) {
250 2
            return $request->getSession()->get(self::OVERRIDE_HOST);
251
        }
252
253 1
        return null;
254
    }
255
256
    /**
257
     * @return array
258
     */
259 1
    public function getHostSwitched()
260
    {
261 1
        $request = $this->getMasterRequest();
262
263 1
        $host = $this->getHost();
264
265 1
        if ($this->hasHostSwitched()) {
266 1
            $host = $request->getSession()->get(self::SWITCH_HOST);
267
        }
268
269 1
        return $this->hosts[$host];
270
    }
271
272
    /**
273
     * @return array
274
     */
275 1
    public function getFullHostConfig()
276
    {
277 1
        return $this->hosts;
278
    }
279
280
    /**
281
     * @param string|null $host
282
     *
283
     * @return array
284
     */
285 2
    public function getFullHost($host = null)
286
    {
287 2
        $host = $this->getRealHost($host);
288
289 2
        if ($host && isset($this->hosts[$host])) {
0 ignored issues
show
Bug Best Practice introduced by Ruud Denivel
The expression $host of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
290 1
            return $this->hosts[$host];
291
        }
292
293 1
        return null;
294
    }
295
296
    /**
297
     * @param string|int $id
298
     *
299
     * @return array
300
     */
301 1
    public function getFullHostById($id)
302
    {
303 1
        foreach ($this->hosts as $host => $parameters) {
304 1
            if (!isset($parameters['id']) || $parameters['id'] !== $id) {
305 1
                continue;
306
            }
307
308 1
            return $parameters;
309
        }
310
311 1
        return null;
312
    }
313
314
    /**
315
     * @param string|null $host
316
     *
317
     * @return string
318
     */
319 1
    public function getHostBaseUrl($host = null)
320
    {
321 1
        $config = $this->getFullHost($host);
322
323 1
        return sprintf('%s://%s', $config['protocol'], $config['host']);
324
    }
325
326
    /**
327
     * @param string|null $host
328
     *
329
     * @return string|null
330
     */
331 12
    private function getRealHost($host = null)
332
    {
333 12
        if (!$host) {
0 ignored issues
show
Bug Best Practice introduced by Sander Goossens
The expression $host of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
334 10
            $host = $this->getHost();
335
        }
336
337 12
        return $host;
338
    }
339
}
340