Completed
Pull Request — master (#358)
by Stefan
30:38 queued 25:29
created

PaymentSubscriber   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSubscribedEvents() 0 7 1
A getPaymentService() 0 8 2
B extendBackendPayment() 0 31 4
C onFilterPaymentMethods() 0 32 7
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
use Shopware\Models\Payment\Repository as PaymentRepository;
13
14
class PaymentSubscriber implements SubscriberInterface
15
{
16
    /**
17
     * @var \ShopwarePlugins\Connect\Services\PaymentService
18
     */
19
    private $paymentService;
20
21
    /**
22
     * @var Helper
23
     */
24
    private $helper;
25
26
    /**
27
     * @var PaymentRepository
28
     */
29
    private $paymentRepository;
30
31
    /**
32
     * @param Helper $helper
33
     * @param PaymentRepository $paymentRepository
34
     */
35
    public function __construct(Helper $helper, PaymentRepository $paymentRepository)
36
    {
37
        $this->helper = $helper;
38
        $this->paymentRepository = $paymentRepository;
39
    }
40
41
    /**
42
     * @return array
43
     */
44
    public static function getSubscribedEvents()
45
    {
46
        return [
47
            'Enlight_Controller_Action_PostDispatch_Backend_Payment' => 'extendBackendPayment',
48
            'Shopware_Modules_Admin_GetPaymentMeans_DataFilter' => 'onFilterPaymentMethods',
49
        ];
50
    }
51
52
    /**
53
     * @return mixed
54
     */
55
    public function getPaymentService()
56
    {
57
        if ($this->paymentService == null) {
58
            $this->paymentService = Shopware()->Container()->get('swagconnect.payment_service');
59
        }
60
61
        return $this->paymentService;
62
    }
63
64
    /**
65
     * @param \Enlight_Event_EventArgs $args
66
     */
67
    public function extendBackendPayment(\Enlight_Event_EventArgs $args)
68
    {
69
        /** @var $subject \Enlight_Controller_Action */
70
        $subject = $args->getSubject();
71
        $request = $subject->Request();
72
73
        switch ($request->getActionName()) {
74
            case 'load':
75
                $subject->View()->extendsTemplate(
76
                    'backend/payment/model/connect_attribute.js'
77
                );
78
79
                $subject->View()->extendsTemplate(
80
                    'backend/payment/view/payment/connect_form.js'
81
                );
82
                break;
83
            case 'getPayments':
84
                $subject->View()->data = $this->getPaymentService()->allowConnect(
85
                    $subject->View()->data
86
                );
87
                break;
88
            case 'updatePayments':
89
                $paymentId = (int) $request->getParam('id', null);
90
                $isAllowed = (bool) $request->getParam('connectIsAllowed', false);
91
92
                $this->getPaymentService()->updateConnectAllowed($paymentId, $isAllowed);
93
                break;
94
            default:
95
                break;
96
        }
97
    }
98
99
    /**
100
     * @param \Enlight_Event_EventArgs $args
101
     */
102
    public function onFilterPaymentMethods(\Enlight_Event_EventArgs $args)
103
    {
104
        $paymentMeans = $args->getReturn();
105
106
        $sessionId = Shopware()->SessionID();
107
        $hasConnectProduct = $this->helper->hasBasketConnectProducts($sessionId);
108
109
        if ($hasConnectProduct === true) {
110
            foreach ($paymentMeans as $key => &$payment) {
111
                /** @var \Shopware\Models\Payment\Payment $payment */
112
                $payment = $this->paymentRepository->find($payment['id']);
113
                if (!$payment) {
114
                    unset($paymentMeans[$key]);
115
                    continue;
116
                }
117
118
                if (!$payment->getAttribute()) {
119
                    unset($paymentMeans[$key]);
120
                    continue;
121
                }
122
123
                $attribute = $payment->getAttribute();
124
                if (method_exists($attribute, 'getConnectIsAllowed') === true
125
                    && $attribute->getConnectIsAllowed() == 0) {
126
                    unset($paymentMeans[$key]);
127
                    continue;
128
                }
129
            }
130
131
            $args->setReturn($paymentMeans);
132
        }
133
    }
134
}
135