Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

AdminBundle/Twig/MultiDomainAdminTwigExtension.php (5 issues)

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\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
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Extension has been deprecated with message: since Twig 2.7, use "Twig\Extension\AbstractExtension" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
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()
28
    {
29
        return [
30
            new Twig_SimpleFunction('multidomain_widget', [$this, 'renderWidget'], ['needs_environment' => true, 'is_safe' => ['html']]),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: since Twig 2.7, use "Twig\TwigFunction" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
31
            new Twig_SimpleFunction('is_multidomain_site', [$this, 'isMultiDomainSite']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: since Twig 2.7, use "Twig\TwigFunction" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
32
            new Twig_SimpleFunction('get_switched_host', [$this, 'getSwitchedHost']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: since Twig 2.7, use "Twig\TwigFunction" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
33
            new Twig_SimpleFunction('switched_host_is_current', [$this, 'switchedHostIsCurrent']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: since Twig 2.7, use "Twig\TwigFunction" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
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