Completed
Pull Request — master (#358)
by Simon
04:39
created

addConnectTemplateVariablesToDetail()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 46
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 26
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 46
rs 8.4751
1
<?php
2
/**
3
 * (c) shopware AG <[email protected]>
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace ShopwarePlugins\Connect\Subscribers;
9
10
use Enlight\Event\SubscriberInterface;
11
use Shopware\Connect\SDK;
12
use ShopwarePlugins\Connect\Components\Config;
13
use ShopwarePlugins\Connect\Components\Helper;
14
use ShopwarePlugins\Connect\Components\Utils\ConnectOrderUtil;
15
16
/**
17
 * Loads various template extensions
18
 */
19
class TemplateExtension implements SubscriberInterface
20
{
21
    /**
22
     * @var string
23
     */
24
    private $pluginPath;
25
26
    /**
27
     * @var \Shopware_Components_Snippet_Manager
28
     */
29
    private $snippetManager;
30
31
    /**
32
     * @var SDK
33
     */
34
    private $sdk;
35
    /**
36
     * @var Helper
37
     */
38
    private $helper;
39
40
    /**
41
     * @param string $pluginPath
42
     * @param \Shopware_Components_Snippet_Manager $snippetManager
43
     * @param SDK $sdk
44
     * @param Helper $helper
45
     */
46
    public function __construct($pluginPath, \Shopware_Components_Snippet_Manager $snippetManager, SDK $sdk, Helper $helper)
47
    {
48
        $this->pluginPath = $pluginPath;
49
        $this->snippetManager = $snippetManager;
50
        $this->sdk = $sdk;
51
        $this->helper = $helper;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public static function getSubscribedEvents()
58
    {
59
        return [
60
            'Enlight_Controller_Action_PostDispatch_Backend_Order' => 'onPostDispatchBackendOrder',
61
            'Enlight_Controller_Action_PostDispatch_Frontend_Detail' => 'addConnectTemplateVariablesToDetail',
62
            'Enlight_Controller_Action_PostDispatch_Frontend' => 'addConnectStyle'
63
        ];
64
    }
65
66
    public function addConnectStyle(\Enlight_Event_EventArgs $args)
67
    {
68
        /** @var \Enlight_Controller_Action $subject */
69
        $subject = $args->getSubject();
70
        $subject->View()->addTemplateDir($this->pluginPath . 'Views/responsive/', 'connect');
71
    }
72
73
    /**
74
     * Extends the order backend module in order to show a special hint for connect products
75
     *
76
     * @param \Enlight_Event_EventArgs $args
77
     */
78 View Code Duplication
    public function onPostDispatchBackendOrder(\Enlight_Event_EventArgs $args)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80
        /** @var $subject \Enlight_Controller_Action */
81
        $subject = $args->getSubject();
82
        $request = $subject->Request();
83
84
        switch ($request->getActionName()) {
85
            case 'load':
86
                $subject->View()->addTemplateDir($this->pluginPath . 'Views/', 'connect');
87
                $this->snippetManager->addConfigDir($this->pluginPath . 'Snippets/');
88
89
                $subject->View()->extendsTemplate(
90
                    'backend/order/view/connect.js'
91
                );
92
93
                $subject->View()->extendsTemplate(
94
                    'backend/order/controller/connect_main.js'
95
                );
96
97
                break;
98
99
            case 'getList':
100
                $subject->View()->data = $this->markConnectOrders(
101
                    $subject->View()->data
102
                );
103
104
                break;
105
106
            default:
107
                break;
108
        }
109
    }
110
111
    /**
112
     * Mark Orders as Connect Orders for view purposes.
113
     *
114
     * @param array $data
115
     * @return array
116
     */
117
    private function markConnectOrders($data)
118
    {
119
        $orderIds = array_map(function ($orderView) {
120
            return (int) $orderView['id'];
121
        }, $data);
122
123
        if (!$orderIds) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $orderIds of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
124
            return $data;
125
        }
126
127
        $connectOrderData = [];
128
129
130
        $connectOrderUtil = new ConnectOrderUtil();
131
        $result = $connectOrderUtil->getRemoteConnectOrders($orderIds);
132
133
        foreach ($result as $connectOrder) {
134
            $connectOrderData[$connectOrder['orderID']] = $connectOrder;
135
        }
136
137
        $result = $connectOrderUtil->getLocalConnectOrders($orderIds);
138
139
        foreach ($result as $connectOrder) {
140
            $connectOrderData[$connectOrder['orderID']] = $connectOrder;
141
        }
142
143
        if (!$connectOrderData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $connectOrderData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
144
            return $data;
145
        }
146
147
        $shopNames = [];
148
149
        foreach ($data as $idx => $order) {
150
            if (! isset($connectOrderData[$order['id']])) {
151
                continue;
152
            }
153
154
            $result = $connectOrderData[$order['id']];
155
156
            $data[$idx]['connectShopId'] = $result['connect_shop_id'];
157
            $data[$idx]['connectOrderId'] = $result['connect_order_id'];
158
159
            if (!isset($shopNames[$result['connect_shop_id']])) {
160
                $shopNames[$result['connect_shop_id']] = $this->sdk->getShop($result['connect_shop_id'])->name;
161
            }
162
163
            $data[$idx]['connectShop'] = $shopNames[$result['connect_shop_id']];
164
        }
165
166
        return $data;
167
    }
168
169
    /**
170
     * Event listener method for the frontend detail page. Will add connect template variables if the current product
171
     * is a connect product.
172
     *
173
     * @event Enlight_Controller_Action_PostDispatch_Frontend_Detail
174
     * @param \Enlight_Event_EventArgs $args
175
     */
176
    public function addConnectTemplateVariablesToDetail(\Enlight_Event_EventArgs $args)
177
    {
178
        /** @var $action \Enlight_Controller_Action */
179
        $action = $args->getSubject();
180
        $view = $action->View();
181
182
        $action->View()->addTemplateDir($this->pluginPath . 'Views/responsive', 'connect');
183
184
        $articleData = $view->getAssign('sArticle');
185
        if (empty($articleData['articleID'])) {
186
            return;
187
        }
188
189
        if ($this->helper->isRemoteArticleDetail($articleData['articleDetailsID']) === false) {
190
            return;
191
        }
192
193
        $shopProductId = $this->helper->getShopProductId($articleData['articleDetailsID']);
194
        $products = $this->helper->getRemoteProducts([$shopProductId->sourceId], $shopProductId->shopId);
195
196
        if (empty($products)) {
197
            return;
198
        }
199
200
        $product = reset($products);
201
        if (empty($product->shopId)) {
202
            return;
203
        }
204
205
        // Fix prices for displaying
206
        foreach (['price', 'purchasePrice', 'vat'] as $name) {
207
            $product->$name = round($product->$name, 2);
208
        }
209
210
        $shop = $this->sdk->getShop($product->shopId);
211
212
        $modelsManager = Shopware()->Models();
213
        /** @var \ShopwarePlugins\Connect\Components\Config $configComponent */
214
        $configComponent = new Config($modelsManager);
215
        $view->assign([
216
            'connectProduct' => $product,
217
            'connectShop' => $shop,
218
            'connectShopInfo' => $configComponent->getConfig('detailShopInfo'),
219
            'connectNoIndex' => $configComponent->getConfig('detailProductNoIndex'),
220
        ]);
221
    }
222
}
223