PaymentProcessor::setGatewayData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * PaymentProcessor.php
4
 *
5
 * @author Bram de Leeuw
6
 * Date: 24/03/17
7
 */
8
9
namespace Broarm\EventTickets;
10
11
use SS_Object;
12
use Payment;
13
use SilverStripe\Omnipay\Exception\Exception;
14
use SilverStripe\Omnipay\GatewayInfo;
15
use SilverStripe\Omnipay\Service\ServiceFactory;
16
use SilverStripe\Omnipay\Service\ServiceResponse;
17
18
/**
19
 * Class PaymentProcessor
20
 *
21
 * @package Broarm\EventTickets
22
 */
23
class PaymentProcessor extends SS_Object
24
{
25
    /**
26
     * @config
27
     * @var string
28
     */
29
    private static $currency = 'EUR';
30
31
    /**
32
     * @var Reservation
33
     */
34
    protected $reservation;
35
36
    /**
37
     * @var Payment
38
     */
39
    protected $payment;
40
41
    /**
42
     * @var array
43
     */
44
    protected $gatewayData = array(
45
        'transactionId' => null,
46
        'firstName' => null,
47
        'lastName' => null,
48
        'email' => null,
49
        'company' => null,
50
        'billingAddress1' => null,
51
        'billingAddress2' => null,
52
        'billingCity' => null,
53
        'billingPostcode' => null,
54
        'billingState' => null,
55
        'billingCountry' => null,
56
        'billingPhone' => null,
57
        'shippingAddress1' => null,
58
        'shippingAddress2' => null,
59
        'shippingCity' => null,
60
        'shippingPostcode' => null,
61
        'shippingState' => null,
62
        'shippingCountry' => null,
63
        'shippingPhone' => null,
64
        'description' => null
65
    );
66
67
    /**
68
     * PaymentProcessor constructor.
69
     *
70
     * @param Reservation $reservation
71
     */
72
    public function __construct(Reservation $reservation)
73
    {
74
        $this->reservation = $reservation;
75
        $this->setGatewayData(array(
76
            'transactionId' => $reservation->Status,
77
            'firstName' => $reservation->MainContact()->FirstName,
0 ignored issues
show
Documentation introduced by
The property FirstName does not exist on object<Broarm\EventTickets\Attendee>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
78
            'lastName' => $reservation->MainContact()->Surname,
0 ignored issues
show
Documentation introduced by
The property Surname does not exist on object<Broarm\EventTickets\Attendee>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
79
            'email' => $reservation->MainContact()->Email,
0 ignored issues
show
Documentation introduced by
The property Email does not exist on object<Broarm\EventTickets\Attendee>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
80
            'description' => $reservation->ReservationCode
81
        ));
82
83
        parent::__construct();
84
        $this->extend('updatePaymentProcessor');
85
    }
86
87
    /**
88
     * Create a payment trough the given payment gateway
89
     *
90
     * @param string $gateway
91
     *
92
     * @return Payment
93
     */
94
    public function createPayment($gateway)
95
    {
96
        if (!GatewayInfo::isSupported($gateway)) {
97
            user_error(_t(
98
                "PaymentProcessor.INVALID_GATEWAY",
99
                "`{gateway}` is not supported.",
100
                null,
101
                array('gateway' => (string)$gateway)
0 ignored issues
show
Documentation introduced by
array('gateway' => (string) $gateway) is of type array<string,string,{"gateway":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
102
            ), E_USER_ERROR);
103
        }
104
105
        // Create a payment
106
        $this->payment = Payment::create()->init(
107
            $gateway,
108
            $this->reservation->Total,
109
            self::config()->get('currency')
110
        );
111
112
        // Set a reference to the reservation
113
        $this->payment->ReservationID = $this->reservation->ID;
114
115
        return $this->payment;
116
    }
117
118
    /**
119
     * Create the service factory
120
     * Catch any exceptions that might occur
121
     *
122
     * @return null|ServiceResponse
123
     */
124
    public function createServiceFactory()
125
    {
126
        $factory = ServiceFactory::create();
127
        $service = $factory->getService($this->payment, ServiceFactory::INTENT_PAYMENT);
128
129
        try {
130
            $serviceResponse = $service->initiate($this->getGatewayData());
131
        } catch (Exception $ex) {
132
            // error out when an exception occurs
133
            user_error($ex->getMessage(), E_USER_WARNING);
134
            return null;
135
        }
136
137
        return $serviceResponse;
138
    }
139
140
    /**
141
     * Set and merges the gateway data
142
     *
143
     * @param array $data
144
     */
145
    public function setGatewayData($data = array())
146
    {
147
        $this->gatewayData = array_merge($this->gatewayData, $data);
148
    }
149
150
    /**
151
     * Get the gateway data
152
     *
153
     * @return array
154
     */
155
    public function getGateWayData()
156
    {
157
        return $this->gatewayData;
158
    }
159
160
    /**
161
     * Get the reservation
162
     *
163
     * @return Reservation
164
     */
165
    public function getReservation()
166
    {
167
        return $this->reservation;
168
    }
169
}
170