Skip to content

Commit e2fc8b8

Browse files
authored
Dispatch event on responses (#41)
1 parent b1785a5 commit e2fc8b8

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-14
lines changed

src/Client/Magento.php

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Support\Enumerable;
1010
use Illuminate\Support\LazyCollection;
1111
use JustBetter\MagentoClient\Contracts\BuildsRequest;
12+
use JustBetter\MagentoClient\Events\MagentoResponseEvent;
1213
use JustBetter\MagentoClient\OAuth\KeyStore\FileKeyStore;
1314

1415
class Magento
@@ -52,110 +53,110 @@ public function graphql(string $query, array $variables = []): Response
5253
'variables' => $variables,
5354
]);
5455

55-
return $response;
56+
return $this->handleResponse($response);
5657
}
5758

5859
public function get(string $path, array $data = []): Response
5960
{
6061
$response = $this->request()->get($this->getUrl($path), $data);
6162

62-
return $response;
63+
return $this->handleResponse($response);
6364
}
6465

6566
public function post(string $path, array $data = []): Response
6667
{
6768
/** @var Response $response */
6869
$response = $this->request()->post($this->getUrl($path), $data);
6970

70-
return $response;
71+
return $this->handleResponse($response);
7172
}
7273

7374
public function postAsync(string $path, array $data = []): Response
7475
{
7576
/** @var Response $response */
7677
$response = $this->request()->post($this->getUrl($path, true), $data);
7778

78-
return $response;
79+
return $this->handleResponse($response);
7980
}
8081

8182
public function postBulk(string $path, array $data = []): Response
8283
{
8384
/** @var Response $response */
8485
$response = $this->request()->post($this->getUrl($path, true, true), $data);
8586

86-
return $response;
87+
return $this->handleResponse($response);
8788
}
8889

8990
public function patch(string $path, array $data = []): Response
9091
{
9192
/** @var Response $response */
9293
$response = $this->request()->patch($this->getUrl($path), $data);
9394

94-
return $response;
95+
return $this->handleResponse($response);
9596
}
9697

9798
public function patchAsync(string $path, array $data = []): Response
9899
{
99100
/** @var Response $response */
100101
$response = $this->request()->patch($this->getUrl($path, true), $data);
101102

102-
return $response;
103+
return $this->handleResponse($response);
103104
}
104105

105106
public function patchBulk(string $path, array $data = []): Response
106107
{
107108
/** @var Response $response */
108109
$response = $this->request()->patch($this->getUrl($path, true, true), $data);
109110

110-
return $response;
111+
return $this->handleResponse($response);
111112
}
112113

113114
public function put(string $path, array $data = []): Response
114115
{
115116
/** @var Response $response */
116117
$response = $this->request()->put($this->getUrl($path), $data);
117118

118-
return $response;
119+
return $this->handleResponse($response);
119120
}
120121

121122
public function putAsync(string $path, array $data = []): Response
122123
{
123124
/** @var Response $response */
124125
$response = $this->request()->put($this->getUrl($path, true), $data);
125126

126-
return $response;
127+
return $this->handleResponse($response);
127128
}
128129

129130
public function putBulk(string $path, array $data = []): Response
130131
{
131132
/** @var Response $response */
132133
$response = $this->request()->put($this->getUrl($path, true, true), $data);
133134

134-
return $response;
135+
return $this->handleResponse($response);
135136
}
136137

137138
public function delete(string $path, array $data = []): Response
138139
{
139140
/** @var Response $response */
140141
$response = $this->request()->delete($this->getUrl($path), $data);
141142

142-
return $response;
143+
return $this->handleResponse($response);
143144
}
144145

145146
public function deleteAsync(string $path, array $data = []): Response
146147
{
147148
/** @var Response $response */
148149
$response = $this->request()->delete($this->getUrl($path, true), $data);
149150

150-
return $response;
151+
return $this->handleResponse($response);
151152
}
152153

153154
public function deleteBulk(string $path, array $data = []): Response
154155
{
155156
/** @var Response $response */
156157
$response = $this->request()->delete($this->getUrl($path, true, true), $data);
157158

158-
return $response;
159+
return $this->handleResponse($response);
159160
}
160161

161162
/** @return LazyCollection<int, array> */
@@ -227,6 +228,13 @@ protected function request(): PendingRequest
227228
return $request;
228229
}
229230

231+
protected function handleResponse(Response $response): Response
232+
{
233+
MagentoResponseEvent::dispatch($response);
234+
235+
return $response;
236+
}
237+
230238
public static function fake(): void
231239
{
232240
config()->set('magento.connection', 'default');
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace JustBetter\MagentoClient\Events;
4+
5+
use Illuminate\Foundation\Events\Dispatchable;
6+
use Illuminate\Http\Client\Response;
7+
8+
class MagentoResponseEvent
9+
{
10+
use Dispatchable;
11+
12+
public function __construct(
13+
public Response $response
14+
) {
15+
16+
}
17+
}

tests/Client/ClientTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
use Illuminate\Http\Client\PendingRequest;
66
use Illuminate\Http\Client\Request;
7+
use Illuminate\Support\Facades\Event;
78
use Illuminate\Support\Facades\Http;
89
use Illuminate\Support\Str;
910
use JustBetter\MagentoClient\Client\Magento;
11+
use JustBetter\MagentoClient\Events\MagentoResponseEvent;
1012
use JustBetter\MagentoClient\Tests\TestCase;
1113

1214
class ClientTest extends TestCase
@@ -569,4 +571,25 @@ public function test_it_resets_the_interceptor(): void
569571
fn (Request $request) => ! $request->hasHeader('some-header'),
570572
]);
571573
}
574+
575+
public function test_it_dispatches_event(): void
576+
{
577+
Event::fake();
578+
579+
Http::fake([
580+
'magento/rest/all/V1/products*' => Http::response(['items' => ['item']]),
581+
]);
582+
583+
/** @var Magento $magento */
584+
$magento = app(Magento::class);
585+
586+
$magento->get('products', [
587+
'searchCriteria[pageSize]' => 10,
588+
'searchCriteria[currentPage]' => 0,
589+
]);
590+
591+
Event::assertDispatched(MagentoResponseEvent::class, function (MagentoResponseEvent $event): bool {
592+
return $event->response->ok() && $event->response->json('items') === ['item'];
593+
});
594+
}
572595
}

0 commit comments

Comments
 (0)