Passed
Push — master ( da5c3a...138f6a )
by Torben
132:09 queued 128:49
created

PaymentService::translate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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