Completed
Push — master ( 05ab3c...1a19c2 )
by David
02:03
created

PostDispatch::onPostDispatch()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 61
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 1
Metric Value
c 6
b 0
f 1
dl 0
loc 61
rs 7.399
cc 7
eloc 37
nc 9
nop 1

How to fix   Long Method   

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 PostDispatch
24
 */
25
class PostDispatch implements SubscriberInterface
26
{
27
    /**
28
     * @var TagManagerVariables
29
     */
30
    private $variables;
31
32
    /**
33
     * @var \Shopware_Components_Config
34
     */
35
    private $config;
36
37
    public function __construct(
38
        TagManagerVariables $variables,
39
        \Shopware_Components_Config $config
40
    ) {
41
        $this->variables = $variables;
42
        $this->config = $config;
43
    }
44
45
    /**
46
     * @return array
47
     */
48
    public static function getSubscribedEvents()
49
    {
50
        return [
51
            'Enlight_Controller_Action_PostDispatch_Frontend' => 'onPostDispatch',
52
            'Enlight_Controller_Action_PostDispatch_Widgets' => 'onPostDispatch',
53
        ];
54
    }
55
56
    /**
57
     * @param \Enlight_Controller_ActionEventArgs $args
58
     */
59
    public function onPostDispatch(\Enlight_Controller_ActionEventArgs $args)
60
    {
61
        if (
62
            !$this->config->getByNamespace('WbmTagManager', 'wbmTagManagerActive') ||
63
            empty($this->config->getByNamespace('WbmTagManager', 'wbmTagManagerContainer'))
64
        ) {
65
            return;
66
        }
67
68
        $controller = $args->getSubject();
69
        $request = $controller->Request();
70
71
        $module = $oModule = join('_', [
72
                strtolower($request->getModuleName()),
73
                strtolower($request->getControllerName()),
74
                strtolower($request->getActionName()),
75
            ]);
76
77
        if ($module == 'frontend_checkout_ajaxcart') {
78
            $module = 'frontend_checkout_' . strtolower($request->getParam('action'));
79
        }
80
81
        $search = [
82
            'widgets_listing_ajaxlisting',
83
            'widgets_listing_listingcount',
84
            'frontend_checkout_ajaxcart',
85
            'frontend_checkout_ajax_add_article',
86
            'frontend_checkout_ajax_delete_article',
87
        ];
88
        $replace = [
89
            'frontend_listing_index',
90
            'frontend_listing_index',
91
            'frontend_checkout_cart',
92
            'frontend_checkout_ajaxaddarticlecart',
93
            'frontend_checkout_ajaxdeletearticlecart',
94
        ];
95
        $module = str_replace($search, $replace, $module);
96
97
        $this->variables->setViewVariables($controller->View()->getAssign());
98
        $this->variables->render($module);
99
100
        // Since SW 5.3 the generic listingCountAction is used for paginated listings.
101
        // Get the response json body, decode it, prepend the dataLayer to the listing key
102
        // and set json encoded markup as response body.
103
        if ($oModule == 'widgets_listing_listingcount') {
104
            /** @var \Enlight_Controller_Response_ResponseHttp $response */
105
            $response = $controller->Response();
106
            $data = json_decode($response->getBody(), true);
107
108
            if (isset($data['listing'])) {
109
                if ($this->variables->getVariables()) {
110
                    $data['listing'] = $this->variables->prependDataLayer(
111
                        $data['listing'],
112
                        $this->config->getByNamespace('WbmTagManager', 'wbmTagManagerJsonPrettyPrint')
113
                    );
114
115
                    $response->setBody(json_encode($data));
116
                }
117
            }
118
        }
119
    }
120
}
121