Passed
Push — develop ( 8877c5...09323d )
by Edwin
04:21
created

AbstractResource::getRequestOptions()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 2
crap 4
1
<?php
2
3
namespace ShopifyClient\Resource;
4
5
use ShopifyClient\Exception\ClientException;
6
use ShopifyClient\Request;
7
8
abstract class AbstractResource
9
{
10
    /**
11
     * @var Request
12
     */
13
    protected $request;
14
15
    /**
16
     * @var array
17
     */
18
    protected $actions = [];
19
20
    /**
21
     * @var array
22
     */
23
    protected $childResources = [];
24
25
    /**
26
     * AbstractResource constructor.
27
     * @param Request $request
28
     */
29 5
    public function __construct(Request $request)
30
    {
31 5
        $this->request = $request;
32 5
    }
33
34
    /**
35
     * @param string $action
36
     * @return array
37
     * @throws ClientException
38
     */
39 133
    public function getAction(string $action): array
40
    {
41 133
        if (!$this->hasAction($action)) {
42 1
            throw new ClientException(sprintf('Action: %s not found on resource. ', $action, get_called_class()));
43
        }
44
45 132
        return $this->actions[$action];
46
    }
47
48
    /**
49
     * @return Request
50
     */
51 1
    public function getRequest()
52
    {
53 1
        return $this->request;
54
    }
55
56
    /**
57
     * @param string $action
58
     * @return bool
59
     */
60 133
    public function hasAction(string $action): bool
61
    {
62 133
        if (!isset($this->actions[$action])) {
63 3
            return false;
64
        }
65
66 132
        return true;
67
    }
68
69
    /**
70
     * @return array
71
     */
72 5
    public function getChildResources(): array
73
    {
74 5
        return $this->childResources;
75
    }
76
77
    /**
78
     * @param string $action
79
     * @param float|null $parentId
80
     * @param float|null $childId
81
     * @param float|null $childChildId
82
     * @param array|null $parameters
83
     * @return array|bool
84
     */
85 133
    protected function request(
86
        string $action,
87
        float $parentId = null,
88
        float $childId = null,
89
        float $childChildId = null,
90
        array $parameters = []
91
    ) {
92 133
        $this->request->setResponseKey($this->getResponseKey($action));
93
94 132
        return $this->request->request(
95 132
            $this->getMethod($action),
96 131
            $this->getEndpoint($action, $parentId, $childId, $childChildId),
97 130
            $this->getRequestOptions($action, $parameters)
98
        );
99
    }
100
101
    /**
102
     * @param string $action
103
     * @param float|null $parentId
104
     * @param float|null $childId
105
     * @param float|null $childChildId
106
     * @return string
107
     * @throws ClientException
108
     */
109 131
    protected function getEndpoint(
110
        string $action,
111
        float $parentId = null,
112
        float $childId = null,
113
        float $childChildId = null
114
    ): string {
115 131
        $actionData = $this->getAction($action);
116
117 131
        if (!isset($actionData['endpoint'])) {
118 1
            throw new ClientException(sprintf('Endpoint key not set for action: %s.', $action));
119
        }
120
121 130
        return sprintf($actionData['endpoint'], $parentId, $childId, $childChildId);
122
    }
123
124
    /**
125
     * @param string $action
126
     * @return string
127
     * @throws ClientException
128
     */
129 132
    protected function getMethod(string $action): string
130
    {
131 132
        $actionData = $this->getAction($action);
132
133 132
        if (!isset($actionData['method'])) {
134 1
            throw new ClientException(sprintf('Method key not set for action: %s.', $action));
135
        }
136
137 131
        return $actionData['method'];
138
    }
139
140
    /**
141
     * @param string $action
142
     * @return string
143
     */
144 130
    protected function getResourceKey(string $action): string
145
    {
146 130
        $actionData = $this->getAction($action);
147
148 130
        if (!isset($actionData['resourceKey'])) {
149 28
            return '';
150
        }
151
152 105
        return $actionData['resourceKey'];
153
    }
154
155
    /**
156
     * @param string $action
157
     * @return string
158
     */
159 133
    protected function getResponseKey(string $action): string
160
    {
161 133
        $actionData = $this->getAction($action);
162
163 132
        if (!isset($actionData['responseKey'])) {
164 25
            return '';
165
        }
166
167 108
        return $actionData['responseKey'];
168
    }
169
170
    /**
171
     * @param string $action
172
     * @param array $parameters
173
     * @return array
174
     */
175 130
    private function getRequestOptions(string $action, array $parameters): array
176
    {
177 130
        $method      = $this->getMethod($action);
178 130
        $resourceKey = $this->getResourceKey($action);
179
180 130
        if ($method === 'POST' || $method === 'PUT') {
181 52
            return strlen($resourceKey) < 1 ? [] : ['body' => json_encode([$resourceKey => $parameters])];
182
        }
183
184
        return [
185 81
            'query' => $parameters
186
        ];
187
    }
188
189
    /**
190
     * @param string $method
191
     * @param array|null $arguments
192
     * @return array|bool
193
     */
194 133
    public function __call(string $method, array $arguments = [])
195
    {
196 133
        return $this->request(
197 133
            $method,
198 133
            $this->getParentId($arguments),
199 133
            $this->getChildId($arguments),
200 133
            $this->getChildChildId($arguments),
201 133
            $this->getParameters($arguments)
202
        );
203
    }
204
205
    /**
206
     * @param array $arguments
207
     * @return float|null
208
     */
209 133
    private function getParentId(array $arguments)
210
    {
211 133
        $parentId = null;
212
213 133
        if (!empty($arguments[0])) {
214 126
            if (is_numeric($arguments[0])) {
215 109
                $parentId = $arguments[0];
216
            }
217
        }
218
219 133
        return $parentId;
220
    }
221
222
    /**
223
     * @param array $arguments
224
     * @return float|null
225
     */
226 133
    private function getChildId(array $arguments)
227
    {
228 133
        $childId = null;
229
230 133
        if (!empty($arguments[1])) {
231 75
            if (is_numeric($arguments[1])) {
232 46
                $childId = $arguments[1];
233
            }
234
        }
235
236 133
        return $childId;
237
    }
238
239
    /**
240
     * @param array $arguments
241
     * @return float|null
242
     */
243 133
    private function getChildChildId(array $arguments)
244
    {
245 133
        $childChildId = null;
246
247 133
        if (!empty($arguments[2])) {
248 24
            if (is_numeric($arguments[2])) {
249 8
                $childChildId = $arguments[2];
250
            }
251
        }
252
253 133
        return $childChildId;
254
    }
255
256
    /**
257
     * @param array $arguments
258
     * @return array
259
     */
260 133
    private function getParameters(array $arguments): array
261
    {
262 133
        foreach ($arguments as $value) {
263 127
            if (is_array($value)) {
264 127
                return $value;
265
            }
266
        }
267
268 82
        return [];
269
    }
270
}
271