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

Dispatches::onPostDispatchBackendShipping()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 17
rs 9.4285
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 ShopwarePlugins\Connect\Components\Helper;
12
13
/**
14
 * Extends the dispatch module and removes non-connect aware dispatches, if connect products are in the basket
15
 */
16
class Dispatches implements SubscriberInterface
17
{
18
    /**
19
     * @var Helper
20
     */
21
    private $helper;
22
23
    /**
24
     * @param Helper $helper
25
     */
26
    public function __construct(Helper $helper)
27
    {
28
        $this->helper = $helper;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public static function getSubscribedEvents()
35
    {
36
        return [
37
            'Enlight_Controller_Action_PostDispatch_Backend_Shipping' => 'onPostDispatchBackendShipping',
38
            'sAdmin::sGetPremiumDispatches::after' => 'onFilterDispatches',
39
        ];
40
    }
41
42
    /**
43
     * If connect products are in the basket: Remove dispatches which are not allowed for connect
44
     *
45
     * @event sAdmin::sGetPremiumDispatches::after
46
     * @param \Enlight_Hook_HookArgs $args
47
     */
48
    public function onFilterDispatches(\Enlight_Hook_HookArgs $args)
49
    {
50
        $dispatches = $args->getReturn();
51
52
        if (!count($dispatches) > 0) {
53
            return;
54
        }
55
56
        // If no connect products are in basket, don't modify anything
57
        if (!$this->helper->hasBasketConnectProducts(Shopware()->SessionID())) {
58
            return;
59
        }
60
61
        $dispatchIds = array_keys($dispatches);
62
63
        $questionMarks = implode(', ', str_split(str_repeat('?', count($dispatchIds))));
64
65
        $sql = "
66
        SELECT `dispatchID`
67
        FROM s_premium_dispatch_attributes
68
        WHERE `connect_allowed` > 0
69
        AND `dispatchID` IN ({$questionMarks})
70
        ";
71
72
        $allowedDispatchIds = Shopware()->Db()->fetchCol($sql, $dispatchIds);
73
74
        // Remove the non-allowed dispatches from dispatch array
75
        foreach ($dispatches as $id => $dispatch) {
76
            if (!in_array($id, $allowedDispatchIds)) {
77
                unset($dispatches[$id]);
78
            }
79
        }
80
81
        $args->setReturn($dispatches);
82
    }
83
84
    /**
85
     * Extends the shipping backend module
86
     *
87
     * @param \Enlight_Event_EventArgs $args
88
     */
89
    public function onPostDispatchBackendShipping(\Enlight_Event_EventArgs $args)
90
    {
91
        /** @var $subject \Enlight_Controller_Action */
92
        $subject = $args->getSubject();
93
        $request = $subject->Request();
94
95
        switch ($request->getActionName()) {
96
            case 'load':
97
                $subject->View()->extendsTemplate(
98
                    'backend/shipping/connect.js'
99
                );
100
101
                break;
102
            default:
103
                break;
104
        }
105
    }
106
}
107