Skip to content

Commit

Permalink
Dispatch event when a webhook has been handled
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Sep 2, 2024
1 parent 0bc44f4 commit d7df11d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Controller/HandleWebhookAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
namespace Setono\SyliusPeakPlugin\Controller;

use Doctrine\Persistence\ManagerRegistry;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerAwareInterface;
use Setono\Doctrine\ORMTrait;
use Setono\PeakWMS\Parser\WebhookParser;
use Setono\PeakWMS\Parser\WebhookParserInterface;
use Setono\SyliusPeakPlugin\Event\WebhookHandledEvent;
use Setono\SyliusPeakPlugin\Factory\WebhookFactoryInterface;
use Setono\SyliusPeakPlugin\Logger\WebhookLogger;
use Setono\SyliusPeakPlugin\WebhookHandler\WebhookHandlerInterface;
Expand All @@ -26,6 +28,7 @@ public function __construct(
private readonly WebhookHandlerInterface $webhookHandler,
private readonly WebhookFactoryInterface $webhookFactory,
ManagerRegistry $managerRegistry,
private readonly EventDispatcherInterface $eventDispatcher,
) {
$this->managerRegistry = $managerRegistry;
}
Expand All @@ -47,6 +50,8 @@ public function __invoke(Request $request): JsonResponse
$data = $this->webhookParser->parse($request->getContent(), $dataClass);

$this->webhookHandler->handle($data);

$this->eventDispatcher->dispatch(new WebhookHandledEvent($webhook));
} catch (\InvalidArgumentException $e) {
throw new BadRequestHttpException($e->getMessage());
} finally {
Expand Down
16 changes: 16 additions & 0 deletions src/Event/WebhookHandledEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusPeakPlugin\Event;

final class WebhookHandledEvent
{
public function __construct(
/**
* This is one of the Webhook DTO classes from the Setono\PeakWMS\DataTransferObject\Webhook namespace
*/
public readonly object $webhook,
) {
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<argument type="service" id="Setono\SyliusPeakPlugin\WebhookHandler\WebhookHandlerInterface"/>
<argument type="service" id="setono_sylius_peak.factory.webhook"/>
<argument type="service" id="doctrine"/>
<argument type="service" id="event_dispatcher"/>
</service>
</services>
</container>

0 comments on commit d7df11d

Please sign in to comment.