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