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

BasketWidget::fixBasketWidgetForConnect()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 49
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 27
nc 5
nop 1
dl 0
loc 49
rs 8.5906
c 1
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\BasketHelper;
12
use ShopwarePlugins\Connect\Components\Helper;
13
14
/**
15
 * The basket widget shows the current basket amount and the current basket's products.
16
 * It needs to be modified in order to show the connect products / valuess
17
 *
18
 * Class BasketWidget
19
 * @package ShopwarePlugins\Connect\Subscribers
20
 */
21
class BasketWidget implements SubscriberInterface
22
{
23
    /**
24
     * @var BasketHelper
25
     */
26
    private $basketHelper;
27
28
    /**
29
     * @var Helper
30
     */
31
    private $helper;
32
33
    /**
34
     * @param BasketHelper $basketHelper
35
     * @param Helper $helper
36
     */
37
    public function __construct(BasketHelper $basketHelper, Helper $helper)
38
    {
39
        $this->basketHelper = $basketHelper;
40
        $this->helper = $helper;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public static function getSubscribedEvents()
47
    {
48
        return [
49
            'sBasket::sGetBasket::after' => 'storeBasketResultToSession',
50
            'Enlight_Controller_Action_PostDispatch_Widgets_Checkout' => 'fixBasketWidgetForConnect',
51
            'Enlight_Controller_Action_PostDispatch_Frontend_Checkout' => 'fixBasketWidgetForConnect'
52
        ];
53
    }
54
55
    /**
56
     * Fix the basket widget
57
     *
58
     * @param \Enlight_Event_EventArgs $args
59
     */
60
    public function fixBasketWidgetForConnect(\Enlight_Event_EventArgs $args)
61
    {
62
        /** @var $action \Enlight_Controller_Action */
63
        $action = $args->getSubject();
64
        $view = $action->View();
65
        $request = $action->Request();
66
        $actionName = $request->getActionName();
67
68
        // ajaxCart was removed from array, because when user puts
69
        // connect article it's displayed in ajax cart, but
70
        // then puts local article and connect is missing
71
        if (!in_array($actionName, ['info', 'ajaxAmount'])) {
72
            return;
73
        }
74
75
        // If the basket is empty or does not contain connect products return
76
        $basket = Shopware()->Session()->connectGetBasket;
77
        if (empty($basket) || !$this->helper->hasBasketConnectProducts(Shopware()->SessionID())) {
78
            return;
79
        }
80
81
        $this->basketHelper->setBasket($basket);
82
83
        // Return if we don't have any connect products
84
        $connectProducts = $this->basketHelper->getConnectProducts();
85
        if (empty($connectProducts)) {
86
            return;
87
        }
88
89
        // Fix the basket for connect
90
        $this->basketHelper->fixBasket();
91
        $vars = $this->basketHelper->getDefaultTemplateVariables();
92
93
        // Fix the basket widget template
94
        if ($actionName === 'ajaxCart') {
95
            $view->sBasket = $vars['sBasket'];
96
97
            $view->sShippingcosts = $view->sBasket['sShippingcosts'];
98
            $view->sShippingcostsDifference = $view->sBasket['sShippingcostsDifference'];
99
            $view->sAmount = $view->sBasket['sAmount'];
100
            $view->sAmountWithTax = $view->sBasket['sAmountWithTax'];
101
            $view->sAmountTax = $view->sBasket['sAmountTax'];
102
            $view->sAmountNet = $view->sBasket['AmountNetNumeric'];
103
        } else {
104
            // Assign the new amount / quantity
105
            $view->sBasketQuantity = $vars['sBasket']['Quantity'];
106
            $view->sBasketAmount = $vars['sBasket']['Amount'];
107
        }
108
    }
109
110
    /**
111
     * Hook for the sGetBasket method, which will store the most recent basket to the session
112
     *
113
     * @event sBasket::sGetBasket::after
114
     * @param \Enlight_Hook_HookArgs $args
115
     */
116
    public function storeBasketResultToSession(\Enlight_Hook_HookArgs $args)
117
    {
118
        $basket = $args->getReturn();
119
        Shopware()->Session()->connectGetBasket = $basket;
120
121
        $args->setReturn($basket);
122
    }
123
}
124