Passed
Pull Request — master (#2)
by Andreas
11:41
created

RestGateway   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 62.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 14
c 1
b 0
f 0
dl 0
loc 69
ccs 10
cts 16
cp 0.625
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRestClient() 0 6 1
A getResponse() 0 11 1
A createClient() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Larium\Pay\Gateway;
6
7
use Larium\Pay\Client\RestClient;
8
use Larium\Pay\Response;
9
10
abstract class RestGateway extends Gateway
11
{
12
    /**
13
     * Get the base uri for gateway to make the requests.
14
     *
15
     * @return string
16
     */
17
    abstract protected function getBaseUri(): string;
18
19
    /**
20
     * Authenticate gateway.
21
     * It is common for rest gateways to authenticate with basic auth, bearer
22
     * or custom header authentication.
23
     * So here we can add directly to client the appropriate authentication
24
     * info.
25
     *
26
     * @param RestClient $client
27
     * @return void
28
     */
29
    abstract protected function authenticate(RestClient $client): void;
30
31
    /**
32
     * Returns the RestClient for given resource name.
33
     *
34
     * Resource name is usually the uri path for the resource to call.
35
     * In conjuction with base uri will create the full endpoint uri of
36
     * gateway to request.
37
     *
38
     * @param string $resource
39
     * @return RestClient
40
     */
41
    protected function getRestClient($resource): RestClient
42
    {
43
        $client = $this->createClient($this->getBaseUri(), $resource);
44
        $this->authenticate($client);
45
46
        return $client;
47
    }
48
49
    /**
50
     * Factory method for creating the rest client.
51
     *
52
     * @param string $uri The base uri path of gateway.
53
     * @param string $resource The resource path to request.
54
     * @return RestClient
55
     */
56
    protected function createClient($uri, $resource): RestClient
57
    {
58
        return new RestClient($uri, $resource);
59
    }
60
61
    /**
62
     * Returns the final Response object
63
     *
64
     * @param array $response The response returned from RestClient @see
65
     *                        RestClient::resolveResponse method
66
     * @return \Larium\Pay\Response|mixed
67
     */
68 1
    protected function getResponse(array $response): mixed
69
    {
70 1
        return $this->createResponse(
71 1
            $this->success($response),
72 1
            $this->message($response),
73 1
            $this->transactionId($response),
74 1
            $this->errorCode($response),
75 1
            $this->responseCode($response),
76 1
            $response['body'],
77 1
            $response['raw_response'],
78 1
            $response['raw_request']
79 1
        );
80
    }
81
}
82