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

MultiDomainBundle/Helper/DomainConfiguration.php (1 issue)

Check for loose comparison of strings.

Best Practice Bug Major

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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);
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');
52
            $this->em = $this->container->get('doctrine.orm.entity_manager');
53
        } else {
54 32
            $this->adminRouteHelper = $adminRouteHelper;
55 32
            $this->hosts = $hosts;
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
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()
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)
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)
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();
138
    }
139
140
    /**
141
     * @param string|null $host
142
     *
143
     * @return array
144
     */
145 3 View Code Duplication
    public function getBackendLocales($host = null)
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()
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()
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()
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()
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) {
334 10
            $host = $this->getHost();
335
        }
336
337 12
        return $host;
338
    }
339
}
340