DomainConfiguration   B
last analyzed

Complexity

Total Complexity 51

Size/Duplication

Total Lines 342
Duplicated Lines 23.1 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 51
lcom 1
cbo 6
dl 79
loc 342
ccs 108
cts 114
cp 0.9474
rs 7.92
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 24 5
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
     * @deprecated since KunstmaanMultiDomainBundle 5.7 and will be removed in KunstmaanMultiDomainBundle 6.0. Use the `$rootNodeCache` property instead.
20
     */
21
    protected $rootNode;
22
23
    /**
24
     * @var array
25
     */
26
    protected $hosts;
27
28
    /**
29
     * @var array
30
     */
31
    protected $aliases = array();
32
33
    /**
34
     * @var AdminRouteHelper
35
     */
36
    protected $adminRouteHelper;
37
38
    /** @var array */
39
    private $rootNodeCache = [];
40
41
    /** @var EntityManagerInterface */
42
    private $em;
43
44
    /**
45
     * @param ContainerInterface|string $multilanguage
0 ignored issues
show
Documentation introduced by
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...
46
     */
47 32
    public function __construct(/*ContainerInterface|RequestStack*/ $requestStack, $multilanguage = null, $defaultLocale = null, $requiredLocales = null, AdminRouteHelper $adminRouteHelper = null, EntityManagerInterface $em = null, array $hosts = null)
48
    {
49 32
        parent::__construct($requestStack, $multilanguage, $defaultLocale, $requiredLocales);
50
51 32
        if ($requestStack instanceof ContainerInterface) {
52
            @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
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...
53
54
            $this->container = $requestStack;
55
            $this->adminRouteHelper = $this->container->get('kunstmaan_admin.adminroute.helper');
56
            $this->hosts = $this->container->getParameter('kunstmaan_multi_domain.hosts');
0 ignored issues
show
Documentation Bug introduced by
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...
57
            $this->em = $this->container->get('doctrine.orm.entity_manager');
58
        } else {
59 32
            $this->adminRouteHelper = $adminRouteHelper;
60 32
            $this->hosts = $hosts;
0 ignored issues
show
Documentation Bug introduced by
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...
61 32
            $this->em = $em;
62
        }
63
64 32
        foreach ($this->hosts as $host => $hostInfo) {
65 32
            if (isset($hostInfo['aliases'])) {
66 32
                foreach ($hostInfo['aliases'] as $alias) {
67 32
                    $this->aliases[$alias] = $host;
68
                }
69
            }
70
        }
71 32
    }
72
73
    /**
74
     * @return string
75
     */
76 25
    public function getHost()
77
    {
78 25
        if ($this->hasHostOverride()) {
79 2
            return $this->getHostOverride();
80
        }
81
82 23
        $host = parent::getHost();
83 23
        if (isset($this->aliases[$host])) {
84 1
            $host = $this->aliases[$host];
85
        }
86
87 23
        return $host;
88
    }
89
90
    /**
91
     * @return array
0 ignored issues
show
Documentation introduced by
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...
92
     */
93 1
    public function getHosts()
94
    {
95 1
        return array_keys($this->hosts);
96
    }
97
98
    /**
99
     * @return string
100
     */
101 2 View Code Duplication
    public function getDefaultLocale()
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...
102
    {
103 2
        $host = $this->getHost();
104 2
        if (isset($this->hosts[$host]['default_locale'])) {
105 1
            return $this->hosts[$host]['default_locale'];
106
        }
107
108 1
        return parent::getDefaultLocale();
109
    }
110
111
    /**
112
     * @param string|null $host
113
     *
114
     * @return bool
115
     */
