Completed
Push — master ( fb7508...c5b3db )
by David
05:11
created

Dispatch::handleDispatch()   B

Complexity

Conditions 10
Paths 13

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 57
rs 7.0715
cc 10
nc 13
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 \Shopware_Components_Config
34
     */
35
    private $config;
36
37
    /**
38
     * @var array
39
     */
40
    private $modules;
41
42
    /**
43
     * @param TagManagerVariables         $variables
44
     * @param \Shopware_Components_Config $config
45
     * @param array                       $modules
46
     */
47
    public function __construct(
48
        TagManagerVariables $variables,
49
        \Shopware_Components_Config $config,
50
        $modules
51
    ) {
52
        $this->variables = $variables;
53
        $this->config = $config;
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->config->getByNamespace('WbmTagManager', 'wbmTagManagerActive') ||
102
            empty($this->config->getByNamespace('WbmTagManager', '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
        $this->variables->setViewVariables($controller->View()->getAssign());
127
        $this->variables->render($module);
128
129
        if ($isPreDispatch) {
130
            return;
131
        }
132
133
        // Since SW 5.3 the generic listingCountAction is used for paginated listings.
134
        // Get the response json body, decode it, prepend the dataLayer to the listing key
135
        // and set json encoded markup as response body.
136
        if ($oModule == 'widgets_listing_listingcount') {
137
            /** @var \Enlight_Controller_Response_ResponseHttp $response */
138
            $response = $controller->Response();
139
            $data = json_decode($response->getBody(), true);
140
141
            if (isset($data['listing'])) {
142
                if ($this->variables->getVariables()) {
143
                    $data['listing'] = $this->variables->prependDataLayer(
144
                        $data['listing'],
145
                        $this->config->getByNamespace('WbmTagManager', 'wbmTagManagerJsonPrettyPrint')
146
                    );
147
148
                    $response->setBody(json_encode($data));
149
                }
150
            }
151
        }
152
    }
153
154
    /**
155
     * @param string $module
156
     *
157
     * @return string
158
     */
159
    private function rewriteModuleKey($module)
160
    {
161
        $search = [
162
            'widgets_listing_ajaxlisting',
163
            'widgets_listing_listingcount',
164
            'frontend_checkout_ajaxcart',
165
            'frontend_checkout_ajax_add_article',
166
            'frontend_checkout_ajax_delete_article',
167
        ];
168
169
        $replace = [
170
            'frontend_listing_index',
171
            'frontend_listing_index',
172
            'frontend_checkout_cart',
173
            'frontend_checkout_ajaxaddarticlecart',
174
            'frontend_checkout_ajaxdeletearticlecart',
175
        ];
176
177
        return str_replace($search, $replace, $module);
178
    }
179
}
180