|
9 | 9 | use Illuminate\Support\Enumerable; |
10 | 10 | use Illuminate\Support\LazyCollection; |
11 | 11 | use JustBetter\MagentoClient\Contracts\BuildsRequest; |
| 12 | +use JustBetter\MagentoClient\Events\MagentoResponseEvent; |
12 | 13 | use JustBetter\MagentoClient\OAuth\KeyStore\FileKeyStore; |
13 | 14 |
|
14 | 15 | class Magento |
@@ -52,110 +53,110 @@ public function graphql(string $query, array $variables = []): Response |
52 | 53 | 'variables' => $variables, |
53 | 54 | ]); |
54 | 55 |
|
55 | | - return $response; |
| 56 | + return $this->handleResponse($response); |
56 | 57 | } |
57 | 58 |
|
58 | 59 | public function get(string $path, array $data = []): Response |
59 | 60 | { |
60 | 61 | $response = $this->request()->get($this->getUrl($path), $data); |
61 | 62 |
|
62 | | - return $response; |
| 63 | + return $this->handleResponse($response); |
63 | 64 | } |
64 | 65 |
|
65 | 66 | public function post(string $path, array $data = []): Response |
66 | 67 | { |
67 | 68 | /** @var Response $response */ |
68 | 69 | $response = $this->request()->post($this->getUrl($path), $data); |
69 | 70 |
|
70 | | - return $response; |
| 71 | + return $this->handleResponse($response); |
71 | 72 | } |
72 | 73 |
|
73 | 74 | public function postAsync(string $path, array $data = []): Response |
74 | 75 | { |
75 | 76 | /** @var Response $response */ |
76 | 77 | $response = $this->request()->post($this->getUrl($path, true), $data); |
77 | 78 |
|
78 | | - return $response; |
| 79 | + return $this->handleResponse($response); |
79 | 80 | } |
80 | 81 |
|
81 | 82 | public function postBulk(string $path, array $data = []): Response |
82 | 83 | { |
83 | 84 | /** @var Response $response */ |
84 | 85 | $response = $this->request()->post($this->getUrl($path, true, true), $data); |
85 | 86 |
|
86 | | - return $response; |
| 87 | + return $this->handleResponse($response); |
87 | 88 | } |
88 | 89 |
|
89 | 90 | public function patch(string $path, array $data = []): Response |
90 | 91 | { |
91 | 92 | /** @var Response $response */ |
92 | 93 | $response = $this->request()->patch($this->getUrl($path), $data); |
93 | 94 |
|
94 | | - return $response; |
| 95 | + return $this->handleResponse($response); |
95 | 96 | } |
96 | 97 |
|
97 | 98 | public function patchAsync(string $path, array $data = []): Response |
98 | 99 | { |
99 | 100 | /** @var Response $response */ |
100 | 101 | $response = $this->request()->patch($this->getUrl($path, true), $data); |
101 | 102 |
|
102 | | - return $response; |
| 103 | + return $this->handleResponse($response); |
103 | 104 | } |
104 | 105 |
|
105 | 106 | public function patchBulk(string $path, array $data = []): Response |
106 | 107 | { |
107 | 108 | /** @var Response $response */ |
108 | 109 | $response = $this->request()->patch($this->getUrl($path, true, true), $data); |
109 | 110 |
|
110 | | - return $response; |
| 111 | + return $this->handleResponse($response); |
111 | 112 | } |
112 | 113 |
|
113 | 114 | public function put(string $path, array $data = []): Response |
114 | 115 | { |
115 | 116 | /** @var Response $response */ |
116 | 117 | $response = $this->request()->put($this->getUrl($path), $data); |
117 | 118 |
|
118 | | - return $response; |
| 119 | + return $this->handleResponse($response); |
119 | 120 | } |
120 | 121 |
|
121 | 122 | public function putAsync(string $path, array $data = []): Response |
122 | 123 | { |
123 | 124 | /** @var Response $response */ |
124 | 125 | $response = $this->request()->put($this->getUrl($path, true), $data); |
125 | 126 |
|
126 | | - return $response; |
| 127 | + return $this->handleResponse($response); |
127 | 128 | } |
128 | 129 |
|
129 | 130 | public function putBulk(string $path, array $data = []): Response |
130 | 131 | { |
131 | 132 | /** @var Response $response */ |
132 | 133 | $response = $this->request()->put($this->getUrl($path, true, true), $data); |
133 | 134 |
|
134 | | - return $response; |
| 135 | + return $this->handleResponse($response); |
135 | 136 | } |
136 | 137 |
|
137 | 138 | public function delete(string $path, array $data = []): Response |
138 | 139 | { |
139 | 140 | /** @var Response $response */ |
140 | 141 | $response = $this->request()->delete($this->getUrl($path), $data); |
141 | 142 |
|
142 | | - return $response; |
| 143 | + return $this->handleResponse($response); |
143 | 144 | } |
144 | 145 |
|
145 | 146 | public function deleteAsync(string $path, array $data = []): Response |
146 | 147 | { |
147 | 148 | /** @var Response $response */ |
148 | 149 | $response = $this->request()->delete($this->getUrl($path, true), $data); |
149 | 150 |
|
150 | | - return $response; |
| 151 | + return $this->handleResponse($response); |
151 | 152 | } |
152 | 153 |
|
153 | 154 | public function deleteBulk(string $path, array $data = []): Response |
154 | 155 | { |
155 | 156 | /** @var Response $response */ |
156 | 157 | $response = $this->request()->delete($this->getUrl($path, true, true), $data); |
157 | 158 |
|
158 | | - return $response; |
| 159 | + return $this->handleResponse($response); |
159 | 160 | } |
160 | 161 |
|
161 | 162 | /** @return LazyCollection<int, array> */ |
@@ -227,6 +228,13 @@ protected function request(): PendingRequest |
227 | 228 | return $request; |
228 | 229 | } |
229 | 230 |
|
| 231 | + protected function handleResponse(Response $response): Response |
| 232 | + { |
| 233 | + MagentoResponseEvent::dispatch($response); |
| 234 | + |
| 235 | + return $response; |
| 236 | + } |
| 237 | + |
230 | 238 | public static function fake(): void |
231 | 239 | { |
232 | 240 | config()->set('magento.connection', 'default'); |
|
0 commit comments