SuccessResponse   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 29
c 3
b 0
f 0
dl 0
loc 94
ccs 25
cts 25
cp 1
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A handle() 0 22 5
A getPreviousPage() 0 5 1
A getNextPage() 0 5 1
1
<?php
2
3
namespace Igorsgm\Ghost\Responses;
4
5
use Igorsgm\Ghost\Apis\BaseApi;
6
use Igorsgm\Ghost\Models\Meta;
7
use Igorsgm\Ghost\Models\Resources\BaseResource;
8
use Illuminate\Http\Client\Response;
9
use Illuminate\Support\Collection;
10
11
class SuccessResponse
12
{
13
    /**
14
     * @var BaseApi
15
     */
16
    private $api;
17
18
    /**
19
     * @var BaseResource
20
     */
21
    private $resource;
22
23
    /**
24
     * @var Response|mixed
25
     */
26
    private $response;
27
28
    /**
29
     * @var bool
30
     */
31
    public $success = true;
32
33
    /**
34
     * @var Collection|array
35
     */
36
    public $data = [];
37
38
    /**
39
     * @var Meta|array
40
     */
41
    public $meta = [];
42
43
    /**
44
     * @param  Response|mixed  $response
45
     */
46 98
    public function __construct(BaseApi $api, BaseResource $resource, $response)
47
    {
48 98
        $this->api = $api;
49 98
        $this->resource = $resource;
50 98
        $this->response = $response;
51
52 98
        $this->handle();
53
    }
54
55
    /**
56
     * @return void
57
     */
58 98
    private function handle()
59
    {
60 98
        $resourceName = $this->resource->getResourceName();
61 98
        $decodedResponse = $this->response->json();
62
63 98
        $responseData = data_get($decodedResponse, $resourceName);
64 98
        $meta = data_get($decodedResponse, 'meta');
65
66 98
        if (in_array($resourceName, ['settings', 'site'])) {
67 3
            $data = new $this->resource($responseData);
0 ignored issues
show
Bug introduced by
It seems like $responseData can also be of type null; however, parameter $data of Igorsgm\Ghost\Models\Res...Resource::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

67
            $data = new $this->resource(/** @scrutinizer ignore-type */ $responseData);
Loading history...
68
        } else {
69 95
            $data = collect();
70 95
            $responseData = ! empty($responseData) ? $responseData : [];
71 95
            foreach ($responseData as $resourceItemData) {
72 91
                $data->push(new $this->resource($resourceItemData));
73
            }
74
        }
75
76 98
        $this->data = $data;
77
78 98
        if (! empty($meta)) {
79 53
            $this->meta = new Meta($meta);
80
        }
81
    }
82
83
    /**
84
     * Fetches the Previous page response
85
     *
86
     * @return array|array[]|SuccessResponse
87
     */
88 6
    public function getPreviousPage()
89
    {
90 6
        $previusPage = $this->meta->prev();
91
92 6
        return $this->api->page($previusPage)->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

92
        return $this->api->page($previusPage)->/** @scrutinizer ignore-call */ get();
Loading history...
93
    }
94
95
    /**
96
     * Fetches the Next page response
97
     *
98
     * @return array|array[]|SuccessResponse
99
     */
100 6
    public function getNextPage()
101
    {
102 6
        $nextPage = $this->meta->next();
103
104 6
        return $this->api->page($nextPage)->get();
105
    }
106
}
107