Completed
Pull Request — master (#358)
by Stefan
30:38 queued 25:29
created

TemplateExtension::markConnectOrders()   C

Complexity

Conditions 8
Paths 21

Size

Total Lines 51
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 27
c 0
b 0
f 0
nc 21
nop 1
dl 0
loc 51
rs 6.5978

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