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