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

LeadGenerationBundle/Twig/PopupTwigExtension.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\LeadGenerationBundle\Twig;
4
5
use Kunstmaan\LeadGenerationBundle\Entity\Popup\AbstractPopup;
6
use Kunstmaan\LeadGenerationBundle\Entity\Rule\AbstractRule;
7
use Kunstmaan\LeadGenerationBundle\Service\PopupManager;
8
use Kunstmaan\LeadGenerationBundle\Service\RuleServiceInterface;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use Twig\Environment;
11
use Twig\Extension\AbstractExtension;
12
use Twig\TwigFunction;
13
14
/**
15
 * @final since 5.4
16
 */
17
class PopupTwigExtension extends AbstractExtension
18
{
19
    /**
20
     * @var PopupManager
21
     */
22
    private $popupManager;
23
24
    /**
25
     * @var ContainerInterface
26
     */
27
    private $container;
28
29
    /**
30
     * @var array
31
     */
32
    private $popupTypes;
33
34
    /**
35
     * @var bool
36
     */
37
    private $debug;
38
39
    /**
40
     * @param bool $debug
41
     */
42
    public function __construct(PopupManager $popupManager, ContainerInterface $container, array $popupTypes, $debug)
43
    {
44
        $this->popupManager = $popupManager;
45
        $this->container = $container;
46
        $this->popupTypes = $popupTypes;
47
        $this->debug = $debug;
48
    }
49
50
    /**
51
     * Returns a list of functions to add to the existing list.
52
     *
53
     * @return array An array of functions
0 ignored issues
show
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...
54
     */
55
    public function getFunctions()
56
    {
57
        return [
58
            new TwigFunction('lead_generation_render_js_includes', [$this, 'renderJsIncludes'], ['is_safe' => ['html'], 'needs_environment' => true]),
59
            new TwigFunction('lead_generation_render_popups_html', [$this, 'renderPopupsHtml'], ['is_safe' => ['html'], 'needs_environment' => true]),
60
            new TwigFunction('lead_generation_render_initialize_js', [$this, 'renderInitializeJs'], ['is_safe' => ['html'], 'needs_environment' => true]),
61
            new TwigFunction('lead_generation_get_rule_properties', [$this, 'getRuleProperties']),
62
            new TwigFunction('get_available_popup_types', [$this, 'getAvailablePopupTypes']),
63
            new TwigFunction('get_available_rule_types', [$this, 'getAvailableRuleTypes']),
64
        ];
65
    }
66
67
    /**
68
     * @return string
69
     */
70
    public function renderJsIncludes(Environment $environment)
71
    {
72
        $files = $this->popupManager->getUniqueJsIncludes();
73
74
        return $environment->render('@KunstmaanLeadGeneration/js-includes.html.twig', ['files' => $files]);
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function renderPopupsHtml(Environment $environment)
81
    {
82
        $popups = $this->popupManager->getPopups();
83
84
        return $environment->render('@KunstmaanLeadGeneration/popups-html.html.twig', ['popups' => $popups]);
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function renderInitializeJs(Environment $environment)
91
    {
92
        $popups = $this->popupManager->getPopups();
93
94
        return $environment->render('@KunstmaanLeadGeneration/initialize-js.html.twig', ['popups' => $popups, 'debug' => $this->debug]);
95
    }
96
97
    /**
98
     * @return array
99
     */
100
    public function getRuleProperties(AbstractRule $rule)
101
    {
102
        $properties = [];
103
        if (!\is_null($rule->getService())) {
104
            $service = $this->container->get($rule->getService());
105
            if ($service instanceof RuleServiceInterface) {
106
                $properties = $service->getJsProperties($rule);
107
            }
108
        }
109
110
        return array_merge($rule->getJsProperties(), $properties);
111
    }
112
113
    /**
114
     * Get the available popup types.
115
     *
116
     * @return array
117
     */
118
    public function getAvailablePopupTypes()
119
    {
120
        $popups = [];
121
        foreach ($this->popupTypes as $popupType) {
122
            $object = new $popupType();
123
            $popups[$object->getClassname()] = $object->getFullClassname();
124
        }
125
126
        return $popups;
127
    }
128
129
    /**
130
     * Get the available popup types for a specific popup.
131
     *
132
     * @return array
133
     */
134
    public function getAvailableRuleTypes(AbstractPopup $popup)
135
    {
136
        $rulesTypes = $this->popupManager->getAvailableRules($popup);
137
138
        $rules = [];
139
        foreach ($rulesTypes as $ruleType) {
140
            $object = new $ruleType();
141
            $rules[$object->getClassname()] = $object->getFullClassname();
142
        }
143
144
        return $rules;
145
    }
146
}
147