Passed
Push — master ( d4f0dd...ebfa4f )
by Robin
05:37
created

BaseClient   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 140
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __call() 0 15 5
A get() 0 4 1
A head() 0 4 1
A put() 0 5 2
A post() 0 5 2
A postFile() 0 10 2
A delete() 0 4 1
A request() 0 4 1
1
<?php
2
3
namespace Konsulting\JustGivingApiSdk\ResourceClients;
4
5
use GuzzleHttp\ClientInterface;
6
use Illuminate\Support\Str;
7
use Konsulting\JustGivingApiSdk\ResourceClients\Models\Model;
8
use Konsulting\JustGivingApiSdk\Support\Response;
9
use Psr\Http\Message\ResponseInterface;
10
11
abstract class BaseClient
12
{
13
    /**
14
     * Method name aliases.
15
     *
16
     * @var array
17
     */
18
    protected $aliases = [];
19
20
    /**
21
     * The HTTP client used to perform requests.
22
     *
23
     * @var ClientInterface;
24
     */
25
    public $httpClient;
26
27
    /**
28
     * ClientBase constructor.
29
     *
30
     * @param ClientInterface $httpClient
31
     */
32 74
    public function __construct(ClientInterface $httpClient)
33
    {
34 74
        $this->httpClient = $httpClient;
35 74
    }
36
37
    /**
38
     * Check if the called method is valid if it's converted to camel case. If not, look for a defined method alias
39
     * that's either an exact match, or the Pascal-cased version of the called method.
40
     *
41
     * @param $calledMethod
42
     * @param $args
43
     * @return mixed
44
     */
45 4
    public function __call($calledMethod, $args)
46
    {
47 4
        $camelMethod = Str::camel($calledMethod);
48 4
        $pascalMethod = Str::studly($calledMethod);
49
50 4
        if (method_exists($this, $camelMethod)) {
51 1
            return $this->$camelMethod(...$args);
52
        }
53
54 3
        foreach ($this->aliases as $realMethod => $aliases) {
55 3
            if (in_array($calledMethod, (array) $aliases) || in_array($pascalMethod, (array) $aliases)) {
56 3
                return $this->$realMethod(...$args);
57
            }
58
        }
59 1
    }
60
61
    /**
62
     * Perform a GET request.
63
     *
64
     * @param string $uri
65
     * @return Response|ResponseInterface
66
     */
67 44
    protected function get($uri)
68
    {
69 44
        return $this->request('get', $uri);
70
    }
71
72
    /**
73
     * Perform a HEAD request.
74
     *
75
     * @param string $uri
76
     * @return Response|ResponseInterface
77
     */
78 4
    protected function head($uri)
79
    {
80 4
        return $this->request('head', $uri);
81
    }
82
83
    /**
84
     * Perform a PUT request.
85
     *
86
     * @param       $uri
87
     * @param Model $payload
88
     * @return Response|ResponseInterface
89
     */
90 19
    protected function put($uri, Model $payload = null)
91
    {
92 19
        return $this->request('put', $uri,
93 19
            ['json' => isset($payload) ? $payload->getAttributes() : '']);
94
    }
95
96
    /**
97
     * Perform a POST request.
98
     *
99
     * @param       $uri
100
     * @param Model $payload
101
     * @return Response|ResponseInterface
102
     */
103 12
    protected function post($uri, Model $payload = null)
104
    {
105 12
        return $this->request('post', $uri,
106 12
            ['json' => isset($payload) ? $payload->getAttributes() : '']);
107
    }
108
109
    /**
110
     * Perform a POST request with data from a file sent as the request body.
111
     *
112
     * @param string $uri
113
     * @param string $filename
114
     * @param string $contentType
115
     * @return Response|ResponseInterface
116
     */
117 3
    protected function postFile($uri, $filename, $contentType = null)
118
    {
119 3
        $options = ['body' => fopen($filename, 'r')];
120
121 3
        if ($contentType !== null) {
122 1
            $options['headers']['Content-Type'] = $contentType;
123
        }
124
125 3
        return $this->request('post', $uri, $options);
126
    }
127
128
    /**
129
     * Perform a DELETE request.
130
     *
131
     * @param string $uri
132
     * @return Response|ResponseInterface
133
     */
134 4
    protected function delete($uri)
135
    {
136 4
        return $this->request('delete', $uri);
137
    }
138
139
    /**
140
     * Perform a request on the HTTP client.
141
     *
142
     * @param string $method
143
     * @param string $uri
144
     * @param array  $options
145
     */
146 71
    private function request($method, $uri, $options = [])
147
    {
148 71
        return $this->httpClient->request($method, $uri, $options);
149
    }
150
}
151