Completed
Pull Request — master (#358)
by Simon
04:39
created

PaymentSubscriber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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