Completed
Push — master ( 60aec0...f632bc )
by Andreas
12:47 queued 10:41
created

Gateway::createResponse()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 33
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 4
cts 4
cp 1
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 24
nc 2
nop 8
crap 2

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
        ThreedSecureAuthenticate::class => 'threedSecureAuthenticate',
39
    ];
40
41
    /**
42
     * Return whether the response is success or not.
43
     *
44
     * $response param contains all the elements of gateway response,
45
     * parsed as associative array, including http status and headers.
46
     *
47
     * @param array $response
48
     * @return bool
49
     */
50
    abstract protected function success(array $response);
51
52
    /**
53
     * Returns the message from gateway response.
54
     *
55
     * $response param contains all the elements of gateway response,
56
     * parsed as associative array, including http status and headers.
57
     *
58
     * @param array $response
59
     * @return string
60
     */
61
    abstract protected function message(array $response);
62
63
    /**
64
     * Returns th unique transaction id from gateway response.
65
     *
66
     * $response param contains all the elements of gateway response,
67
     * parsed as associative array, including http status and headers.
68
     *
69
     * @param array $response
70
     * @return string
71
     */
72
    abstract protected function transactionId(array $response);
73
74
    /**
75
     * Returns error code from gateway if exists.
76
     *
77
     * $response param contains all the elements of gateway response,
78
     * parsed as associative array, including http status and headers.
79
     *
80
     * @param array $response
81
     * @return string|null
82
     */
83
    abstract protected function errorCode(array $response);
84
85
    /**
86
     * Returns response code from card processing, if exists.
87
     * @link https://arch.developer.visa.com/vpp/documents/xml/Request_and_Response.html Example of response codes
88
     *
89
     * $response param contains all the elements of gateway response,
90
     * parsed as associative array, including http status and headers.
91
     *
92
     * @param array $response
93
     * @return string|null
94
     */
95
    abstract protected function responseCode(array $response);
96
97
    final public function __construct(array $options = [])
98
    {
99
        $this->options = new ParamsBag($options);
100
101
        $this->sandbox = $this->options->get('sandbox', false);
102
    }
103
104 6
    public function execute(
105
        Transaction $transaction,
106 6
        callable $responseCallback = null
107
    ) {
108
        $transaction->commit();
109
110 6
        $this->responseCallback = $responseCallback;
111
112 6
        foreach ($this->transactionToMethod as $class => $method) {
113 1
            if ($transaction instanceof $class) {
114 1
                return $this->$method($transaction);
115
            }
116 1
        }
117
118
        throw new \RuntimeException(
119
            sprintf('Invalid transaction type `%s`', get_class($transaction))
120
        );
121 5
    }
122
123
    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...
124
    {
125
        throw GatewayException::notImplemented(__FUNCTION__);
126
    }
127
128
    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...
129
    {
130
        throw GatewayException::notImplemented(__FUNCTION__);
131
    }
132
133
    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...
134
    {
135
        throw GatewayException::notImplemented(__FUNCTION__);
136
    }
137
138 1
    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...
139
    {
140
        throw GatewayException::notImplemented(__FUNCTION__);
141 1
    }
142
143
    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...
144
    {
145
        throw GatewayException::notImplemented(__FUNCTION__);
146
    }
147
148
    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...
149
    {
150
        throw GatewayException::notImplemented(__FUNCTION__);
151
    }
152
153
    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...
154
    {
155
        throw GatewayException::notImplemented(__FUNCTION__);
156
    }
157
158
    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...
159
    {
160
        throw GatewayException::notImplemented(__FUNCTION__);
161
    }
162
163
    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...
164
    {
165
        throw GatewayException::notImplemented(__FUNCTION__);
166
    }
167
168
    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...
169
    {
170
        throw GatewayException::notImplemented(__FUNCTION__);
171
    }
172
173
    /**
174
     * Creates and return the response from gateway.
175
     *
176
     * @param bool   $success       Whether response was success or not.
177
     * @param string $message       The message theat describe response status or
178
     *                              reason.
179
     * @param string $transactionId The unique identifier from transaction.
180
     * @param string $errorCode     The error code if transaction was failed.
181
     * @param string $responseCode  The ISO 8583 response code. Not always
182
     *                              available.
183
     * @param array  $payload       An associative array of the response from
184
     *                              gateway including http status and headers.
185
     * @param string $rawResponse   The raw response of gateway.
186
     * @param string $rawRequest    The raw request to gateway.
187
     *
188
     * @return Larium\Pay\Response|mixed Response may be a user response if $responseCallback
189
     *                                   param is used in execute method
190
     */
191 1
    protected function createResponse(
192
        $success,
193
        $message,
194
        $transactionId,
195
        $errorCode = '0',
196
        $responseCode = null,
197
        array $payload = [],
198
        $rawResponse = null,
199
        $rawRequest = null
200
    ) {
201
        $response = new Response(
202
            $success,
203
            $message,
204
            $transactionId,
205
            $errorCode,
206
            $responseCode,
207
            $payload,
208
            $rawResponse,
209
            $rawRequest
210
        );
211
212 1
        if ($this->responseCallback) {
213
            return call_user_func_array(
214
                $this->responseCallback,
215
                [
216
                    $response,
217
                    $payload
218
                ]
219
            );
220
        }
221
222 1
        return $response;
223 1
    }
224
}
225