 igorsgm    /
                    laravel-ghost
                      igorsgm    /
                    laravel-ghost
                
                            | 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  
  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 The method  get()does not exist onIgorsgm\Ghost\Apis\BaseApi. Since it exists in all sub-types, consider adding an abstract or default implementation toIgorsgm\Ghost\Apis\BaseApi.
                                                                                   (
                                     Ignorable by Annotation
                                ) If this is a false-positive, you can also ignore this issue in your code via the  
  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 | 
