MakesHttpRequests::handleErrorResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Signifly\Shopify\Support;
4
5
use Illuminate\Http\Client\Response;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Str;
8
use Signifly\Shopify\Exceptions\ErrorHandlerInterface;
9
use Signifly\Shopify\REST\Resources\ApiResource;
10
use Signifly\Shopify\Shopify;
11
12
/**
13
 * @mixin Shopify
14
 */
15
trait MakesHttpRequests
16
{
17
    protected Response $lastResponse;
18
19
    public function get(string $url, $query = null): Response
20
    {
21
        $response = $this->getHttpClient()->get($url, $query);
0 ignored issues
show
Bug introduced by
It seems like getHttpClient() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

21
        $response = $this->/** @scrutinizer ignore-call */ getHttpClient()->get($url, $query);
Loading history...
22
23
        $this->handleErrorResponse($response);
24
25
        return $response;
26
    }
27
28
    public function post(string $url, array $data = []): Response
29
    {
30
        $response = $this->getHttpClient()->post($url, $data);
31
32
        $this->handleErrorResponse($response);
33
34
        return $response;
35
    }
36
37
    public function put(string $url, array $data = []): Response
38
    {
39
        $response = $this->getHttpClient()->put($url, $data);
40
41
        $this->handleErrorResponse($response);
42
43
        return $response;
44
    }
45
46
    public function delete(string $url, array $data = []): Response
47
    {
48
        $response = $this->getHttpClient()->delete($url, $data);
49
50
        $this->handleErrorResponse($response);
51
52
        return $response;
53
    }
54
55
    protected function resourceClassFor(string $resource): string
56
    {
57
        $resourceClass = Str::of($resource)
58
            ->studly()
59
            ->singular()
60
            ->prepend('Signifly\\Shopify\\REST\\Resources\\')
61
            ->append('Resource');
62
63
        return class_exists($resourceClass) ? $resourceClass : ApiResource::class;
64
    }
65
66
    protected function createResource(string $resource, array $data, array $uriPrefix = [], string $responseKey = null): ApiResource
67
    {
68
        $key = Str::singular($resource);
69
        $resourceClass = $this->resourceClassFor($resource);
70
71
        $response = $this->post(implode('/', [...$uriPrefix, "{$resource}.json"]), [$key => $data]);
72
73
        if (! empty($responseKey)) {
74
            $key = $responseKey;
75
        }
76
77
        return new $resourceClass($response[$key], $this);
78
    }
79
80
    protected function getResourceCount(string $resource, array $params, array $uriPrefix = []): int
81
    {
82
        $response = $this->get(implode('/', [...$uriPrefix, "{$resource}/count.json"]), $params);
83
84
        return $response['count'] ?? 0;
85
    }
86
87
    protected function getResources(string $resource, array $params, array $uriPrefix = []): Collection
88
    {
89
        $resourceClass = $this->resourceClassFor($resource);
90
        $response = $this->get(implode('/', [...$uriPrefix, "{$resource}.json"]), $params);
91
92
        return $this->transformCollection($response[$resource], $resourceClass);
0 ignored issues
show
Bug introduced by
It seems like transformCollection() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( 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->/** @scrutinizer ignore-call */ transformCollection($response[$resource], $resourceClass);
Loading history...
93
    }
94
95
    protected function getResource(string $resource, $resourceId, array $uriPrefix = []): ApiResource
96
    {
97
        $key = Str::singular($resource);
98
        $resourceClass = $this->resourceClassFor($resource);
99
100
        $response = $this->get(implode('/', [...$uriPrefix, "{$resource}/{$resourceId}.json"]));
101
102
        return new $resourceClass($response[$key], $this);
103
    }
104
105
    protected function updateResource(string $resource, $resourceId, array $data, array $uriPrefix = [], string $responseKey = null): ApiResource
106
    {
107
        $key = Str::singular($resource);
108
        $resourceClass = $this->resourceClassFor($resource);
109
110
        $response = $this->put(implode('/', [...$uriPrefix, "{$resource}/{$resourceId}.json"]), [$key => $data]);
111
112
        if (! empty($responseKey)) {
113
            $key = $responseKey;
114
        }
115
116
        return new $resourceClass($response[$key], $this);
117
    }
118
119
    protected function deleteResource(string $resource, $resourceId, array $uriPrefix = []): void
120
    {
121
        $this->delete(implode('/', [...$uriPrefix, "{$resource}/{$resourceId}.json"]));
122
    }
123
124
    public function getLastResponse(): Response
125
    {
126
        return $this->lastResponse;
127
    }
128
129
    private function handleErrorResponse(Response $response): void
130
    {
131
        $this->lastResponse = $response;
132
133
        app(ErrorHandlerInterface::class)->handle($response);
134
    }
135
}
136