Completed
Pull Request — master (#358)
by Simon
03:44
created

addConnectTemplateVariablesToDetail()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 28
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 48
rs 8.551
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 ShopwarePlugins\Connect\Components\Config;
11
use ShopwarePlugins\Connect\Components\Utils\ConnectOrderUtil;
12
13
/**
14
 * Loads various template extensions
15
 *
16
 * Class TemplateExtension
17
 * @package ShopwarePlugins\Connect\Subscribers
18
 */
19
class TemplateExtension extends BaseSubscriber
20
{
21
    public function getSubscribedEvents()
22
    {
23
        return [
24
            'Enlight_Controller_Action_PostDispatch_Backend_Order' => 'onPostDispatchBackendOrder',
25
            'Enlight_Controller_Action_PostDispatch_Frontend_Detail' => 'addConnectTemplateVariablesToDetail',
26
            'Enlight_Controller_Action_PostDispatch_Frontend' => 'addConnectStyle'
27
        ];
28
    }
29
30
    public function addConnectStyle(\Enlight_Event_EventArgs $args)
31
    {
32
        $this->registerMyTemplateDir();
33
    }
34
35
    /**
36
     * Extends the order backend module in order to show a special hint for connect products
37
     *
38
     * @param \Enlight_Event_EventArgs $args
39
     */
40 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...
41
    {
42
        /** @var $subject \Enlight_Controller_Action */
43
        $subject = $args->getSubject();
44
        $request = $subject->Request();
45
46
        switch ($request->getActionName()) {
47
            case 'load':
48
                $this->registerMyTemplateDir();
49
                $this->registerMySnippets();
50
                $subject->View()->extendsTemplate(
51
                    'backend/order/view/connect.js'
52
                );
53
54
                $subject->View()->extendsTemplate(
55
                    'backend/order/controller/connect_main.js'
56
                );
57
58
                break;
59
60
            case 'getList':
61
                $subject->View()->data = $this->markConnectOrders(
62
                    $subject->View()->data
63
                );
64
65
                break;
66
67
            default:
68
                break;
69
        }
70
    }
71
72
    /**
73
     * Mark Orders as Connect Orders for view purposes.
74
     *
75
     * @param array $data
76
     * @return array
77
     */
78
    private function markConnectOrders($data)
79
    {
80
        $sdk = $this->getSDK();
81
82
        $orderIds = array_map(function ($orderView) {
83
            return (int) $orderView['id'];
84
        }, $data);
85
86
        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...
87
            return $data;
88
        }
89
90
        $connectOrderData = [];
91
92
93
        $connectOrderUtil = new ConnectOrderUtil();
94
        $result = $connectOrderUtil->getRemoteConnectOrders($orderIds);
95
96
        foreach ($result as $connectOrder) {
97
            $connectOrderData[$connectOrder['orderID']] = $connectOrder;
98
        }
99
100
        $result = $connectOrderUtil->getLocalConnectOrders($orderIds);
101
102
        foreach ($result as $connectOrder) {
103
            $connectOrderData[$connectOrder['orderID']] = $connectOrder;
104
        }
105
106
        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...
107
            return $data;
108
        }
109
110
        $shopNames = [];
111
112
        foreach ($data as $idx => $order) {
113
            if (! isset($connectOrderData[$order['id']])) {
114
                continue;
115
            }
116
117
            $result = $connectOrderData[$order['id']];
118
119
            $data[$idx]['connectShopId'] = $result['connect_shop_id'];
120
            $data[$idx]['connectOrderId'] = $result['connect_order_id'];
121
122
            if (!isset($shopNames[$result['connect_shop_id']])) {
123
                $shopNames[$result['connect_shop_id']] = $sdk->getShop($result['connect_shop_id'])->name;
124
            }
125
126
            $data[$idx]['connectShop'] = $shopNames[$result['connect_shop_id']];
127
        }
128
129
        return $data;
130
    }
131
132
    /**
133
     * Event listener method for the frontend detail page. Will add connect template variables if the current product
134
     * is a connect product.
135
     *
136
     * @event Enlight_Controller_Action_PostDispatch_Frontend_Detail
137
     * @param \Enlight_Event_EventArgs $args
138
     */
139
    public function addConnectTemplateVariablesToDetail(\Enlight_Event_EventArgs $args)
140
    {
141
        /** @var $action \Enlight_Controller_Action */
142
        $action = $args->getSubject();
143
        $view = $action->View();
144
        $sdk = $this->getSDK();
145
        $helper = $this->getHelper();
146
147
        $this->registerMyTemplateDir();
148
149
        $articleData = $view->getAssign('sArticle');
150
        if (empty($articleData['articleID'])) {
151
            return;
152
        }
153
154
        if ($helper->isRemoteArticleDetail($articleData['articleDetailsID']) === false) {
155
            return;
156
        }
157
158
        $shopProductId = $helper->getShopProductId($articleData['articleDetailsID']);
159
        $products = $helper->getRemoteProducts([$shopProductId->sourceId], $shopProductId->shopId);
160
161
        if (empty($products)) {
162
            return;
163
        }
164
165
        $product = reset($products);
166
        if (empty($product->shopId)) {
167
            return;
168
        }
169
170
        // Fix prices for displaying
171
        foreach (['price', 'purchasePrice', 'vat'] as $name) {
172
            $product->$name = round($product->$name, 2);
173
        }
174
175
        $shop = $sdk->getShop($product->shopId);
176
177
        $modelsManager = Shopware()->Models();
178
        /** @var \ShopwarePlugins\Connect\Components\Config $configComponent */
179
        $configComponent = new Config($modelsManager);
180
        $view->assign([
181
            'connectProduct' => $product,
182
            'connectShop' => $shop,
183
            'connectShopInfo' => $configComponent->getConfig('detailShopInfo'),
184
            'connectNoIndex' => $configComponent->getConfig('detailProductNoIndex'),
185
        ]);
186
    }
187
}
188