Completed
Push — master ( 764508...89a649 )
by Jeroen
18:42 queued 05:38
created

MultiDomainBundle/Helper/DomainConfiguration.php (2 issues)

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