Passed
Branch develop (1d1e3b)
by Igor
02:57
created

BaseApi::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Igorsgm\Ghost\Apis;
4
5
use Igorsgm\Ghost\Models\Resources\BaseResource;
6
use Igorsgm\Ghost\Responses\ErrorResponse;
7
use Igorsgm\Ghost\Responses\SuccessResponse;
8
use Illuminate\Support\Collection;
9
10
abstract class BaseApi
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $baseUrl;
16
17
    /**
18
     * @var BaseResource
19
     */
20
    protected $resource;
21
22
    /**
23
     * @var string
24
     */
25
    public string $resourceId = "";
26
27
    /**
28
     * @var string
29
     */
30
    public string $resourceSlug = "";
31
32
    /**
33
     * @var string
34
     */
35
    public string $include = "";
36
37
    /**
38
     * @var string
39
     */
40
    public string $fields = "";
41
42
    /**
43
     * @var string
44
     */
45
    public string $formats = "";
46
47
    /**
48
     * @var string
49
     */
50
    protected $source = "";
51
52
    /**
53
     * @var string
54
     */
55
    public string $limit = "";
56
57
    /**
58
     * @var string
59
     */
60
    public string $filter = "";
61
62
    /**
63
     * @var string
64
     */
65
    public string $page = "";
66
67
    /**
68
     * @var string
69
     */
70
    public string $order = "";
71
72
    /**
73
     * @var string
74
     */
75
    protected string $key;
76
77
    /**
78
     * @var string
79
     */
80
    protected string $domain;
81
82
    /**
83
     * @var string
84
     */
85
    protected string $version;
86
87
    /**
88
     * @return string
89
     */
90 105
    protected function buildEndpoint(): string
91
    {
92 105
        $endpoint = $this->resource->getResourceName();
93
94 105
        if (!empty($this->resourceId)) {
95 28
            $endpoint .= "/$this->resourceId";
96 78
        } elseif (!empty($this->resourceSlug)) {
97 10
            $endpoint .= "/slug/$this->resourceSlug";
98
        }
99
100 105
        return $endpoint;
101
    }
102
103
    /**
104
     * @param string $suffix
105
     * @return string
106
     */
107 104
    protected function makeApiUrl($suffix = ''): string
108
    {
109 104
        return sprintf("%s/%s/?%s", $this->baseUrl, $this->buildEndpoint().$suffix, $this->buildParams());
110
    }
111
112
    /**
113
     * @return string
114
     */
115 114
    protected function buildParams(): string
116
    {
117 114
        $params = ['include', 'fields', 'formats', 'source', 'filter', 'limit', 'page', 'order', 'key'];
118
119 114
        $queryParams = [];
120 114
        foreach ($params as $param) {
121 114
            $queryParams[$param] = $this->{$param} ?: null;
122
        }
123
124 114
        return http_build_query($queryParams);
125
    }
126
127
    /**
128
     * @param $response
129
     * @return ErrorResponse|SuccessResponse
130
     */
131 102
    protected function handleResponse($response)
132
    {
133 102
        if ($response->failed()) {
134 4
            return new ErrorResponse($response);
135
        }
136
137 98
        return new SuccessResponse($this, $this->resource, $response);
138
    }
139
140
    /**
141
     * Return resource from slug
142
     *
143
     * @param  string  $slug
144
     *
145
     * @return array|ErrorResponse|mixed
146
     */
147 9
    public function fromSlug(string $slug)
148
    {
149 9
        $this->resourceSlug = $slug;
150 9
        $response = $this->get();
0 ignored issues
show
Bug introduced by
The method get() does not exist on Igorsgm\Ghost\Apis\BaseApi. Since it exists in all sub-types, consider adding an abstract or default implementation to Igorsgm\Ghost\Apis\BaseApi. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

150
        /** @scrutinizer ignore-call */ 
151
        $response = $this->get();
Loading history...
151
152 9
        if ($response instanceof ErrorResponse) {
153 1
            return $response;
154
        }
155
156 8
        return data_get($response->data, 0, []);
157
    }
158
159
    /**
160
     * @return Collection|array[]
161
     */
162 15
    public function all()
163
    {
164 15
        return $this->limit('all')->get();
165
    }
166
167
    /**
168
     * @return BaseResource|ErrorResponse
169
     */
170 5
    public function first()
171
    {
172 5
        $response = $this->limit(1)->get();
173 5
        return $response->data->first();
174
    }
175
176
    /**
177
     * @param $limit
178
     * @return array
179
     */
180 22
    public function paginate($limit = null)
181
    {
182 22
        if (isset($limit)) {
183 22
            $this->limit = $limit;
184
        }
185
186 22
        return $this->get();
187
    }
