Completed
Push — master ( c8ca4b...15b904 )
by David
04:12
created

Dispatch   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 16
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 159
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getSubscribedEvents() 0 9 1
A onPostDispatch() 0 4 1
A onPreDispatch() 0 4 1
C handleDispatch() 0 59 11
A rewriteModuleKey() 0 22 1
1
<?php
2
/**
3
 * Tag Manager
4
 * Copyright (c) Webmatch GmbH
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 */
16
17
namespace WbmTagManager\Subscriber\Frontend;
18
19
use Enlight\Event\SubscriberInterface;
20
use WbmTagManager\Services\TagManagerVariables;
21
22
/**
23
 * Class Dispatch
24
 */
25
class Dispatch implements SubscriberInterface
26
{
27
    /**
28
     * @var TagManagerVariables
29
     */
30
    private $variables;
31
32
    /**
33
     * @var array
34
     */
35
    private $pluginConfig;
36
37
    /**
38
     * @var array
39
     */
40
    private $modules;
41
42
    /**
43
     * @param TagManagerVariables         $variables
44
     * @param array                       $pluginConfig
45
     * @param array                       $modules
46
     */
47
    public function __construct(
48
        TagManagerVariables $variables,
49
        $pluginConfig,
50
        $modules
51
    ) {
52
        $this->variables = $variables;
53
        $this->pluginConfig = $pluginConfig;
54
        $this->modules = $modules;
55
    }
56
57
    /**
58
     * @return array
59
     */
60
    public static function getSubscribedEvents()
61
    {
62
        return [
63
            'Enlight_Controller_Action_PostDispatch_Frontend' => 'onPostDispatch',
64
            'Enlight_Controller_Action_PostDispatch_Widgets' => 'onPostDispatch',
65
            'Enlight_Controller_Action_PreDispatch_Frontend' => 'onPreDispatch',
66
            'Enlight_Controller_Action_PreDispatch_Widgets' => 'onPreDispatch',
67
        ];
68
    }
69
70
    /**
71
     * @param \Enlight_Controller_ActionEventArgs $args
72
     *
73
     * @throws \Exception
74
     */
75
    public function onPostDispatch(\Enlight_Controller_ActionEventArgs $args)
76
    {
77
        $this->handleDispatch($args);
78
    }
79
80
    /**
81
     * @param \Enlight_Controller_ActionEventArgs $args
82
     *
83
     * @throws \Exception
84
     */
85
    public function onPreDispatch(\Enlight_Controller_ActionEventArgs $args)
86
    {
87
        $this->handleDispatch($args, true);
88
    }
89
90
    /**
91
     * @param \Enlight_Controller_ActionEventArgs $args
92
     * @param bool                                $isPreDispatch
93
     *
94
     * @throws \Exception
95
     */
96
    public function handleDispatch(
97
        \Enlight_Controller_ActionEventArgs $args,
98
        $isPreDispatch = false
99
    ) {
100
        if (
101
            !$this->pluginConfig['wbmTagManagerActive'] ||
102
            empty($this->pluginConfig['wbmTagManagerContainer'])
103
        ) {
104
            return;
105
        }
106
107
        $controller = $args->getSubject();
108
        $request = $controller->Request();
109
110
        $module = $oModule = join('_', [
111
                strtolower($request->getModuleName()),
112
                strtolower($request->getControllerName()),
113
                strtolower($request->getActionName()),
114
            ]);
115
116
        if ($module == 'frontend_checkout_ajaxcart') {
117
            $module = 'frontend_checkout_' . strtolower($request->getParam('action'));
118
        }
119
120
        $module = $this->rewriteModuleKey($module);
121
122
        if (!isset($this->modules[$module]) || (bool) $this->modules[$module] !== $isPreDispatch) {
123
            return;
124
        }
125
126
        if (!$this->variables->getVariables()) {
127
            $this->variables->setViewVariables($controller->View()->getAssign());
128
            $this->variables->render($module);
129
        }
130
131
        if ($isPreDispatch) {
132
            return;
133
        }
134
135
        // Since SW 5.3 the generic listingCountAction is used for paginated listings.
136
        // Get the response json body, decode it, prepend the dataLayer to the listing key
137
        // and set json encoded markup as response body.
138
        if ($oModule == 'widgets_listing_listingcount') {
139
            /** @var \Enlight_Controller_Response_ResponseHttp $response */
140
            $response = $controller->Response();
141
            $data = json_decode($response->getBody(), true);
142
143
            if (isset($data['listing'])) {
144
                if ($this->variables->getVariables()) {
145
                    $data['listing'] = $this->variables->prependDataLayer(
146
                        $data['listing'],
147
                        $this->pluginConfig['wbmTagManagerJsonPrettyPrint']
148
                    );
149
150
                    $response->setBody(json_encode($data));
151
                }
152
            }
153
        }
154
    }
155
156
    /**
157
     * @param string $module
158
     *
159
     * @return string
160
     */
161
    private function rewriteModuleKey($module)
162
    {
163
        $search = [
164
            'widgets_listing_ajaxlisting',
165
            'widgets_listing_listingcount',
166
            'frontend_checkout_ajaxcart',
167
            'frontend_checkout_ajax_add_article',
168
            'frontend_checkout_ajax_delete_article',
169
            'frontend_checkout_deletearticle',
170
        ];
171
172
        $replace = [
173
            'frontend_listing_index',
174
            'frontend_listing_index',
175
            'frontend_checkout_cart',
176
            'frontend_checkout_ajaxaddarticlecart',
177
            'frontend_checkout_ajaxdeletearticlecart',
178
            'frontend_checkout_ajaxdeletearticlecart',
179
        ];
180
181
        return str_replace($search, $replace, $module);
182
    }
183
}
184