Passed
Push — main ( 52a7ec...5410b3 )
by Torben
03:32
created

PaymentService::paymentActionEnabled()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 7
eloc 21
c 4
b 0
f 0
nc 7
nop 2
dl 0
loc 27
rs 8.6506
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace DERHANSEN\SfEventMgt\Service;
13
14
use DERHANSEN\SfEventMgt\Domain\Model\Event;
15
use DERHANSEN\SfEventMgt\Payment\AbstractPayment;
16
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Utility\LocalizationUtility was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
20
class PaymentService
21
{
22
    /**
23
     * Returns an array of configured payment methods available for all events
24
     */
25
    public function getPaymentMethods(): array
26
    {
27
        $paymentMethods = [];
28
        $configuredPaymentMethods = $this->getConfiguredPaymentMethodConfig();
29
        foreach ($configuredPaymentMethods as $key => $value) {
30
            $paymentMethods[$key] = $this->translate('payment.title.' . $key, $value['extkey']);
31
        }
32
33
        return $paymentMethods;
34
    }
35
36
    /**
37
     * Translates the given key (required, so translations can be mocked)
38
     */
39
    protected function translate(string $key, string $extension, ?array $arguments = null): ?string
40
    {
41
        return LocalizationUtility::translate($key, $extension, $arguments);
42
    }
43
44
    /**
45
     * Returns an array of payment methods configured in the event
46
     */
47
    public function getRestrictedPaymentMethods(Event $event): array
48
    {
49
        $restrictedPaymentMethods = [];
50
        $allPaymentMethods = $this->getPaymentMethods();
51
        $selectedPaymentMethods = explode(',', $event->getSelectedPaymentMethods());
52
        foreach ($selectedPaymentMethods as $selectedPaymentMethod) {
53
            if (isset($allPaymentMethods[$selectedPaymentMethod])) {
54
                $restrictedPaymentMethods[$selectedPaymentMethod] = $allPaymentMethods[$selectedPaymentMethod];
55
            }
56
        }
57
58
        return $restrictedPaymentMethods;
59
    }
60
61
    /**
62
     * Returns an array of payment method configurations and respects enabled/disabled payment methods from
63
     * the extension configuration
64
     */
65
    protected function getConfiguredPaymentMethodConfig(): array
66
    {
67
        $extensionConfiguration = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('sf_event_mgt');
68
        $allPaymentMethods = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['sf_event_mgt']['paymentMethods'] ?? [];
69
        if ((bool)($extensionConfiguration['enableInvoice'] ?? false) === false) {
70
            unset($allPaymentMethods['invoice']);
71
        }
72
        if ((bool)($extensionConfiguration['enableTransfer'] ?? false) === false) {
73
            unset($allPaymentMethods['transfer']);
74
        }
75
76
        return $allPaymentMethods;
77
    }
78
79
    /**
80
     * Returns an instance of the given payment method
81
     */
82
    public function getPaymentInstance(string $paymentMethod): ?AbstractPayment
83
    {
84
        $paymentInstance = null;
85
        $configuredPaymentMethods = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['sf_event_mgt']['paymentMethods'] ?? [];
86
        if (isset($configuredPaymentMethods[$paymentMethod]) &&
87
            class_exists($configuredPaymentMethods[$paymentMethod]['class'] ?? '')) {
88
            /** @var AbstractPayment $paymentInstance */
89
            $paymentInstance = GeneralUtility::makeInstance($configuredPaymentMethods[$paymentMethod]['class']);
90
        }
91
92
        return $paymentInstance;
93
    }
94
95
    /**
96
     * Returns, if the given action is enabled for the payment method
97
     */
98
    public function paymentActionEnabled(string $paymentMethod, string $action): bool
99
    {
100
        $result = false;
101
        $paymentInstance = $this->getPaymentInstance($paymentMethod);
102
        if (!$paymentInstance) {
103
            return false;
104
        }
105
106
        switch ($action) {
107
            case 'redirectAction':
108
                $result = $paymentInstance->isRedirectEnabled();
109
                break;
110
            case 'successAction':
111
                $result = $paymentInstance->isSuccessLinkEnabled();
112
                break;
113
            case 'failureAction':
114
                $result = $paymentInstance->isFailureLinkEnabled();
115
                break;
116
            case 'cancelAction':
117
                $result = $paymentInstance->isCancelLinkEnabled();
118
                break;
119
            case 'notifyAction':
120
                $result = $paymentInstance->isNotifyLinkEnabled();
121
                break;
122
        }
123
124
        return $result;
125
    }
126
}
127