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

MultiDomainAdminTwigExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Kunstmaan\AdminBundle\Twig;
4
5
use Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface;
6
use Twig\Environment;
7
use Twig\Extension\AbstractExtension;
8
use Twig\TwigFunction;
9
10
/**
11
 * @final since 5.4
12
 */
13
class MultiDomainAdminTwigExtension extends AbstractExtension
14
{
15
    /**
16
     * @var DomainConfigurationInterface
17
     */
18
    private $domainConfiguration;
19
20
    public function __construct(DomainConfigurationInterface $domainConfiguration)
21
    {
22
        $this->domainConfiguration = $domainConfiguration;
23
    }
24
25
    /**
26
     * Get Twig functions defined in this extension.
27
     *
28
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use TwigFunction[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
29
     */
30 View Code Duplication
    public function getFunctions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
31
    {
32
        return [
33
            new TwigFunction('multidomain_widget', [$this, 'renderWidget'], ['needs_environment' => true, 'is_safe' => ['html']]),
34
            new TwigFunction('is_multidomain_site', [$this, 'isMultiDomainSite']),
35
            new TwigFunction('get_switched_host', [$this, 'getSwitchedHost']),
36
            new TwigFunction('switched_host_is_current', [$this, 'switchedHostIsCurrent']),
37
        ];
38
    }
39
40
    /**
41
     * Render multidomain switcher widget.
42
     *
43
     * @param string $route      The route
44
     * @param array  $parameters The route parameters
45
     *
46
     * @return string
47
     */
48
    public function renderWidget(Environment $env, $route, array $parameters = [])
49
    {
50
        $template = $env->load(
51
            '@KunstmaanAdmin/MultiDomainAdminTwigExtension/widget.html.twig'
52
        );
53
54
        return $template->render(
55
            \array_merge(
56
                $parameters, [
57
                    'hosts' => $this->getAdminDomainHosts(),
58
                    'route' => $route,
59
                ]
60
            )
61
        );
62
    }
63
64
    /**
65
     * Check if site is multiDomain
66
     *
67
     * @return bool
68
     */
69
    public function isMultiDomainSite()
70
    {
71
        return $this->domainConfiguration->isMultiDomainHost();
72
    }
73
74
    /**
75
     * @return array|null
76
     */
77
    public function getSwitchedHost()
78
    {
79
        return $this->domainConfiguration->getHostSwitched();
80
    }
81
82
    /**
83
     * @return bool
84
     */
85
    public function switchedHostIsCurrent()
86
    {
87
        $hostInfo = $this->getSwitchedHost();
88
89
        return $this->domainConfiguration->getHost() === $hostInfo['host'];
90
    }
91
92
    /**
93
     * @return array
94
     */
95
    public function getAdminDomainHosts()
96
    {
97
        return $this->domainConfiguration->getHosts();
98
    }
99
}
100