116 3 View Code Duplication
    public function isMultiLanguage($host = 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...
117
    {
118 3
        $host = $this->getRealHost($host);
119
120 3
        if (isset($this->hosts[$host])) {
121 2
            $hostInfo = $this->hosts[$host];
122
123 2
            return 'multi_lang' === $hostInfo['type'];
124
        }
125
126 1
        return parent::isMultiLanguage();
127
    }
128
129
    /**
130
     * @param string|null $host
131
     *
132
     * @return array
133
     */
134 3 View Code Duplication
    public function getFrontendLocales($host = 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...
135
    {
136 3
        $host = $this->getRealHost($host);
137
138 3
        if (isset($this->hosts[$host]['locales'])) {
139 2
            return array_keys($this->hosts[$host]['locales']);
140
        }
141
142 1
        return parent::getBackendLocales();
0 ignored issues
show
Comprehensibility Bug introduced by
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...
143
    }
144
145
    /**
146
     * @param string|null $host
147
     *
148
     * @return array
149
     */
150 3 View Code Duplication
    public function getBackendLocales($host = 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...
151
    {
152 3
        $host = $this->getRealHost($host);
153
154 3
        if (isset($this->hosts[$host]['locales'])) {
155 2
            return array_values($this->hosts[$host]['locales']);
156
        }
157
158 1
        return parent::getBackendLocales();
159
    }
160
161
    /**
162
     * @return bool
163
     */
164 5
    public function isMultiDomainHost()
165
    {
166 5
        $host = $this->getHost();
167
168 5
        return isset($this->hosts[$host]);
169
    }
170
171
    /**
172
     * Fetch the root node for the current host
173
     *
174
     * @param string|null $host
175
     *
176
     * @return Node|null
177
     */
178 2
    public function getRootNode($host = null)
179
    {
180 2
        if (!$this->isMultiDomainHost()) {
181 1
            return parent::getRootNode();
182
        }
183
184 1
        $host = $this->getRealHost($host);
185 1
        if (null === $host) {
186
            return null;
187
        }
188
189 1
        if (!array_key_exists($host, $this->rootNodeCache)) {
190 1
            $internalName = $this->hosts[$host]['root'];
191 1
            $nodeRepo = $this->em->getRepository(Node::class);
192 1
            $this->rootNodeCache[$host] = $nodeRepo->getNodeByInternalName($internalName);
193
194
            // Keep BC by setting the first node found.
195 1
            if (null === $this->rootNode) {
0 ignored issues
show
Deprecated Code introduced by
The property Kunstmaan\MultiDomainBun...onfiguration::$rootNode has been deprecated with message: since KunstmaanMultiDomainBundle 5.7 and will be removed in KunstmaanMultiDomainBundle 6.0. Use the `$rootNodeCache` property instead.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
196 1
                $this->rootNode = $this->rootNodeCache[$host];
0 ignored issues
show
Deprecated Code introduced by
The property Kunstmaan\MultiDomainBun...onfiguration::$rootNode has been deprecated with message: since KunstmaanMultiDomainBundle 5.7 and will be removed in KunstmaanMultiDomainBundle 6.0. Use the `$rootNodeCache` property instead.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
197
            }
198
        }
199
200 1
        return $this->rootNodeCache[$host];
201
    }
202
203
    /**
204
     * Return (optional) extra config settings for the current host
205
     */
206 2 View Code Duplication
    public function getExtraData()
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...
207
    {
208 2
        $host = $this->getHost();
209
210 2
        if (!isset($this->hosts[$host]['extra'])) {
211 1
            return parent::getExtraData();
212
        }
213
214 1
        return $this->hosts[$host]['extra'];
215
    }
216
217
    /**
218
     * Return (optional) extra config settings for the locales for the current host
219
     */
220 1 View Code Duplication
    public function getLocalesExtraData()
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...
221
    {
222 1
        $host = $this->getHost();
223
224 1
        if (!isset($this->hosts[$host]['locales_extra'])) {
225 1
            return parent::getLocalesExtraData();
226
        }
227
228 1
        return $this->hosts[$host]['locales_extra'];
229
    }
230
231
    /**
232
     * @return bool
233
     */
234 25 View Code Duplication
    protected function hasHostOverride()
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...
235
    {
236 25
        $request = $this->getMasterRequest();
237
238 25
        return !\is_null($request) &&
239 25
        $this->adminRouteHelper->isAdminRoute($request->getRequestUri()) &&
240 25
        $request->hasPreviousSession() &&
241 25
        $request->getSession()->has(self::OVERRIDE_HOST);
242
    }
243
244
    /**
245
     * @return bool
246
     */
247 2 View Code Duplication
    public function hasHostSwitched()
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...
248
    {
249 2
        $request = $this->getMasterRequest();
250
251 2
        return !\is_null($request) &&
252 2
        $this->adminRouteHelper->isAdminRoute($request->getRequestUri()) &&
253 2
        $request->hasPreviousSession() &&
254 2
        $request->getSession()->has(self::SWITCH_HOST);
255
    }
256
257
    /**
258
     * @return string|null
259
     */
260 3
    protected function getHostOverride()
261
    {
262 3
        if (null !== ($request = $this->getMasterRequest()) && $request->hasPreviousSession()) {
263 2
            return $request->getSession()->get(self::OVERRIDE_HOST);
264
        }
265
266 1
        return null;
267
    }
268
269
    /**
270
     * @return array
271
     */
272 1
    public function getHostSwitched()
273
    {
274 1
        $request = $this->getMasterRequest();
275
276 1
        $host = $this->getHost();
277
278 1
        if ($this->hasHostSwitched()) {
279 1
            $host = $request->getSession()->get(self::SWITCH_HOST);
280
        }
281
282 1
        return $this->hosts[$host];
283
    }
284
285
    /**
286
     * @return array
287
     */
288 1
    public function getFullHostConfig()
289
    {
290 1
        return $this->hosts;
291
    }
292
293
    /**
294
     * @param string|null $host
295
     *
296
     * @return array
297
     */
298 2
    public function getFullHost($host = null)
299
    {
300 2
        $host = $this->getRealHost($host);
301
302 2
        if ($host && isset($this->hosts[$host])) {
0 ignored issues
show
Bug Best Practice introduced by
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...
303 1
            return $this->hosts[$host];
304
        }
305
306 1
        return null;
307
    }
308
309
    /**
310
     * @param string|int $id
311
     *
312
     * @return array
313
     */
314 1
    public function getFullHostById($id)
315
    {
316 1
        foreach ($this->hosts as $host => $parameters) {
317 1
            if (!isset($parameters['id']) || $parameters['id'] !== $id) {
318 1
                continue;
319
            }
320
321 1
            return $parameters;
322
        }
323
324 1
        return null;
325
    }
326
327
    /**
328
     * @param string|null $host
329
     *
330
     * @return string
331
     */
332 1
    public function getHostBaseUrl($host = null)
333
    {
334 1
        $config = $this->getFullHost($host);
335
336 1
        return sprintf('%s://%s', $config['protocol'], $config['host']);
337
    }
338
339
    /**
340
     * @param string|null $host
341
     *
342
     * @return string|null
343
     */
344 12
    private function getRealHost($host = null)
345
    {
346 12
        if (!$host) {
0 ignored issues
show
Bug Best Practice introduced by
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...
347 10
            $host = $this->getHost();
348
        }
349
350 12
        return $host;
351
    }
352
}
353