Skip to content

Commit

Permalink
Dispatch event on responses (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentBean authored May 21, 2024
1 parent b1785a5 commit e2fc8b8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/Client/Magento.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Enumerable;
use Illuminate\Support\LazyCollection;
use JustBetter\MagentoClient\Contracts\BuildsRequest;
use JustBetter\MagentoClient\Events\MagentoResponseEvent;
use JustBetter\MagentoClient\OAuth\KeyStore\FileKeyStore;

class Magento
Expand Down Expand Up @@ -52,110 +53,110 @@ public function graphql(string $query, array $variables = []): Response
'variables' => $variables,
]);

return $response;
return $this->handleResponse($response);
}

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

return $response;
return $this->handleResponse($response);
}

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

return $response;
return $this->handleResponse($response);
}

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

return $response;
return $this->handleResponse($response);
}

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

return $response;
return $this->handleResponse($response);
}

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

return $response;
return $this->handleResponse($response);
}

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

return $response;
return $this->handleResponse($response);
}

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

return $response;
return $this->handleResponse($response);
}

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

return $response;
return $this->handleResponse($response);
}

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

return $response;
return $this->handleResponse($response);
}

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

return $response;
return $this->handleResponse($response);
}

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

return $response;
return $this->handleResponse($response);
}

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

return $response;
return $this->handleResponse($response);
}

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

return $response;
return $this->handleResponse($response);
}

/** @return LazyCollection<int, array> */
Expand Down Expand Up @@ -227,6 +228,13 @@ protected function request(): PendingRequest
return $request;
}

protected function handleResponse(Response $response): Response
{
MagentoResponseEvent::dispatch($response);

return $response;
}

public static function fake(): void
{
config()->set('magento.connection', 'default');
Expand Down
17 changes: 17 additions & 0 deletions src/Events/MagentoResponseEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace JustBetter\MagentoClient\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Http\Client\Response;

class MagentoResponseEvent
{
use Dispatchable;

public function __construct(
public Response $response
) {

}
}
23 changes: 23 additions & 0 deletions tests/Client/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use JustBetter\MagentoClient\Client\Magento;
use JustBetter\MagentoClient\Events\MagentoResponseEvent;
use JustBetter\MagentoClient\Tests\TestCase;

class ClientTest extends TestCase
Expand Down Expand Up @@ -569,4 +571,25 @@ public function test_it_resets_the_interceptor(): void
fn (Request $request) => ! $request->hasHeader('some-header'),
]);
}

public function test_it_dispatches_event(): void
{
Event::fake();

Http::fake([
'magento/rest/all/V1/products*' => Http::response(['items' => ['item']]),
]);

/** @var Magento $magento */
$magento = app(Magento::class);

$magento->get('products', [
'searchCriteria[pageSize]' => 10,
'searchCriteria[currentPage]' => 0,
]);

Event::assertDispatched(MagentoResponseEvent::class, function (MagentoResponseEvent $event): bool {
return $event->response->ok() && $event->response->json('items') === ['item'];
});
}
}

0 comments on commit e2fc8b8

Please sign in to comment.