Completed
Pull Request — master (#358)
by Simon
04:39
created

Dispatches::onPostDispatchBackendShipping()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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