Completed
Push — payment ( 3621c5...bc96a4 )
by Torben
41:08
created

PaymentService::getConfiguredPaymentMethodConfig()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 3
eloc 8
nc 4
nop 0
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\Payment\AbstractPayment;
18
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21
/**
22
 * PaymentService
23
 *
24
 * @author Torben Hansen <[email protected]>
25
 */
26
class PaymentService
27
{
28
29
    /**
30
     * Returns an array of configured payment methods available for all events
31
     *
32
     * @return array
33
     */
34
    public function getPaymentMethods()
35
    {
36
        $paymentMethods = [];
37
        $configuredPaymentMethods = $this->getConfiguredPaymentMethodConfig();
38
        foreach ($configuredPaymentMethods as $key => $value) {
39
            $paymentMethods[$key] = LocalizationUtility::translate('payment.title.' . $key, $value['extkey']);
40
        }
41
        return $paymentMethods;
42
    }
43
44
    /**
45
     * Returns an array of payment method configurations and respects enabled/disabled payment methods from
46
     * the extension configuration
47
     *
48
     * @return array
49
     */
50
    protected function getConfiguredPaymentMethodConfig()
51
    {
52
        $extensionConfiguration = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['sf_event_mgt']);
53
        $allPaymentMethods = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['sf_event_mgt']['paymentMethods'];
54
        if ((bool)$extensionConfiguration['enableInvoice'] === false) {
55
            unset($allPaymentMethods['invoice']);
56
        }
57
        if ((bool)$extensionConfiguration['enableTransfer'] === false) {
58
            unset($allPaymentMethods['transfer']);
59
        }
60
        return $allPaymentMethods;
61
    }
62
63
    /**
64
     * Returns an instance of the given payment method
65
     *
66
     * @param string $paymentMethod
67
     * @return null|AbstractPayment
68
     */
69
    public function getPaymentInstance($paymentMethod)
70
    {
71
        $paymentInstance = null;
72
        $configuredPaymentMethods = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['sf_event_mgt']['paymentMethods'];
73
        if (isset($configuredPaymentMethods[$paymentMethod]) &&
74
            class_exists($configuredPaymentMethods[$paymentMethod]['class'])) {
75
            $paymentInstance = GeneralUtility::makeInstance($configuredPaymentMethods[$paymentMethod]['class']);
76
        }
77
        return $paymentInstance;
78
    }
79
80
    /**
81
     * Returns, if the given action is enabled for the payment method
82
     *
83
     * @param string $paymentMethod
84
     * @param string $action
85
     * @return bool
86
     */
87
    public function paymentActionEnabled($paymentMethod, $action)
88
    {
89
        $result = false;
90
        $paymentInstance = $this->getPaymentInstance($paymentMethod);
91
        switch ($action) {
92
            case 'redirectAction':
93
                $result = $paymentInstance->isRedirectEnabled();
94
                break;
95
            case 'successAction':
96
                $result = $paymentInstance->isSuccessLinkEnabled();
97
                break;
98
            case 'failureAction':
99
                $result = $paymentInstance->isFailureLinkEnabled();
100
                break;
101
            case 'cancelAction':
102
                $result = $paymentInstance->isCancelLinkEnabled();
103
                break;
104
            case 'notifyAction':
105
                $result = $paymentInstance->isNotifyLinkEnabled();
106
                break;
107
        }
108
        return $result;
109
    }
110
111
}