Test Failed
Push — master ( cf61d2...d4f0dd )
by Robin
02:18
created

BaseClient::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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 9
    public function __construct(ClientInterface $httpClient)
33
    {
34 9
        $this->httpClient = $httpClient;
35 9
    }
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
    public function __call($calledMethod, $args)
46
    {
47
        $camelMethod = Str::camel($calledMethod);
48
        $pascalMethod = Str::studly($calledMethod);
49
50
        if (method_exists($this, $camelMethod)) {
51
            return $this->$camelMethod(...$args);
52
        }
53
54
        foreach ($this->aliases as $realMethod => $aliases) {
55
            if (in_array($calledMethod, (array) $aliases) || in_array($pascalMethod, (array) $aliases)) {
56
                return $this->$realMethod(...$args);
57
            }
58
        }
59
    }
60
61
    /**
62
     * Perform a GET request.
63
     *
64
     * @param string $uri
65
     * @return Response|ResponseInterface
66
     */
67 3
    protected function get($uri)
68
    {
69 3
        return $this->httpClient->request('get', $uri);
70
    }
71
72
    /**
73
     * Perform a HEAD request.
74
     *
75
     * @param string $uri
76
     * @return Response|ResponseInterface
77
     */
78 1
    protected function head($uri)
79
    {
80 1
        return $this->httpClient->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 3
    protected function put($uri, Model $payload = null)
91
    {
92 3
        return $this->httpClient->request('put', $uri,
93 3
            ['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 4
    protected function post($uri, Model $payload = null)
104
    {
105 4
        return $this->httpClient->request('post', $uri,
106 4
            ['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
    protected function postFile($uri, $filename, $contentType = null)
118
    {
119
        $options = ['body' => fopen($filename, 'r')];
120
121
        if ($contentType !== null) {
122
            $options['headers']['Content-Type'] = $contentType;
123
        }
124
125
        return $this->httpClient->request('post', $uri, $options);
126
    }
127
128
    /**
129
     * Perform a DELETE request.
130
     *
131
     * @param string $uri
132
     * @return Response|ResponseInterface
133
     */
134
    protected function delete($uri)
135
    {
136
        return $this->httpClient->request('delete', $uri);
137
    }
138
}
139