-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
487 additions
and
189 deletions.
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
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,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusPeakWMSPlugin\Controller; | ||
|
||
use Setono\PeakWMS\Parser\WebhookParser; | ||
use Setono\PeakWMS\Parser\WebhookParserInterface; | ||
use Setono\SyliusPeakWMSPlugin\WebhookHandler\WebhookHandlerInterface; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | ||
|
||
final class HandleWebhookControllerAction | ||
{ | ||
public function __construct( | ||
private readonly WebhookParserInterface $webhookParser, | ||
private readonly WebhookHandlerInterface $webhookHandler, | ||
) { | ||
} | ||
|
||
public function __invoke(Request $request): JsonResponse | ||
{ | ||
try { | ||
$dataClass = WebhookParser::convertNameToDataClass($request->query->getInt('name')); | ||
|
||
$data = $this->webhookParser->parse($request->getContent(), $dataClass); | ||
|
||
$this->webhookHandler->handle($data); | ||
} catch (\InvalidArgumentException $e) { | ||
throw new BadRequestHttpException($e->getMessage()); | ||
} | ||
|
||
return new JsonResponse(status: Response::HTTP_NO_CONTENT); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusPeakWMSPlugin\Exception; | ||
|
||
final class UnsupportedWebhookException extends \RuntimeException | ||
{ | ||
public static function fromData(object $data): self | ||
{ | ||
return new self('Unsupported webhook: ' . $data::class); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusPeakWMSPlugin\Message\Command; | ||
|
||
use Sylius\Component\Core\Model\ProductVariantInterface; | ||
|
||
/** | ||
* Will update the inventory for a product variant | ||
*/ | ||
final class UpdateInventory implements CommandInterface | ||
{ | ||
public int $productVariant; | ||
|
||
public function __construct(int|ProductVariantInterface $productVariant) | ||
{ | ||
if ($productVariant instanceof ProductVariantInterface) { | ||
$productVariant = (int) $productVariant->getId(); | ||
} | ||
|
||
$this->productVariant = $productVariant; | ||
} | ||
} |
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,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusPeakWMSPlugin\Message\CommandHandler; | ||
|
||
use Setono\PeakWMS\Client\ClientInterface; | ||
use Setono\PeakWMS\DataTransferObject\Product\Product; | ||
use Setono\SyliusPeakWMSPlugin\Message\Command\UpdateInventory; | ||
use Sylius\Component\Core\Model\ProductVariantInterface; | ||
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface; | ||
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException; | ||
|
||
final class UpdateInventoryHandler | ||
{ | ||
public function __construct( | ||
private readonly ClientInterface $client, | ||
private readonly ProductVariantRepositoryInterface $productVariantRepository, | ||
) { | ||
} | ||
|
||
public function __invoke(UpdateInventory $message): void | ||
{ | ||
$productVariant = $this->productVariantRepository->find($message->productVariant); | ||
if (!$productVariant instanceof ProductVariantInterface) { | ||
throw new UnrecoverableMessageHandlingException(sprintf('Product variant with id %d not found', $message->productVariant)); | ||
} | ||
|
||
$productCode = $productVariant->getProduct()?->getCode(); | ||
$variantCode = $productVariant->getCode(); | ||
|
||
if (null === $productCode || null === $variantCode) { | ||
throw new UnrecoverableMessageHandlingException(sprintf('Product variant with id %d does not have a product code or variant code', $message->productVariant)); | ||
} | ||
|
||
$collection = $this | ||
->client | ||
->product() | ||
->getByProductId($productCode) | ||
->filter(fn (Product $product) => $product->variantId === $variantCode) | ||
; | ||
|
||
if (count($collection) === 0) { | ||
throw new UnrecoverableMessageHandlingException(sprintf('The product with id %s does not have a variant with id/code %s', $productCode, $variantCode)); | ||
} | ||
|
||
if (count($collection) > 1) { | ||
throw new UnrecoverableMessageHandlingException(sprintf('The product with id %s has multiple products with the same variant id/code', $productCode)); | ||
} | ||
|
||
$peakProduct = $collection[0]; | ||
|
||
if (null === $peakProduct->availableToSell) { | ||
throw new UnrecoverableMessageHandlingException(sprintf('The product with id %s and variant id/code %s does not have an availableToSell value', $productCode, $variantCode)); | ||
} | ||
|
||
$productVariant->setOnHand($peakProduct->availableToSell); | ||
|
||
$this->productVariantRepository->add($productVariant); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.