Passed
Pull Request — master (#2)
by Andreas
01:34
created

Gateway::createResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 2.0005

Importance

Changes 0
Metric Value
eloc 15
c 0
b 0
f 0
dl 0
loc 32
ccs 19
cts 20
cp 0.95
rs 9.7666
cc 2
nc 2
nop 8
crap 2.0005

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