Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

SeoBundle/Twig/GoogleAnalyticsTwigExtension.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\SeoBundle\Twig;
4
5
use Kunstmaan\SeoBundle\Helper\Order,
6
    Kunstmaan\SeoBundle\Helper\OrderConverter,
7
    Kunstmaan\SeoBundle\Helper\OrderPreparer;
8
9
use Twig_Environment,
10
    Twig_Extension;
11
12
/**
13
 * Twig extensions for Google Analytics
14
 */
15
class GoogleAnalyticsTwigExtension extends Twig_Extension
16
{
17
    protected $accountVarName = 'account_id';
18
19
    protected $accountId;
20
21
    /** @var OrderPreparer */
22
    protected $orderPreparer;
23
24
    /** @var OrderConverter */
25
    protected $orderConverter;
26
27
    /**
28
     * Returns a list of functions to add to the existing list.
29
     *
30
     * @return array An array of functions
31
     */
32 View Code Duplication
    public function getFunctions()
33
    {
34
        return array(
35
            new \Twig_SimpleFunction(
36
                'google_analytics_initialize',
37
                array($this, 'renderInitialize'),
38
                array('is_safe' => array('html'), 'needs_environment' => true)
39
            ),
40
            new \Twig_SimpleFunction(
41
                'google_analytics_track_order',
42
                array($this, 'renderECommerceTracking'),
43
                array('is_safe' => array('html'), 'needs_environment' => true)
44
            ),
45
        );
46
    }
47
48
49
    /**
50
     * @param string $id The Google Analytics Account ID.
51
     */
52
    public function setAccountID($id)
53
    {
54
        $this->accountId = $id;
55
    }
56
57
    /**
58
     * @param OrderPreparer $preparer
59
     */
60
    public function setOrderPreparer($preparer)
61
    {
62
        $this->orderConverter = $preparer;
0 ignored issues
show
Documentation Bug introduced by
It seems like $preparer of type object<Kunstmaan\SeoBundle\Helper\OrderPreparer> is incompatible with the declared type object<Kunstmaan\SeoBundle\Helper\OrderConverter> of property $orderConverter.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
63
    }
64
65
    /**
66
     * @param OrderConverter $converter
67
     */
68
    public function setOrderConverter($converter)
69
    {
70
        $this->orderConverter = $converter;
71
    }
72
73
    /**
74
     * Renders the default Google Analytics JavaScript.
75
     *
76
     * If the options are not set it'll try and load the account ID from your parameters (google.analytics.account_id)
77
     *
78
     * @param $environment \Twig_Environment
79
     * @param $options     array|null        Example: {account_id: 'UA-XXXXX-Y'}
80
     *
81
     * @return string The HTML rendered.
82
     *
83
     * @throws \Twig_Error_Runtime When the Google Analytics ID is nowhere to be found.
84
     *
85
     */
86
    public function renderInitialize(\Twig_Environment $environment, $options = null)
87
    {
88
        if (is_null($options)) {
89
            $options = array();
90
        }
91
92
        $defaults = array();
93
94
        $this->setOptionIfNotSet($defaults, $this->accountVarName, $this->accountId);
95
96
        // Things set in $options will override things set in $defaults.
97
        $options = array_merge($defaults, $options);
98
99
        if (!$this->isOptionSet($options, $this->accountVarName)) {
100
            throw new \Twig_Error_Runtime(
101
                "The google_analytics_initialize function depends on a Google Analytics account ID. You can either pass this along in the initialize_google_analytics function ($this->accountVarName), provide a variable under 'parameters.google.analytics.account_id'."
102
            );
103
        }
104
105
        $template = $environment->loadTemplate('KunstmaanSeoBundle:GoogleAnalyticsTwigExtension:init.html.twig');
106
107
        return $template->render($options);
108
    }
109
110
111
    /**
112
     * @param Twig_Environment $environment
113
     * @param Order            $order
114
     *
115
     * @return string The HTML rendered.
116
     */
117
    public function renderECommerceTracking(\Twig_Environment $environment, Order $order)
118
    {
119
        $order = $this->orderPreparer->prepare($order);
120
        $options = $this->orderConverter->convert($order);
121
        $template = $environment->loadTemplate(
122
            'KunstmaanSeoBundle:GoogleAnalyticsTwigExtension:ecommerce_tracking.html.twig'
123
        );
124
125
        return $template->render($options);
126
    }
127
128
    /**
129
     * Prefer the given option if already set. Otherwise set the value given.
130
     *
131
     * @param array  &$arr   This is modified in place.
132
     * @param string $option The key in the $arr array.
133
     * @param mixed  $value  The new value if the option wasn't set already.
134
     */
135
    private function setOptionIfNotSet(&$arr, $option, $value)
136
    {
137
        if ($this->isOptionSet($arr, $option)) {
138
            $arr[$option] = $value;
139
        }
140
    }
141
142
    /**
143
     * Check if an option is set.
144
     *
145
     * @param array  $arr    The array to check.
146
     * @param string $option The key in the $arr array.
147
     *
148
     * @return bool
149
     */
150
    private function isOptionSet($arr, $option)
151
    {
152
        return (!isset($arr[$option]) || !empty($arr[$option]));
153
    }
154
}
155