Completed
Pull Request — master (#358)
by Simon
06:06 queued 01:52
created

PaymentSubscriber::extendBackendPayment()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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