Completed
Push — master ( 57f761...d66706 )
by David
04:19
created

Dispatch::isPreDispatchByModule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.8333
cc 1
nc 1
nop 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 Doctrine\DBAL\Connection;
20
use Enlight\Event\SubscriberInterface;
21
use WbmTagManager\Services\TagManagerVariables;
22
23
/**
24
 * Class Dispatch
25
 */
26
class Dispatch implements SubscriberInterface
27
{
28
    /**
29
     * @var TagManagerVariables
30
     */
31
    private $variables;
32
33
    /**
34
     * @var \Shopware_Components_Config
35
     */
36
    private $config;
37
38
    /**
39
     * @var Connection
40
     */
41
    private $connection;
42
43
    /**
44
     * @param TagManagerVariables         $variables
45
     * @param \Shopware_Components_Config $config
46
     * @param Connection                  $connection
47
     */
48
    public function __construct(
49
        TagManagerVariables $variables,
50
        \Shopware_Components_Config $config,
51
        Connection $connection
52
    ) {
53
        $this->variables = $variables;
54
        $this->config = $config;
55
        $this->connection = $connection;
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    public static function getSubscribedEvents()
62
    {
63
        return [
64
            'Enlight_Controller_Action_PostDispatch_Frontend' => 'onPostDispatch',
65
            'Enlight_Controller_Action_PostDispatch_Widgets' => 'onPostDispatch',
66
            'Enlight_Controller_Action_PreDispatch_Frontend' => 'onPreDispatch',
67
            'Enlight_Controller_Action_PreDispatch_Widgets' => 'onPreDispatch',
68
        ];
69
    }
70
71
    /**
72
     * @param \Enlight_Controller_ActionEventArgs $args
73
     *
74
     * @throws \Exception
75
     */
76
    public function onPostDispatch(\Enlight_Controller_ActionEventArgs $args)
77
    {
78
        $this->handleDispatch($args);
79
    }
80
81
    /**
82
     * @param \Enlight_Controller_ActionEventArgs $args
83
     *
84
     * @throws \Exception
85
     */
86
    public function onPreDispatch(\Enlight_Controller_ActionEventArgs $args)
87
    {
88
        $this->handleDispatch($args, true);
89
    }
90
91
    /**
92
     * @param \Enlight_Controller_ActionEventArgs $args
93
     * @param bool                                $isPreDispatch
94
     *
95
     * @throws \Exception
96
     */
97
    public function handleDispatch(
98
        \Enlight_Controller_ActionEventArgs $args,
99
        $isPreDispatch = false
100
    ) {
101
        if (
102
            !$this->config->getByNamespace('WbmTagManager', 'wbmTagManagerActive') ||
103
            empty($this->config->getByNamespace('WbmTagManager', 'wbmTagManagerContainer'))
104
        ) {
105
            return;
106
        }
107
108
        $controller = $args->getSubject();
109
        $request = $controller->Request();
110
111
        $module = $oModule = join('_', [
112
                strtolower($request->getModuleName()),
113
                strtolower($request->getControllerName()),
114
                strtolower($request->getActionName()),
115
            ]);
116
117
        if ($module == 'frontend_checkout_ajaxcart') {
118
            $module = 'frontend_checkout_' . strtolower($request->getParam('action'));
119
        }
120
121
        $search = [
122
            'widgets_listing_ajaxlisting',
123
            'widgets_listing_listingcount',
124
            'frontend_checkout_ajaxcart',
125
            'frontend_checkout_ajax_add_article',
126
            'frontend_checkout_ajax_delete_article',
127
        ];
128
        $replace = [
129
            'frontend_listing_index',
130
            'frontend_listing_index',
131
            'frontend_checkout_cart',
132
            'frontend_checkout_ajaxaddarticlecart',
133
            'frontend_checkout_ajaxdeletearticlecart',
134
        ];
135
        $module = str_replace($search, $replace, $module);
136
137
        if ($isPreDispatch !== $this->isPreDispatchByModule($module)) {
138
            return;
139
        }
140
141
        $this->variables->setViewVariables($controller->View()->getAssign());
142
        $this->variables->render($module);
143
144
        if ($isPreDispatch) {
145
            return;
146
        }
147
148
        // Since SW 5.3 the generic listingCountAction is used for paginated listings.
149
        // Get the response json body, decode it, prepend the dataLayer to the listing key
150
        // and set json encoded markup as response body.
151
        if ($oModule == 'widgets_listing_listingcount') {
152
            /** @var \Enlight_Controller_Response_ResponseHttp $response */
153
            $response = $controller->Response();
154
            $data = json_decode($response->getBody(), true);
155
156
            if (isset($data['listing'])) {
157
                if ($this->variables->getVariables()) {
158
                    $data['listing'] = $this->variables->prependDataLayer(
159
                        $data['listing'],
160
                        $this->config->getByNamespace('WbmTagManager', 'wbmTagManagerJsonPrettyPrint')
161
                    );
162
163
                    $response->setBody(json_encode($data));
164
                }
165
            }
166
        }
167
    }
168
169
    /**
170
     * @param string $module
171
     *
172
     * @return bool
173
     */
174
    private function isPreDispatchByModule($module)
175
    {
176
        $qb = $this->connection->createQueryBuilder();
177
        $qb->select(
178
                ['predispatch']
179
            )
180
            ->from('wbm_data_layer_modules')
181
            ->where('predispatch = 1')
182
            ->andWhere('module = :module')
183
            ->setParameter('module', $module);
184
185
        return (bool) $qb->execute()->fetch(\PDO::FETCH_COLUMN);
186
    }
187
}
188