Completed
Push — master ( 571b10...bdacee )
by Jeroen
21:08 queued 15:29
created

DomainConfiguration::getHostOverride()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 0
crap 3
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
    /** @var EntityManagerInterface */
40
    private $em;
41
42
    /**
43
     * @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...
44
     */
45 32
    public function __construct(/*ContainerInterface|RequestStack*/ $requestStack, $multilanguage = null, $defaultLocale = null, $requiredLocales = null, AdminRouteHelper $adminRouteHelper = null, EntityManagerInterface $em = null, array $hosts = null)
46
    {
47 32
        parent::__construct($requestStack, $multilanguage, $defaultLocale, $requiredLocales);
48
49 32
        if ($requestStack instanceof ContainerInterface) {
50
            @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...
51
52
            $this->container = $requestStack;
53
            $this->adminRouteHelper = $this->container->get('kunstmaan_admin.adminroute.helper');
54
            $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...
55
            $this->em = $this->container->get('doctrine.orm.entity_manager');
56
        } else {
57 32
            $this->adminRouteHelper = $adminRouteHelper;
58 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...
59 32
            $this->em = $em;
60
        }
61
62 32
        foreach ($this->hosts as $host => $hostInfo) {
63 32
            if (isset($hostInfo['aliases'])) {
64 32
                foreach ($hostInfo['aliases'] as $alias) {
65 32
                    $this->aliases[$alias] = $host;
66
                }
67
            }
68
        }
69 32
    }
70
71
    /**
72
     * @return string
73
     */
74 25
    public function getHost()
75
    {
76 25
        if ($this->hasHostOverride()) {
77 2
            return $this->getHostOverride();
78
        }
79
80 23
        $host = parent::getHost();
81 23
        if (isset($this->aliases[$host])) {
82 1
            $host = $this->aliases[$host];
83
        }
84
85 23
        return $host;
86
    }
87
88
    /**
89
     * @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...
90
     */
91 1
    public function getHosts()
92
    {
93 1
        return array_keys($this->hosts);
94
    }
95
96
    /**
97
     * @return string
98
     */
99 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...
100
    {
101 2
        $host = $this->getHost();
102 2
        if (isset($this->hosts[$host]['default_locale'])) {
103 1
            return $this->hosts[$host]['default_locale'];
104
        }
105
106 1
        return parent::getDefaultLocale();
107
    }
108
109
    /**
110
     * @param string|null $host
111
     *
112
     * @return bool
113
     */
114 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...
115
    {
116 3
        $host = $this->getRealHost($host);
117
118 3
        if (isset($this->hosts[$host])) {
119 2
            $hostInfo = $this->hosts[$host];
120
121 2
            return 'multi_lang' === $hostInfo['type'];
122
        }
123
124 1
        return parent::isMultiLanguage();
125
    }
126
127
    /**
128
     * @param string|null $host
129
     *
130
     * @return array
131
     */
132 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...
133
    {
134 3
        $host = $this->getRealHost($host);
135
136 3
        if (isset($this->hosts[$host]['locales'])) {
137 2
            return array_keys($this->hosts[$host]['locales']);
138
        }
139
140 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...
141
    }
142
143
    /**
144
     * @param string|null $host
145
     *
146
     * @return array
147
     */
148 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...
149
    {
150 3
        $host = $this->getRealHost($host);
151
152 3
        if (isset($this->hosts[$host]['locales'])) {
153 2
            return array_values($this->hosts[$host]['locales']);
154
        }
155
156 1
        return parent::getBackendLocales();
157
    }
158
159
    /**
160
     * @return bool
161
     */
162 5
    public function isMultiDomainHost()
163
    {
164 5
        $host = $this->getHost();
165
166 5
        return isset($this->hosts[$host]);
167
    }
168
169
    /**
170
     * Fetch the root node for the current host
171
     *
172
     * @param string|null $host
173
     *
174
     * @return Node|null
175
     */
176 2
    public function getRootNode($host = null)
