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