-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Import product created or updated via webhook (#157)
- Loading branch information
Showing
8 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
webgriffe_sylius_akeneo_webhook: | ||
path: /akeneo/webhook | ||
methods: [POST] | ||
controller: webgriffe_sylius_akeneo.controller.webhook::postAction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Webgriffe\SyliusAkeneoPlugin\Controller; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use RuntimeException; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Webgriffe\SyliusAkeneoPlugin\Message\ItemImport; | ||
use Webgriffe\SyliusAkeneoPlugin\Product\Importer as ProductImporter; | ||
use Webgriffe\SyliusAkeneoPlugin\ProductAssociations\Importer as ProductAssociationsImporter; | ||
|
||
/** | ||
* @psalm-type AkeneoEventProduct = array{ | ||
* uuid: string, | ||
* identifier: string, | ||
* enabled: bool, | ||
* family: string, | ||
* categories: string[], | ||
* groups: string[], | ||
* parent: ?string, | ||
* values: array<string, array>, | ||
* created: string, | ||
* updated: string, | ||
* associations: array<string, array>, | ||
* quantified_associations: array<string, array>, | ||
* } | ||
* @psalm-type AkeneoEventProductModel = array{ | ||
* code: string, | ||
* family: string, | ||
* family_variant: string, | ||
* parent: ?string, | ||
* categories: string[], | ||
* values: array<string, array>, | ||
* created: string, | ||
* updated: string, | ||
* associations: array<string, array>, | ||
* quantified_associations: array<string, array>, | ||
* } | ||
* @psalm-type AkeneoEvent = array{ | ||
* action: string, | ||
* event_id: string, | ||
* event_datetime: string, | ||
* author: string, | ||
* author_type: string, | ||
* pim_source: string, | ||
* data: array{ | ||
* resource: AkeneoEventProduct|AkeneoEventProductModel | ||
* }, | ||
* } | ||
* @psalm-type AkeneoEvents = array{ | ||
* events: AkeneoEvent[], | ||
* } | ||
*/ | ||
final class WebhookController extends AbstractController | ||
{ | ||
public function __construct( | ||
private LoggerInterface $logger, | ||
private MessageBusInterface $messageBus, | ||
private string $secret, | ||
) { | ||
} | ||
|
||
/** | ||
* As guideline see the documentation here: https://api.akeneo.com/getting-started/quick-start-my-first-webhook-5x/step-2.html | ||
* | ||
* @throws RuntimeException | ||
* @throws \JsonException | ||
*/ | ||
public function postAction(Request $request): Response | ||
{ | ||
$timestamp = $request->headers->get('x-akeneo-request-timestamp'); | ||
$signature = $request->headers->get('x-akeneo-request-signature'); | ||
if (null === $timestamp || null === $signature) { | ||
return new Response('', Response::HTTP_UNAUTHORIZED); | ||
} | ||
|
||
$body = $request->getContent(); | ||
$expectedSignature = hash_hmac('sha256', $timestamp . '.' . $body, $this->secret); | ||
if (false === hash_equals($signature, $expectedSignature)) { | ||
return new Response('', Response::HTTP_UNAUTHORIZED); | ||
} | ||
if (time() - (int) $timestamp > 300) { | ||
throw new RuntimeException('Request is too old (> 5min)'); | ||
} | ||
|
||
$this->logger->debug($body); | ||
|
||
/** | ||
* @TODO Could this be improved by using serializer? Is it necessary or overwork? | ||
* | ||
* @var AkeneoEvents $akeneoEvents | ||
*/ | ||
$akeneoEvents = json_decode($body, true, 512, \JSON_THROW_ON_ERROR); | ||
|
||
foreach ($akeneoEvents['events'] as $akeneoEvent) { | ||
$resource = $akeneoEvent['data']['resource']; | ||
if (array_key_exists('identifier', $resource)) { | ||
$productCode = $resource['identifier']; | ||
$this->logger->debug(sprintf( | ||
'Dispatching product import message for %s', | ||
$productCode, | ||
)); | ||
$this->messageBus->dispatch(new ItemImport( | ||
ProductImporter::AKENEO_ENTITY, | ||
$productCode, | ||
)); | ||
$this->logger->debug(sprintf( | ||
'Dispatching product associations import message for %s', | ||
$productCode, | ||
)); | ||
$this->messageBus->dispatch(new ItemImport( | ||
ProductAssociationsImporter::AKENEO_ENTITY, | ||
$productCode, | ||
)); | ||
} | ||
} | ||
|
||
return new Response(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
webgriffe_sylius_akeneo_plugin_admin: | ||
resource: "@WebgriffeSyliusAkeneoPlugin/config/admin_routing.yaml" | ||
prefix: '/%sylius_admin.path_name%' | ||
|
||
webgriffe_sylius_akeneo_plugin_webhook: | ||
resource: "@WebgriffeSyliusAkeneoPlugin/config/webhook_routing.yaml" |