PaymentGatewayField::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
/**
3
 * PaymentGatewayField.php
4
 *
5
 * @author Bram de Leeuw
6
 * Date: 10/03/17
7
 */
8
9
namespace Broarm\EventTickets;
10
11
use FieldGroup;
12
use FieldList;
13
use HiddenField;
14
use LiteralField;
15
use OptionsetField;
16
use SilverStripe\Omnipay\GatewayInfo;
17
18
/**
19
 * Class PaymentGatewayField
20
 *
21
 * @package Broarm\EventTickets
22
 */
23
class PaymentGatewayField extends FieldGroup
24
{
25
    /**
26
     * Construct the payment gateway select
27
     * This could render the following
28
     * 1. Selectable gateways as an option set
29
     * 2. Hidden field with the only configured gateway
30
     * 3. Message when no gateway is configured
31
     *
32
     * PaymentGatewayField constructor.
33
     */
34
    public function __construct()
35
    {
36
        parent::__construct();
37
        $children = FieldList::create();
38
39
        if ($gateways = GatewayInfo::getSupportedGateways(true)) {
40
            $this->extend('updateGateways', $gateways);
41
42
            if (count($gateways) > 1) {
43
                $children->add(OptionsetField::create(
44
                    'Gateway',
45
                    _t('PaymentGatewayField.SELECT_GATEWAY', 'Select a gateway'),
46
                    $gateways
47
                )->setValue(key($gateways)));
48
            } else {
49
                $children->add(HiddenField::create('Gateway', 'Gateway', key($gateways)));
50
            }
51
        } else {
52
            $children->add(
53
                LiteralField::create('NoGateway', "<p>No gateways configured</p>")
54
            );
55
        }
56
57
        $this->extend('updateGatewayField');
58
        $this->setChildren($children);
59
    }
60
}