Completed
Push — master ( b4c2b2...50fde4 )
by Torben
86:18 queued 41:18
created

PaymentService::paymentActionEnabled()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 23
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 23
rs 8.5906
cc 6
eloc 20
nc 6
nop 2
1
<?php
2
namespace DERHANSEN\SfEventMgt\Service;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use DERHANSEN\SfEventMgt\Domain\Model\Event;
18
use DERHANSEN\SfEventMgt\Payment\AbstractPayment;
19
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22
/**
23
 * PaymentService
24
 *
25
 * @author Torben Hansen <[email protected]>
26
 */
27
class PaymentService
28
{
29
30
    /**
31
     * Returns an array of configured payment methods available for all events
32
     *
33
     * @return array
34
     */
35
    public function getPaymentMethods()
36
    {
37
        $paymentMethods = [];
38
        $configuredPaymentMethods = $this->getConfiguredPaymentMethodConfig();
39
        foreach ($configuredPaymentMethods as $key => $value) {
40
            $paymentMethods[$key] = $this->translate('payment.title.' . $key, $value['extkey']);
41
        }
42
        return $paymentMethods;
43
    }
44
45
    /**
46
     * Translates the given key (required, so translations can be mocked)
47
     *
48
     * @param $key
49
     * @param $extension
50
     * @param null $arguments
51
     * @return NULL|string
52
     */
53
    protected function translate($key, $extension, $arguments = null)
54
    {
55
        return \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($key, $extension, $arguments);
56
    }
57
58
    /**
59
     * Returns an array of payment methods configured in the event
60
     *
61
     * @param Event $event
62
     * @return array
63
     */
64
    public function getRestrictedPaymentMethods($event)
65
    {
66
        $restrictedPaymentMethods = [];
67
        $allPaymentMethods = $this->getPaymentMethods();
68
        $selectedPaymentMethods = explode(',', $event->getSelectedPaymentMethods());
69
        foreach ($selectedPaymentMethods as $selectedPaymentMethod) {
70
            if (isset($allPaymentMethods[$selectedPaymentMethod])) {
71
                $restrictedPaymentMethods[$selectedPaymentMethod] = $allPaymentMethods[$selectedPaymentMethod];
72
            }
73
        }
74
        return $restrictedPaymentMethods;
75
    }
76
77
    /**
78
     * Returns an array of payment method configurations and respects enabled/disabled payment methods from
79
     * the extension configuration
80
     *
81
     * @return array
82
     */
83
    protected function getConfiguredPaymentMethodConfig()
84
    {
85
        $extensionConfiguration = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['sf_event_mgt']);
86
        $allPaymentMethods = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['sf_event_mgt']['paymentMethods'];
87
        if ((bool)$extensionConfiguration['enableInvoice'] === false) {
88
            unset($allPaymentMethods['invoice']);
89
        }
90
        if ((bool)$extensionConfiguration['enableTransfer'] === false) {
91
            unset($allPaymentMethods['transfer']);
92
        }
93
        return $allPaymentMethods;
94
    }
95
96
    /**
97
     * Returns an instance of the given payment method
98
     *
99
     * @param string $paymentMethod
100
     * @return null|AbstractPayment
101
     */
102
    public function getPaymentInstance($paymentMethod)
103
    {
104
        $paymentInstance = null;
105
        $configuredPaymentMethods = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['sf_event_mgt']['paymentMethods'];
106
        if (isset($configuredPaymentMethods[$paymentMethod]) &&
107
            class_exists($configuredPaymentMethods[$paymentMethod]['class'])) {
108
            $paymentInstance = GeneralUtility::makeInstance($configuredPaymentMethods[$paymentMethod]['class']);
109
        }
110
        return $paymentInstance;
111
    }
112
113
    /**
114
     * Returns, if the given action is enabled for the payment method
115
     *
116
     * @param string $paymentMethod
117
     * @param string $action
118
     * @return bool
119
     */
120
    public function paymentActionEnabled($paymentMethod, $action)
121
    {
122
        $result = false;
123
        $paymentInstance = $this->getPaymentInstance($paymentMethod);
124
        switch ($action) {
125
            case 'redirectAction':
126
                $result = $paymentInstance->isRedirectEnabled();
127
                break;
128
            case 'successAction':
129
                $result = $paymentInstance->isSuccessLinkEnabled();
130
                break;
131
            case 'failureAction':
132
                $result = $paymentInstance->isFailureLinkEnabled();
133
                break;
134
            case 'cancelAction':
135
                $result = $paymentInstance->isCancelLinkEnabled();
136
                break;
137
            case 'notifyAction':
138
                $result = $paymentInstance->isNotifyLinkEnabled();
139
                break;
140
        }
141
        return $result;
142
    }
143
144
}