188
189
    /**
190
     * Return resource from ID
191
     *
192
     * @param  string  $id
193
     *
194
     * @return BaseResource|ErrorResponse
195
     */
196 17
    public function find(string $id)
197
    {
198 17
        $this->resourceId = $id;
199 17
        $response = $this->get();
200
201 17
        if ($response instanceof ErrorResponse) {
202 3
            return $response;
203
        }
204
205 14
        return data_get($response->data, 0, []);
206
    }
207
208
    /**
209
     * Apply fine-grained filters to target specific data.
210
     *
211
     * @param  string  $filter
212
     *
213
     * @return $this
214
     * @see https://ghost.org/docs/content-api/#filtering
215
     * @see https://gist.github.com/ErisDS/f516a859355d515aa6ad
216
     */
217 1
    public function filter($filter): BaseApi
218
    {
219 1
        $this->filter = $filter;
220
221 1
        return $this;
222
    }
223
224
    /**
225
     * Limit how many records are returned at once
226
     *
227
     * @param  int|string  $limit
228
     *
229
     * @return $this
230
     * @see https://ghost.org/docs/content-api/#limit
231
     */
232 34
    public function limit($limit): BaseApi
233
    {
234 34
        $this->limit = strval($limit);
235
236 34
        return $this;
237
    }
238
239
    /**
240
     * @param  string  $resourceClass
241
     * @return $this
242
     */
243 123
    public function setResource(string $resourceClass): BaseApi
244
    {
245 123
        $this->resource = resolve($resourceClass);
0 ignored issues
show
Documentation Bug introduced by
It seems like resolve($resourceClass) can also be of type Illuminate\Contracts\Foundation\Application. However, the property $resource is declared as type Igorsgm\Ghost\Models\Resources\BaseResource. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
246
247 123
        return $this;
248
    }
249
250
    /**
251
     * @return BaseResource
252
     */
253 19
    public function getResource()
254
    {
255 19
        return $this->resource;
256
    }
257
258
    /**
259
     * Alias for Ghost's include.
260
     *
261
     * The following includes are available:
262
     * Posts & Pages: authors, tags
263
     * Authors: count.posts
264
     * Tags: count.posts
265
     * Tiers: monthly_price, yearly_price, benefits
266
     *
267
     * @param  string|array  ...$includes
268
     * @return $this
269
     * @see https://ghost.org/docs/content-api/#include
270
     *
271
     */
272 11
    public function include(...$includes): BaseApi
273
    {
274 11
        $this->include = collect($includes)->flatten()->implode(',');
275
276 11
        return $this;
277
    }
278
279
    /**
280
     * Alias for include method
281
     *
282
     * @param ...$includes
283
     * @return $this
284
     */
285 2
    public function with(...$includes): BaseApi
286
    {
287 2
        return $this->include(...$includes);
288
    }
289
290
    /**
291
     * Limit the fields returned to the response object
292
     *
293
     * @param  string|array  ...$fields
294
     *
295
     * @return $this
296
     * @see https://ghost.org/docs/content-api/#fields
297
     */
298 3
    public function fields(...$fields): BaseApi
299
    {
300 3
        $this->fields = collect($fields)->flatten()->implode(',');
301
302 3
        return $this;
303
    }
304
305
    /**
306
     * Optionally request different format for posts and pages
307
     * By default, Admin API expects and returns content in the mobiledoc format only.
308
     * To include html in the response use this parameter.
309
     *
310
     * Possible formats: html, plaintext, mobiledoc.
311
     *
312
     *
313
     * @param  string  $format
314
     *
315
     * @return $this
316
     */
317 2
    public function formats(string $format): BaseApi
318
    {
319 2
        $this->formats = $format;
320
321 2
        return $this;
322
    }
323
324
    /**
325
     * @param  int  $page
326
     * @return $this
327
     * @see https://ghost.org/docs/content-api/#page
328
     */
329 14
    public function page(int $page): BaseApi
330
    {
331 14
        $this->page = strval($page);
332
333 14
        return $this;
334
    }
335
336
    /**
337
     * @param  string  $attr
338
     * @param  string  $order
339
     *
340
     * @return $this
341
     * @see https://ghost.org/docs/content-api/#order
342
     */
343 3
    public function order(string $attr, string $order = "DESC"): BaseApi
344
    {
345 3
        $this->order = $attr." ".strtolower($order);
346
347 3
        return $this;
348
    }
349
350
    /**
351
     * Alias for order method
352
     *
353
     * @param  string  $attr
354
     * @param  string  $order
355
     * @return $this
356
     */
357 1
    public function orderBy(string $attr, string $order = "DESC"): BaseApi
358
    {
359 1
        return $this->order($attr, $order);
360
    }
361
}
362