Test Failed
Push — feature/allow-array-payloads ( a6299b )
by Robin
16:19
created

BaseClient::getPayloadAttributes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Konsulting\JustGivingApiSdk\ResourceClients;
4
5
use Illuminate\Support\Str;
6
use Konsulting\JustGivingApiSdk\JustGivingClient;
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 JustGiving client used to perform requests.
22
     *
23
     * @var JustGivingClient;
24
     */
25
    private $client;
26
27
    /**
28
     * ClientBase constructor.
29
     *
30
     * @param JustGivingClient $client
31
     */
32
    public function __construct(JustGivingClient $client)
33
    {
34
        $this->client = $client;
35
    }
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
    protected function get($uri)
68
    {
69
        return $this->request('get', $uri);
70
    }
71
72
    /**
73
     * Perform a HEAD request.
74
     *
75
     * @param string $uri
76
     * @return Response|ResponseInterface
77
     */
78
    protected function head($uri)
79
    {
80
        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
    protected function put($uri, Model $payload = null)
91
    {
92
        return $this->request('put', $uri,
93
            ['json' => isset($payload) ? $payload->getAttributes() : '']);
94
    }
95
96
    /**
97
     * Perform a POST request.
98
     *
99
     * @param       $uri
100
     * @param Model|array $payload
101
     * @return Response|ResponseInterface
102
     */
103
    protected function post($uri, $payload = null)
104
    {
105
        return $this->request('post', $uri,
106
            ['json' => $this->getPayloadAttributes($payload)]);
107
    }
108
109
    /**
110
     * Get the payload as an array.
111
     *
112
     * @param Model|array|null $payload
113
     * @return array
114
     */
115
    private function getPayloadAttributes($payload)
116
    {
117
        if ($payload instanceof Model) {
118
            return $payload->getAttributes();
119
        }
120
121
        return is_array($payload) ? $payload : [];
122
    }
123
124
    /**
125
     * Perform a POST request with data from a file sent as the request body.
126
     *
127
     * @param string $uri
128
     * @param string $filename
129
     * @param string $contentType
130
     * @return Response|ResponseInterface
131
     */
132
    protected function postFile($uri, $filename, $contentType = null)
133
    {
134
        $options = ['body' => fopen($filename, 'r')];
135
136
        if ($contentType !== null) {
137
            $options['headers']['Content-Type'] = $contentType;
138
        }
139
140
        return $this->request('post', $uri, $options);
141
    }
142
143
    /**
144
     * Perform a DELETE request.
145
     *
146
     * @param string $uri
147
     * @return Response|ResponseInterface
148
     */
149
    protected function delete($uri)
150
    {
151
        return $this->request('delete', $uri);
152
    }
153
154
    /**
155
     * Perform a request on the HTTP client.
156
     *
157
     * @param string $method
158
     * @param string $uri
159
     * @param array  $options
160
     */
161
    private function request($method, $uri, $options = [])
162
    {
163
        return $this->client->request($method, $uri, $options);
164
    }
165
}
166