Completed
Push — 6.0 ( eb641c...92cb73 )
by Ruud
148:12 queued 131:31
created

DomainConfiguration::__construct()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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