Completed
Push — 6.0 ( eb641c...92cb73 )
by Ruud
148:12 queued 131:31
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 Kunstmaan\AdminBundle\Helper\DomainConfiguration as BaseDomainConfiguration;
6
use Kunstmaan\NodeBundle\Entity\Node;
7
use Symfony\Component\DependencyInjection\ContainerInterface;
8
9
class DomainConfiguration extends BaseDomainConfiguration
10
{
11
    const OVERRIDE_HOST = '_override_host';
12
    const SWITCH_HOST = '_switch_host';
13
14
    /**
15
     * @var Node
16
     */
17
    protected $rootNode = null;
18
19
    /**
20
     * @var array
21
     */
22
    protected $hosts;
23
24
    /**
25
     * @var array
26
     */
27
    protected $aliases = array();
28
29
    /**
30
     * @var AdminRouteHelper
31
     */
32
    protected $adminRouteHelper;
33
34
    /**
35
     * @param ContainerInterface $container
36
     */
37
    public function __construct(ContainerInterface $container)
38
    {
39
        parent::__construct($container);
40
41
        $this->hosts = $container->getParameter('kunstmaan_multi_domain.hosts');
0 ignored issues
show
Documentation Bug introduced by
It seems like $container->getParameter...an_multi_domain.hosts') of type * is incompatible with the declared type array of property $hosts.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

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