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

PaymentSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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