Completed
Pull Request — master (#4)
by Morten Poul
11:21
created

MakesHttpRequests   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
dl 0
loc 111
rs 10
c 1
b 0
f 0
wmc 14

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getLastResponse() 0 3 1
A resourceClassFor() 0 9 2
A handleErrorResponse() 0 5 1
A put() 0 7 1
A getResources() 0 6 1
A updateResource() 0 8 1
A get() 0 7 1
A deleteResource() 0 3 1
A createResource() 0 8 1
A getResourceCount() 0 5 1
A getResource() 0 8 1
A post() 0 7 1
A delete() 0 7 1
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 = []): ApiResource
67
    {
68
        $key = Str::singular($resource);
69
        $resourceClass = $this->resourceClassFor($resource);
70
71
        $response = $this->post(join('/', [...$uriPrefix, "{$resource}.json"]), [$key => $data]);
72
73
        return new $resourceClass($response[$key], $this);
74
    }
75
76
    protected function getResourceCount(string $resource, array $params, array $uriPrefix = []): int
77
    {
78
        $response = $this->get(join('/', [...$uriPrefix, "{$resource}/count.json"]), $params);
79
80
        return $response['count'] ?? 0;
81
    }
82
83
    protected function getResources(string $resource, array $params, array $uriPrefix = []): Collection
84
    {
85
        $resourceClass = $this->resourceClassFor($resource);
86
        $response = $this->get(join('/', [...$uriPrefix, "{$resource}.json"]), $params);
87
88
        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

88
        return $this->/** @scrutinizer ignore-call */ transformCollection($response[$resource], $resourceClass);
Loading history...
89
    }
90
91
    protected function getResource(string $resource, $resourceId, array $uriPrefix = []): ApiResource
92
    {
93
        $key = Str::singular($resource);
94
        $resourceClass = $this->resourceClassFor($resource);
95
96
        $response = $this->get(join('/', [...$uriPrefix, "{$resource}/{$resourceId}.json"]));
97
98
        return new $resourceClass($response[$key], $this);
99
    }
100
101
    protected function updateResource(string $resource, $resourceId, array $data, array $uriPrefix = []): ApiResource
102
    {
103
        $key = Str::singular($resource);
104
        $resourceClass = $this->resourceClassFor($resource);
105
106
        $response = $this->put(join('/', [...$uriPrefix, "{$resource}/{$resourceId}.json"]), [$key => $data]);
107
108
        return new $resourceClass($response[$key], $this);
109
    }
110
111
    protected function deleteResource(string $resource, $resourceId, array $uriPrefix = []): void
112
    {
113
        $this->delete(join('/', [...$uriPrefix, "{$resource}/{$resourceId}.json"]));
114
    }
115
116
    public function getLastResponse(): Response
117
    {
118
        return $this->lastResponse;
119
    }
120
121
    private function handleErrorResponse(Response $response): void
122
    {
123
        $this->lastResponse = $response;
124
125
        app(ErrorHandlerInterface::class)->handle($response);
126
    }
127
}
128