Completed
Push — payment ( bc96a4...9605b6 )
by Torben
45:21
created

PaymentService::getRestrictedPaymentMethods()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
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] = LocalizationUtility::translate('payment.title.' . $key, $value['extkey']);
41
        }
42
        return $paymentMethods;
43
    }
44
45
    /**
46
     * Returns an array of payment methods configured in the event
47
     *
48
     * @param Event $event
49
     * @return array
50
     */
51
    public function getRestrictedPaymentMethods($event)
52
    {
53
        $restrictedPaymentMethods = [];
54
        $allPaymentMethods = $this->getPaymentMethods();
55
        $selectedPaymentMethods = explode(',', $event->getSelectedPaymentMethods());
56
        foreach ($selectedPaymentMethods as $selectedPaymentMethod) {
57
            if (isset($allPaymentMethods[$selectedPaymentMethod])) {
58
                $restrictedPaymentMethods[$selectedPaymentMethod] = $allPaymentMethods[$selectedPaymentMethod];
59
            }
60
        }
61
        return $restrictedPaymentMethods;
62
    }
63
64
    /**
65
     * Returns an array of payment method configurations and respects enabled/disabled payment methods from
66
     * the extension configuration
67
     *
68
     * @return array
69
     */
70
    protected function getConfiguredPaymentMethodConfig()
71
    {
72
        $extensionConfiguration = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['sf_event_mgt']);
73
        $allPaymentMethods = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['sf_event_mgt']['paymentMethods'];
74
        if ((bool)$extensionConfiguration['enableInvoice'] === false) {
75
            unset($allPaymentMethods['invoice']);
76
        }
77
        if ((bool)$extensionConfiguration['enableTransfer'] === false) {
78
            unset($allPaymentMethods['transfer']);
79
        }
80
        return $allPaymentMethods;
81
    }
82
83
    /**
84
     * Returns an instance of the given payment method
85
     *
86
     * @param string $paymentMethod
87
     * @return null|AbstractPayment
88
     */
89
    public function getPaymentInstance($paymentMethod)
90
    {
91
        $paymentInstance = null;
92
        $configuredPaymentMethods = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['sf_event_mgt']['paymentMethods'];
93
        if (isset($configuredPaymentMethods[$paymentMethod]) &&
94
            class_exists($configuredPaymentMethods[$paymentMethod]['class'])) {
95
            $paymentInstance = GeneralUtility::makeInstance($configuredPaymentMethods[$paymentMethod]['class']);
96
        }
97
        return $paymentInstance;
98
    }
99
100
    /**
101
     * Returns, if the given action is enabled for the payment method
102
     *
103
     * @param string $paymentMethod
104
     * @param string $action
105
     * @return bool
106
     */
107
    public function paymentActionEnabled($paymentMethod, $action)
108
    {
109
        $result = false;
110
        $paymentInstance = $this->getPaymentInstance($paymentMethod);
111
        switch ($action) {
112
            case 'redirectAction':
113
                $result = $paymentInstance->isRedirectEnabled();
114
                break;
115
            case 'successAction':
116
                $result = $paymentInstance->isSuccessLinkEnabled();
117
                break;
118
            case 'failureAction':
119
                $result = $paymentInstance->isFailureLinkEnabled();
120
                break;
121
            case 'cancelAction':
122
                $result = $paymentInstance->isCancelLinkEnabled();
123
                break;
124
            case 'notifyAction':
125
                $result = $paymentInstance->isNotifyLinkEnabled();
126
                break;
127
        }
128
        return $result;
129
    }
130
131
}