Completed
Pull Request — master (#358)
by Stefan
03:07
created

Voucher   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 112
rs 10
wmc 9
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getSubscribedEvents() 0 7 1
A removeDiscount() 0 20 3
B preventPercentagedVoucher() 0 30 4
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
 * Handle vouchers, remove discounts and don't allow percentaged vouchers for connect baskets
16
 */
17
class Voucher implements SubscriberInterface
18
{
19
    /**
20
     * @var Helper
21
     */
22
    private $helper;
23
24
    /**
25
     * @var BasketHelper
26
     */
27
    private $basketHelper;
28
29
    /**
30
     * @var \Shopware_Components_Snippet_Manager
31
     */
32
    private $snippetManager;
33
34
    /**
35
     * @param Helper $helper
36
     * @param BasketHelper $basketHelper
37
     * @param \Shopware_Components_Snippet_Manager $snippetManager
38
     */
39
    public function __construct(
40
        Helper $helper,
41
        BasketHelper $basketHelper,
42
        \Shopware_Components_Snippet_Manager $snippetManager)
43
    {
44
        $this->helper = $helper;
45
        $this->basketHelper = $basketHelper;
46
        $this->snippetManager = $snippetManager;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public static function getSubscribedEvents()
53
    {
54
        return [
55
            'Shopware_Modules_Basket_AddVoucher_Start' => 'preventPercentagedVoucher',
56
            'Shopware_Modules_Basket_GetBasket_FilterSQL' => 'removeDiscount'
57
        ];
58
    }
59
60
    /**
61
     * Helper method to remove percentaged discounts from the basket if connect products are available
62
     *
63
     * Alternative:
64
     *  * Calculate discount only for the default products
65
     *  * Removed percentaged discounts only if connect product has a fixedPrice
66
     *
67
     * @event Shopware_Modules_Basket_GetBasket_FilterSQL
68
     */
69
    public function removeDiscount(\Enlight_Event_EventArgs $args)
70
    {
71
        $message = $this->snippetManager->getNamespace('frontend/connect/checkout')->get(
72
            'noPercentagedDiscountsAllowed',
73
            'In Kombination mit connect-Produkten sind keine prozentualen Rabatte möglich.',
74
            true
75
        );
76
77
        if ($this->helper->hasBasketConnectProducts(Shopware()->SessionID())) {
78
            $stmt = Shopware()->Db()->query(
79
                'DELETE FROM s_order_basket WHERE sessionID=? AND modus=3',
80
                [Shopware()->SessionID()]
81
            );
82
83
            // If rows where actually affected, show the corresponding message
84
            if ($stmt->rowCount()) {
85
                Shopware()->Template()->assign('sVoucherError', $message);
86
            }
87
        }
88
    }
89
90
    /**
91
     * Will not allow percentaged vouchers if connect products are in the basket
92
     *
93
     * @event Shopware_Modules_Basket_AddVoucher_Start
94
     *
95
     * @param \Enlight_Event_EventArgs $args
96
     * @return bool|null
97
     */
98
    public function preventPercentagedVoucher(\Enlight_Event_EventArgs $args)
99
    {
100
        $code = $args->getCode();
101
102
        if (!$this->helper->hasBasketConnectProducts(Shopware()->SessionID())) {
103
            return null;
104
        }
105
106
        $message = $this->snippetManager->getNamespace('frontend/connect/checkout')->get(
107
            'noPercentagedVoucherAllowed',
108
            'In Kombination mit connect-Produkten sind keine prozentualen Gutscheine möglich.',
109
            true
110
        );
111
112
        // Exclude general percentaged vouchers
113
        $result = $this->basketHelper->findPercentagedVouchers($code);
114
        if (!empty($result)) {
115
            Shopware()->Template()->assign('sVoucherError', $message);
116
117
            return true;
118
        }
119
120
        // Exclude $this->basketHelper percentaged vouchers
121
        $result = $this->basketHelper->findPercentagedIndividualVouchers($code);
122
        if (!empty($result)) {
123
            Shopware()->Template()->assign('sVoucherError', $message);
124
125
            return true;
126
        }
127
    }
128
}
129