-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
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); | ||
} | ||
} |
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.
This file was deleted.
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; | ||
} | ||
} |
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.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusPeakWMSPlugin\Provider; | ||
|
||
use Doctrine\Persistence\ManagerRegistry; | ||
use Setono\Doctrine\ORMTrait; | ||
use Setono\PeakWMS\DataTransferObject\Webhook\WebhookDataStockAdjust; | ||
use Sylius\Component\Core\Model\ProductVariantInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class ProductVariantProvider implements ProductVariantProviderInterface | ||
{ | ||
use ORMTrait; | ||
|
||
public function __construct( | ||
ManagerRegistry $managerRegistry, | ||
/** @var class-string<ProductVariantInterface> $productVariantClass */ | ||
private readonly string $productVariantClass, | ||
) { | ||
$this->managerRegistry = $managerRegistry; | ||
} | ||
|
||
public function provideFromStockAdjustment(WebhookDataStockAdjust $stockAdjustment): ?ProductVariantInterface | ||
Check failure on line 25 in src/Provider/ProductVariantProvider.php GitHub Actions / Static Code Analysis (PHP8.2 | Deps: lowest | SF~6.4.0)MixedInferredReturnType
Check failure on line 25 in src/Provider/ProductVariantProvider.php GitHub Actions / Static Code Analysis (PHP8.1 | Deps: lowest | SF~6.4.0)MixedInferredReturnType
|
||
{ | ||
$obj = $this | ||
Check failure on line 27 in src/Provider/ProductVariantProvider.php GitHub Actions / Static Code Analysis (PHP8.2 | Deps: lowest | SF~6.4.0)MixedAssignment
Check failure on line 27 in src/Provider/ProductVariantProvider.php GitHub Actions / Static Code Analysis (PHP8.1 | Deps: lowest | SF~6.4.0)MixedAssignment
|
||
->getRepository($this->productVariantClass) | ||
->createQueryBuilder('o') | ||
->where('o.code = :code') | ||
->setParameter('code', $stockAdjustment->variantId) | ||
->getQuery() | ||
->getOneOrNullResult() | ||
; | ||
Assert::nullOrIsInstanceOf($obj, ProductVariantInterface::class); | ||
|
||
return $obj; | ||
Check failure on line 37 in src/Provider/ProductVariantProvider.php GitHub Actions / Static Code Analysis (PHP8.2 | Deps: lowest | SF~6.4.0)MixedReturnStatement
Check failure on line 37 in src/Provider/ProductVariantProvider.php GitHub Actions / Static Code Analysis (PHP8.1 | Deps: lowest | SF~6.4.0)MixedReturnStatement
|
||
} | ||
} |