Completed
Push — 5.5 ( 8993bf...430688 )
by Jeroen
55:27 queued 49:13
created

DomainConfiguration::getRootNode()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0113

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 12
cts 13
cp 0.9231
rs 9.2248
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5.0113
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 array */
37
    private $rootNodeCache = [];
38
39
    /**
40
     * @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...
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
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
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');
0 ignored issues
show
Bug introduced by
The property em does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

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