Completed
Push — master ( c8fc06...2d35c9 )
by Andreas
02:07
created

Gateway::createResponse()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 33
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2.1017

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 12
cts 17
cp 0.7059
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 24
nc 2
nop 8
crap 2.1017

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
        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 7
    final public function __construct(array $options = [])
99
    {
100 7
        $this->options = new ParamsBag($options);
101
102 7
        $this->sandbox = $this->options->get('sandbox', false);
103 7
    }
104
105 6
    public function execute(
106
        Transaction $transaction,
107
        callable $responseCallback = null
108
    ) {
109 6
        $transaction->commit();
110
111 6
        $this->responseCallback = $responseCallback;
112
113 6
        foreach ($this->transactionToMethod as $class => $method) {
114 6
            if ($transaction instanceof $class) {
115 6
                return $this->$method($transaction);
116
            }
117 5
        }
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 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...
140
    {
141 1
        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 $responseCode  The ISO 8583 response code. Not always
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 1
    protected function createResponse(
193
        $success,
194
        $message,
195
        $transactionId,
196
        $errorCode = '0',
197
        $responseCode = null,
198
        array $payload = [],
199
        $rawResponse = null,
200
        $rawRequest = null
201
    ) {
202 1
        $response = new Response(
203 1
            $success,
204 1
            $message,
205 1
            $transactionId,
206 1
            $errorCode,
207 1
            $responseCode,
208 1
            $payload,
209 1
            $rawResponse,
210
            $rawRequest
211 1
        );
212
213 1
        if ($this->responseCallback) {
214
            return call_user_func_array(
215
                $this->responseCallback,
216
                [
217
                    $response,
218
                    $payload
219
                ]
220
            );
221
        }
222
223 1
        return $response;
224
    }
225
}
226