Completed
Pull Request — master (#392)
by Christian
03:03
created

addConnectTemplateVariablesToDetail()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 43
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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