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

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