Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

MultiDomainBundle/Helper/DomainConfiguration.php (1 issue)

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 = [];
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
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);
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');
55
            $this->em = $this->container->get('doctrine.orm.entity_manager');
56
        } else {
57 32
            $this->adminRouteHelper = $adminRouteHelper;
58 32
            $this->hosts = $hosts;
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
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()
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)
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)
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();
141
    }
142
143
    /**
144
     * @param string|null $host
145
     *
146
     * @return array
147
     */
148 3 View Code Duplication
    public function getBackendLocales($host = null)
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()
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()
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()
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()
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])) {
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) {
345 10
            $host = $this->getHost();
346
        }
347
348 12
        return $host;
349
    }
350
}
351