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\AdminBundle\Twig; |
||
4 | |||
5 | use Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface; |
||
6 | use Twig_Environment; |
||
7 | use Twig_Extension; |
||
8 | use Twig_SimpleFunction; |
||
9 | |||
10 | class MultiDomainAdminTwigExtension extends Twig_Extension |
||
11 | { |
||
12 | /** |
||
13 | * @var DomainConfigurationInterface |
||
14 | */ |
||
15 | private $domainConfiguration; |
||
16 | |||
17 | public function __construct(DomainConfigurationInterface $domainConfiguration) |
||
18 | { |
||
19 | $this->domainConfiguration = $domainConfiguration; |
||
20 | } |
||
21 | |||
22 | /** |
||
23 | * Get Twig functions defined in this extension. |
||
24 | * |
||
25 | * @return array |
||
26 | */ |
||
27 | View Code Duplication | public function getFunctions() |
|
0 ignored issues
–
show
|
|||
28 | { |
||
29 | return [ |
||
30 | new Twig_SimpleFunction('multidomain_widget', [$this, 'renderWidget'], ['needs_environment' => true, 'is_safe' => ['html']]), |
||
31 | new Twig_SimpleFunction('is_multidomain_site', [$this, 'isMultiDomainSite']), |
||
32 | new Twig_SimpleFunction('get_switched_host', [$this, 'getSwitchedHost']), |
||
33 | new Twig_SimpleFunction('switched_host_is_current', [$this, 'switchedHostIsCurrent']), |
||
34 | ]; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Render multidomain switcher widget. |
||
39 | * |
||
40 | * @param Twig_Environment $env |
||
41 | * @param string $route The route |
||
42 | * @param array $parameters The route parameters |
||
43 | * |
||
44 | * @return string |
||
45 | */ |
||
46 | View Code Duplication | public function renderWidget(Twig_Environment $env, $route, array $parameters = []) |
|
47 | { |
||
48 | $template = $env->loadTemplate( |
||
49 | '@KunstmaanAdmin/MultiDomainAdminTwigExtension/widget.html.twig' |
||
50 | ); |
||
51 | |||
52 | return $template->render( |
||
53 | \array_merge( |
||
54 | $parameters, [ |
||
55 | 'hosts' => $this->getAdminDomainHosts(), |
||
56 | 'route' => $route, |
||
57 | ] |
||
58 | ) |
||
59 | ); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Check if site is multiDomain |
||
64 | * |
||
65 | * @return bool |
||
66 | */ |
||
67 | public function isMultiDomainSite() |
||
68 | { |
||
69 | return $this->domainConfiguration->isMultiDomainHost(); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @return array|null |
||
74 | */ |
||
75 | public function getSwitchedHost() |
||
76 | { |
||
77 | return $this->domainConfiguration->getHostSwitched(); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @return bool |
||
82 | */ |
||
83 | public function switchedHostIsCurrent() |
||
84 | { |
||
85 | $hostInfo = $this->getSwitchedHost(); |
||
86 | |||
87 | return $this->domainConfiguration->getHost() === $hostInfo['host']; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @return array |
||
92 | */ |
||
93 | public function getAdminDomainHosts() |
||
94 | { |
||
95 | return $this->domainConfiguration->getHosts(); |
||
96 | } |
||
97 | } |
||
98 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.