Completed
Push — master ( 770316...74fc07 )
by Jeroen
09:08 queued 02:44
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
use Kunstmaan\SeoBundle\Helper\OrderConverter;
7
use Kunstmaan\SeoBundle\Helper\OrderPreparer;
8
use Twig_Environment;
9
use Twig_Extension;
10
11
/**
12
 * Twig extensions for Google Analytics
13
 */
14
class GoogleAnalyticsTwigExtension extends Twig_Extension
15
{
16
    protected $accountVarName = 'account_id';
17
18
    protected $accountId;
19
20
    /** @var OrderPreparer */
21
    protected $orderPreparer;
22
23
    /** @var OrderConverter */
24
    protected $orderConverter;
25
26
    /**
27
     * Returns a list of functions to add to the existing list.
28
     *
29
     * @return array An array of functions
30
     */
31
    public function getFunctions()
32
    {
33
        return array(
34
            new \Twig_SimpleFunction(
35
                'google_analytics_initialize',
36
                array($this, 'renderInitialize'),
37
                array('is_safe' => array('html'), 'needs_environment' => true)
38
            ),
39
            new \Twig_SimpleFunction(
40
                'google_analytics_track_order',
41
                array($this, 'renderECommerceTracking'),
42
                array('is_safe' => array('html'), 'needs_environment' => true)
43
            ),
44
        );
45
    }
46
47
    /**
48
     * @param string $id the Google Analytics Account ID
49
     */
50
    public function setAccountID($id)
51
    {
52
        $this->accountId = $id;
53
    }
54
55
    /**
56
     * @param OrderPreparer $preparer
57
     */
58
    public function setOrderPreparer($preparer)
59
    {
60
        $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...
61
    }
62
63
    /**
64
     * @param OrderConverter $converter
65
     */
66
    public function setOrderConverter($converter)
67
    {
68
        $this->orderConverter = $converter;
69
    }
70
71
    /**
72
     * Renders the default Google Analytics JavaScript.
73
     *
74
     * If the options are not set it'll try and load the account ID from your parameters (google.analytics.account_id)
75
     *
76
     * @param \Twig_Environment $environment
77
     * @param array|null        $options     Example: {account_id: 'UA-XXXXX-Y'}
78
     *
79
     * @return string the HTML rendered
80
     *
81
     * @throws \Twig_Error_Runtime when the Google Analytics ID is nowhere to be found
82
     */
83
    public function renderInitialize(\Twig_Environment $environment, $options = null)
84
    {
85
        if (\is_null($options)) {
86
            $options = array();
87
        }
88
89
        $defaults = array();
90
91
        $this->setOptionIfNotSet($defaults, $this->accountVarName, $this->accountId);
92
93
        // Things set in $options will override things set in $defaults.
94
        $options = array_merge($defaults, $options);
95
96
        if (!$this->isOptionSet($options, $this->accountVarName)) {
97
            throw new \Twig_Error_Runtime(
98
                "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'."
99
            );
100
        }
101
102
        $template = $environment->loadTemplate('KunstmaanSeoBundle:GoogleAnalyticsTwigExtension:init.html.twig');
103
104
        return $template->render($options);
105
    }
106
107
    /**
108
     * @param Twig_Environment $environment
109
     * @param Order            $order
110
     *
111
     * @return string the HTML rendered
112
     */
113
    public function renderECommerceTracking(\Twig_Environment $environment, Order $order)
114
    {
115
        $order = $this->orderPreparer->prepare($order);
116
        $options = $this->orderConverter->convert($order);
117
        $template = $environment->loadTemplate(
118
            'KunstmaanSeoBundle:GoogleAnalyticsTwigExtension:ecommerce_tracking.html.twig'
119
        );
120
121
        return $template->render($options);
122
    }
123
124
    /**
125
     * Prefer the given option if already set. Otherwise set the value given.
126
     *
127
     * @param array  &$arr   This is modified in place
128
     * @param string $option the key in the $arr array
129
     * @param mixed  $value  the new value if the option wasn't set already
130
     */
131
    private function setOptionIfNotSet(&$arr, $option, $value)
132
    {
133
        if ($this->isOptionSet($arr, $option)) {
134
            $arr[$option] = $value;
135
        }
136
    }
137
138
    /**
139
     * Check if an option is set.
140
     *
141
     * @param array  $arr    the array to check
142
     * @param string $option the key in the $arr array
143
     *
144
     * @return bool
145
     */
146
    private function isOptionSet($arr, $option)
147
    {
148
        return !isset($arr[$option]) || !empty($arr[$option]);
149
    }
150
}
151