Completed
Push — master ( 1994ca...a6fb77 )
by Andreas
03:32
created

RestGateway   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 2
cbo 2
dl 0
loc 72
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

5 Methods

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