177
    {
178 2
        if (!$this->isMultiDomainHost()) {
179 1
            return parent::getRootNode();
180
        }
181
182 1
        $host = $this->getRealHost($host);
183 1
        if (null === $host) {
184
            return null;
185
        }
186
187 1
        if (!array_key_exists($host, $this->rootNodeCache)) {
188 1
            $internalName = $this->hosts[$host]['root'];
189 1
            $nodeRepo = $this->em->getRepository(Node::class);
190 1
            $this->rootNodeCache[$host] = $nodeRepo->getNodeByInternalName($internalName);
191
192
            // Keep BC by setting the first node found.
193 1
            if (null === $this->rootNode) {
194 1
                $this->rootNode = $this->rootNodeCache[$host];
195
            }
196
        }
197
198 1
        return $this->rootNodeCache[$host];
199
    }
200
201
    /**
202
     * Return (optional) extra config settings for the current host
203
     */
204 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...
205
    {
206 2
        $host = $this->getHost();
207
208 2
        if (!isset($this->hosts[$host]['extra'])) {
209 1
            return parent::getExtraData();
210
        }
211
212 1
        return $this->hosts[$host]['extra'];
213
    }
214
215
    /**
216
     * Return (optional) extra config settings for the locales for the current host
217
     */
218 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...
219
    {
220 1
        $host = $this->getHost();
221
222 1
        if (!isset($this->hosts[$host]['locales_extra'])) {
223 1
            return parent::getLocalesExtraData();
224
        }
225
226 1
        return $this->hosts[$host]['locales_extra'];
227
    }
228
229
    /**
230
     * @return bool
231
     */
232 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...
233
    {
234 25
        $request = $this->getMasterRequest();
235
236 25
        return !\is_null($request) &&
237 25
        $this->adminRouteHelper->isAdminRoute($request->getRequestUri()) &&
238 25
        $request->hasPreviousSession() &&
239 25
        $request->getSession()->has(self::OVERRIDE_HOST);
240
    }
241
242
    /**
243
     * @return bool
244
     */
245 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...
246
    {
247 2
        $request = $this->getMasterRequest();
248
249 2
        return !\is_null($request) &&
250 2
        $this->adminRouteHelper->isAdminRoute($request->getRequestUri()) &&
251 2
        $request->hasPreviousSession() &&
252 2
        $request->getSession()->has(self::SWITCH_HOST);
253
    }
254
255
    /**
256
     * @return string|null
257
     */
258 3
    protected function getHostOverride()
259
    {
260 3
        if (null !== ($request = $this->getMasterRequest()) && $request->hasPreviousSession()) {
261 2
            return $request->getSession()->get(self::OVERRIDE_HOST);
262
        }
263
264 1
        return null;
265
    }
266
267
    /**
268
     * @return array
269
     */
270 1
    public function getHostSwitched()
271
    {
272 1
        $request = $this->getMasterRequest();
273
274 1
        $host = $this->getHost();
275
276 1
        if ($this->hasHostSwitched()) {
277 1
            $host = $request->getSession()->get(self::SWITCH_HOST);
278
        }
279
280 1
        return $this->hosts[$host];
281
    }
282
283
    /**
284
     * @return array
285
     */
286 1
    public function getFullHostConfig()
287
    {
288 1
        return $this->hosts;
289
    }
290
291
    /**
292
     * @param string|null $host
293
     *
294
     * @return array
295
     */
296 2
    public function getFullHost($host = null)
297
    {
298 2
        $host = $this->getRealHost($host);
299
300 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...
301 1
            return $this->hosts[$host];
302
        }
303
304 1
        return null;
305
    }
306
307
    /**
308
     * @param string|int $id
309
     *
310
     * @return array
311
     */
312 1
    public function getFullHostById($id)
313
    {
314 1
        foreach ($this->hosts as $host => $parameters) {
315 1
            if (!isset($parameters['id']) || $parameters['id'] !== $id) {
316 1
                continue;
317
            }
318
319 1
            return $parameters;
320
        }
321
322 1
        return null;
323
    }
324
325
    /**
326
     * @param string|null $host
327
     *
328
     * @return string
329
     */
330 1
    public function getHostBaseUrl($host = null)
331
    {
332 1
        $config = $this->getFullHost($host);
333
334 1
        return sprintf('%s://%s', $config['protocol'], $config['host']);
335
    }
336
337
    /**
338
     * @param string|null $host
339
     *
340
     * @return string|null
341
     */
342 12
    private function getRealHost($host = null)
343
    {
344 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...
345 10
            $host = $this->getHost();
346
        }
347
348 12
        return $host;
349
    }
350
}
351