Completed
Push — master ( 163cff...0352e7 )
by Andreas
02:22
created

Gateway::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 2
1
<?php
2
3
namespace Larium\Pay\Gateway;
4
5
use Larium\Pay\Response;
6
use Larium\Pay\ParamsBag;
7
use Larium\Pay\GatewayException;
8
use Larium\Pay\Transaction\Query;
9
use Larium\Pay\Transaction\Cancel;
10
use Larium\Pay\Transaction\Refund;
11
use Larium\Pay\Transaction\Capture;
12
use Larium\Pay\Transaction\Initial;
13
use Larium\Pay\Transaction\Transfer;
14
use Larium\Pay\Transaction\Purchase;
15
use Larium\Pay\Transaction\Retrieve;
16
use Larium\Pay\Transaction\Authorize;
17
use Larium\Pay\Transaction\Transaction;
18
use Larium\Pay\Transaction\ThreedSecureAuthenticate;
19
20
abstract class Gateway
21
{
22
    protected $sandbox;
23
24
    protected $options;
25
26
    private $responseCallback;
27
28
    private $transactionToMethod = [
29
        Query::class => 'query',
30
        Cancel::class => 'cancel',
31
        Refund::class => 'refund',
32
        Capture::class => 'capture',
33
        Initial::class => 'initiate',
34
        Transfer::class => 'transfer',
35
        Purchase::class => 'purchase',
36
        Retrieve::class => 'retrieve',
37
        Authorize::class => 'authorize',
38
        Transaction::class => 'transaction',
39
        ThreedSecureAuthenticate::class => 'threedSecureAuthenticate',
40
    ];
41
42
    /**
43
     * Return whether the response is success or not.
44
     *
45
     * $response param contains all the elements of gateway response,
46
     * parsed as associative array, including http status and headers.
47
     *
48
     * @param array $response
49
     * @return bool
50
     */
51
    abstract protected function success(array $response);
52
53
    /**
54
     * Returns the message from gateway response.
55
     *
56
     * $response param contains all the elements of gateway response,
57
     * parsed as associative array, including http status and headers.
58
     *
59
     * @param array $response
60
     * @return string
61
     */
62
    abstract protected function message(array $response);
63
64
    /**
65
     * Returns th unique transaction id from gateway response.
66
     *
67
     * $response param contains all the elements of gateway response,
68
     * parsed as associative array, including http status and headers.
69
     *
70
     * @param array $response
71
     * @return string
72
     */
73
    abstract protected function transactionId(array $response);
74
75
    /**
76
     * Returns error code from gateway if exists.
77
     *
78
     * $response param contains all the elements of gateway response,
79
     * parsed as associative array, including http status and headers.
80
     *
81
     * @param array $response
82
     * @return string|null
83
     */
84
    abstract protected function errorCode(array $response);
85
86
    /**
87
     * Returns response code from card processing, if exists.
88
     * @link https://arch.developer.visa.com/vpp/documents/xml/Request_and_Response.html Example of response codes
89
     *
90
     * $response param contains all the elements of gateway response,
91
     * parsed as associative array, including http status and headers.
92
     *
93
     * @param array $response
94
     * @return string|null
95
     */
96
    abstract protected function responseCode(array $response);
97
98
    final public function __construct(array $options = [])
99
    {
100
        $this->options = new ParamsBag($options);
101
102
        $this->sandbox = $this->options->get('sandbox', false);
103
    }
104
105
    public function execute(
106
        Transaction $transaction,
107
        callable $responseCallback = null
108
    ) {
109
        $transaction->commit();
110
111
        $this->responseCallback = $responseCallback;
112
113
        foreach ($this->transactionToMethod as $class => $method) {
114
            if ($transaction instanceof $class) {
115
                return $this->$method($transaction);
116
            }
117
        }
118
119
        throw new \RuntimeException(
120
            sprintf('Invalid transaction type `%s`', get_class($transaction))
121
        );
122
    }
123
124
    protected function purchase(Purchase $transaction)
0 ignored issues
show
Unused Code introduced by
The parameter $transaction is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
125
    {
126
        throw GatewayException::notImplemented(__FUNCTION__);
127
    }
128
129
    protected function authorize(Authorize $transaction)
0 ignored issues
show
Unused Code introduced by
The parameter $transaction is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
130
    {
131
        throw GatewayException::notImplemented(__FUNCTION__);
132
    }
133
134
    protected function capture(Capture $transaction)
0 ignored issues
show
Unused Code introduced by
The parameter $transaction is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
135
    {
136
        throw GatewayException::notImplemented(__FUNCTION__);
137
    }
138
139
    protected function refund(Refund $transaction)
0 ignored issues
show
Unused Code introduced by
The parameter $transaction is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
140
    {
141
        throw GatewayException::notImplemented(__FUNCTION__);
142
    }
143
144
    protected function cancel(Cancel $transaction)
0 ignored issues
show
Unused Code introduced by
The parameter $transaction is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
145
    {
146
        throw GatewayException::notImplemented(__FUNCTION__);
147
    }
148
149
    protected function retrieve(Retrieve $transaction)
0 ignored issues
show
Unused Code introduced by
The parameter $transaction is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
150
    {
151
        throw GatewayException::notImplemented(__FUNCTION__);
152
    }
153
154
    protected function initiate(Initial $transaction)
0 ignored issues
show
Unused Code introduced by
The parameter $transaction is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
155
    {
156
        throw GatewayException::notImplemented(__FUNCTION__);
157
    }
158
159
    protected function query(Query $transaction)
0 ignored issues
show
Unused Code introduced by
The parameter $transaction is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
160
    {
161
        throw GatewayException::notImplemented(__FUNCTION__);
162
    }
163
164
    protected function threedSecureAuthenticate(ThreedSecureAuthenticate $transaction)
0 ignored issues
show
Unused Code introduced by
The parameter $transaction is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
165
    {
166
        throw GatewayException::notImplemented(__FUNCTION__);
167
    }
168
169
    protected function transfer(Transfer $transaction)
0 ignored issues
show
Unused Code introduced by
The parameter $transaction is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
170
    {
171
        throw GatewayException::notImplemented(__FUNCTION__);
172
    }
173
174
    /**
175
     * Creates and return the response from gateway.
176
     *
177
     * @param bool   $success       Whether response was success or not.
178
     * @param string $message       The message theat describe response status or
179
     *                              reason.
180
     * @param string $transactionId The unique identifier from transaction.
181
     * @param string $errorCode     The error code if transaction was failed.
182
     * @param string $responsecCode The ISO 8583 response code. Not always
0 ignored issues
show
Documentation introduced by
There is no parameter named $responsecCode. Did you maybe mean $responseCode?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
183
     *                              available.
184
     * @param array  $payload       An associative array of the response from
185
     *                              gateway including http status and headers.
186
     * @param string $rawResponse   The raw response of gateway.
187
     * @param string $rawRequest    The raw request to gateway.
188
     *
189
     * @return Larium\Pay\Response|mixed Response may be a user response if $responseCallback
190
     *                                   param is used in execute method
191
     */
192
    protected function createResponse(
193
        $success,
194
        $message,
195
        $transactionId,
196
        $errorCode = '0',
197
        $responseCode = null,
198
        array $payload = null,
199
        $rawResponse = null,
200
        $rawRequest = null
201
    ) {
202
        $response = new Response(
203
            $success,
204
            $message,
205
            $transactionId,
206
            $errorCode,
207
            $responseCode,
208
            $payload,
0 ignored issues
show
Bug introduced by
It seems like $payload defined by parameter $payload on line 198 can also be of type null; however, Larium\Pay\Response::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
209
            $rawResponse,
210
            $rawRequest
211
        );
212
213
        if ($this->responseCallback) {
214
            return call_user_func_array(
215
                $this->responseCallback,
216
                [
217
                    $response,
218
                    $payload
219
                ]
220
            );
221
        }
222
223
        return $response;
224
    }
225
}
226