Skip to content

Commit

Permalink
Cleaned up a few things
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Jun 13, 2024
1 parent c26a40f commit 4cb5dcf
Show file tree
Hide file tree
Showing 31 changed files with 246 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
name: 'setono:sylius-peak-wms:upload-orders',
description: 'Upload orders to Peak WMS',
name: 'setono:sylius-peak-wms:process',
description: 'Processes upload order requests',
)]
final class UploadOrdersCommand extends Command
final class ProcessCommand extends Command
{
public function __construct(private readonly UploadOrderRequestProcessorInterface $orderUploader)
public function __construct(private readonly UploadOrderRequestProcessorInterface $uploadOrderRequestProcessor)
{
parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->orderUploader->process();
$this->uploadOrderRequestProcessor->process();

return 0;
}
Expand Down
7 changes: 2 additions & 5 deletions src/Controller/Admin/PeakWMSController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,23 @@

use Setono\SyliusPeakWMSPlugin\Message\Command\RegisterWebhooks;
use Setono\SyliusPeakWMSPlugin\Registrar\WebhookRegistrarInterface;
use Setono\SyliusPeakWMSPlugin\Repository\RegisteredWebhooksRepositoryInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;

final class PeakWMSController extends AbstractController
{
public function __construct(
private readonly RegisteredWebhooksRepositoryInterface $registeredWebhooksRepository,
private readonly MessageBusInterface $commandBus,
private readonly WebhookRegistrarInterface $webhookRegistrar,
) {
}

public function index(Request $request): Response
public function index(): Response
{
return $this->render('@SetonoSyliusPeakWMSPlugin/admin/peak_wms/index.html.twig', [
'registeredWebhooks' => $this->registeredWebhooksRepository->findOneByVersion($this->webhookRegistrar->getVersion()),
'webhooksShouldBeRegistered' => $this->webhookRegistrar->outOfDate(),
]);
}

Expand Down
14 changes: 14 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Setono\SyliusPeakWMSPlugin\Model\RegisteredWebhooks;
use Setono\SyliusPeakWMSPlugin\Model\RemoteEvent;
use Setono\SyliusPeakWMSPlugin\Model\UploadOrderRequest;
use Setono\SyliusPeakWMSPlugin\Repository\RegisteredWebhooksRepository;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Sylius\Bundle\ResourceBundle\Form\Type\DefaultResourceType;
Expand Down Expand Up @@ -71,6 +72,19 @@ private function addResourcesSection(ArrayNodeDefinition $node): void
->scalarNode('repository')->cannotBeEmpty()->end()
->scalarNode('form')->defaultValue(DefaultResourceType::class)->end()
->scalarNode('factory')->defaultValue(Factory::class)->end()
->end()
->end()
->end()
->end()
->arrayNode('upload_order_request')
->addDefaultsIfNotSet()
->children()
->variableNode('options')->end()
->arrayNode('classes')
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(UploadOrderRequest::class)->cannotBeEmpty()->end()
->scalarNode('factory')->defaultValue(Factory::class)->end()
;
}
}
2 changes: 1 addition & 1 deletion src/DependencyInjection/SetonoSyliusPeakWMSExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function load(array $configs, ContainerBuilder $container): void
->addTag('setono_sylius_peak_wms.sales_order_data_mapper')
;

$container->setParameter('setono_sylius_peak_wms.api.key', $config['api_key']);
$container->setParameter('setono_sylius_peak_wms.api_key', $config['api_key']);

$this->registerResources(
'setono_sylius_peak_wms',
Expand Down
9 changes: 9 additions & 0 deletions src/Exception/WebhookRegistrationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusPeakWMSPlugin\Exception;

final class WebhookRegistrationException extends \RuntimeException
{
}
31 changes: 31 additions & 0 deletions src/Factory/RegisteredWebhooksFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusPeakWMSPlugin\Factory;

use Setono\SyliusPeakWMSPlugin\Model\RegisteredWebhooksInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

final class RegisteredWebhooksFactory implements RegisteredWebhooksFactoryInterface
{
public function __construct(
/** @var FactoryInterface<RegisteredWebhooksInterface> $decoratedFactory */
private readonly FactoryInterface $decoratedFactory,

Check failure on line 14 in src/Factory/RegisteredWebhooksFactory.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.2 | Deps: lowest | SF~5.4.0)

TooManyTemplateParams

src/Factory/RegisteredWebhooksFactory.php:14:9: TooManyTemplateParams: Sylius\Component\Resource\Factory\FactoryInterface<Setono\SyliusPeakWMSPlugin\Model\RegisteredWebhooksInterface> has too many template params, expecting 0 (see https://psalm.dev/184)

Check failure on line 14 in src/Factory/RegisteredWebhooksFactory.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.1 | Deps: lowest | SF~5.4.0)

TooManyTemplateParams

src/Factory/RegisteredWebhooksFactory.php:14:9: TooManyTemplateParams: Sylius\Component\Resource\Factory\FactoryInterface<Setono\SyliusPeakWMSPlugin\Model\RegisteredWebhooksInterface> has too many template params, expecting 0 (see https://psalm.dev/184)
) {
}

public function createNew(): RegisteredWebhooksInterface

Check failure on line 18 in src/Factory/RegisteredWebhooksFactory.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.2 | Deps: lowest | SF~5.4.0)

MoreSpecificReturnType

src/Factory/RegisteredWebhooksFactory.php:18:34: MoreSpecificReturnType: The declared return type 'Setono\SyliusPeakWMSPlugin\Model\RegisteredWebhooksInterface' for Setono\SyliusPeakWMSPlugin\Factory\RegisteredWebhooksFactory::createNew is more specific than the inferred return type 'object' (see https://psalm.dev/070)

Check failure on line 18 in src/Factory/RegisteredWebhooksFactory.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.1 | Deps: lowest | SF~5.4.0)

MoreSpecificReturnType

src/Factory/RegisteredWebhooksFactory.php:18:34: MoreSpecificReturnType: The declared return type 'Setono\SyliusPeakWMSPlugin\Model\RegisteredWebhooksInterface' for Setono\SyliusPeakWMSPlugin\Factory\RegisteredWebhooksFactory::createNew is more specific than the inferred return type 'object' (see https://psalm.dev/070)
{
return $this->decoratedFactory->createNew();

Check failure on line 20 in src/Factory/RegisteredWebhooksFactory.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.2 | Deps: lowest | SF~5.4.0)

LessSpecificReturnStatement

src/Factory/RegisteredWebhooksFactory.php:20:16: LessSpecificReturnStatement: The type 'object' is more general than the declared return type 'Setono\SyliusPeakWMSPlugin\Model\RegisteredWebhooksInterface' for Setono\SyliusPeakWMSPlugin\Factory\RegisteredWebhooksFactory::createNew (see https://psalm.dev/129)

Check failure on line 20 in src/Factory/RegisteredWebhooksFactory.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.1 | Deps: lowest | SF~5.4.0)

LessSpecificReturnStatement

src/Factory/RegisteredWebhooksFactory.php:20:16: LessSpecificReturnStatement: The type 'object' is more general than the declared return type 'Setono\SyliusPeakWMSPlugin\Model\RegisteredWebhooksInterface' for Setono\SyliusPeakWMSPlugin\Factory\RegisteredWebhooksFactory::createNew (see https://psalm.dev/129)
}

public function createFromData(string $version, array $webhooks): RegisteredWebhooksInterface
{
$obj = $this->createNew();
$obj->setVersion($version);
$obj->setWebhooks($webhooks);

return $obj;
}
}
22 changes: 22 additions & 0 deletions src/Factory/RegisteredWebhooksFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusPeakWMSPlugin\Factory;

use Setono\PeakWMS\DataTransferObject\Webhook\Webhook;
use Setono\SyliusPeakWMSPlugin\Model\RegisteredWebhooksInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

/**
* @extends FactoryInterface<RegisteredWebhooksInterface>
*/
interface RegisteredWebhooksFactoryInterface extends FactoryInterface

Check failure on line 14 in src/Factory/RegisteredWebhooksFactoryInterface.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.2 | Deps: lowest | SF~5.4.0)

TooManyTemplateParams

src/Factory/RegisteredWebhooksFactoryInterface.php:14:54: TooManyTemplateParams: Setono\SyliusPeakWMSPlugin\Factory\RegisteredWebhooksFactoryInterface has too many template params when extending Sylius\Component\Resource\Factory\FactoryInterface, expecting 0 (see https://psalm.dev/184)

Check failure on line 14 in src/Factory/RegisteredWebhooksFactoryInterface.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (PHP8.1 | Deps: lowest | SF~5.4.0)

TooManyTemplateParams

src/Factory/RegisteredWebhooksFactoryInterface.php:14:54: TooManyTemplateParams: Setono\SyliusPeakWMSPlugin\Factory\RegisteredWebhooksFactoryInterface has too many template params when extending Sylius\Component\Resource\Factory\FactoryInterface, expecting 0 (see https://psalm.dev/184)
{
public function createNew(): RegisteredWebhooksInterface;

/**
* @param list<Webhook> $webhooks
*/
public function createFromData(string $version, array $webhooks): RegisteredWebhooksInterface;
}
23 changes: 2 additions & 21 deletions src/Message/CommandHandler/RegisterWebhooksHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,16 @@
namespace Setono\SyliusPeakWMSPlugin\Message\CommandHandler;

use Setono\SyliusPeakWMSPlugin\Message\Command\RegisterWebhooks;
use Setono\SyliusPeakWMSPlugin\Model\RegisteredWebhooksInterface;
use Setono\SyliusPeakWMSPlugin\Registrar\WebhookRegistrarInterface;
use Setono\SyliusPeakWMSPlugin\Repository\RegisteredWebhooksRepositoryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

final class RegisterWebhooksHandler
{
public function __construct(
private readonly RegisteredWebhooksRepositoryInterface $registeredWebhooksRepository,
private readonly WebhookRegistrarInterface $webhookRegistrar,
/** @var FactoryInterface<RegisteredWebhooksInterface> $registeredWebhooksFactory */
private readonly FactoryInterface $registeredWebhooksFactory,
) {
public function __construct(private readonly WebhookRegistrarInterface $webhookRegistrar)
{
}

public function __invoke(RegisterWebhooks $message): void
{
$this->webhookRegistrar->register();

$version = $this->webhookRegistrar->getVersion();

$registeredWebhooks = $this->registeredWebhooksRepository->findOneByVersion($version);
if (null === $registeredWebhooks) {
$registeredWebhooks = $this->registeredWebhooksFactory->createNew();
}

$registeredWebhooks->setRegisteredAt(new \DateTimeImmutable());
$registeredWebhooks->setVersion($this->webhookRegistrar->getVersion());

$this->registeredWebhooksRepository->add($registeredWebhooks);
}
}
23 changes: 20 additions & 3 deletions src/Model/RegisteredWebhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ class RegisteredWebhooks implements RegisteredWebhooksInterface

protected ?string $version = null;

protected ?\DateTimeInterface $registeredAt = null;
protected array $webhooks = [];

protected \DateTimeInterface $registeredAt;

public function __construct()
{
$this->registeredAt = new \DateTimeImmutable();
}

public function getId(): ?int
{
Expand All @@ -27,12 +34,22 @@ public function setVersion(?string $version): void
$this->version = $version;
}

public function getRegisteredAt(): ?\DateTimeInterface
public function getWebhooks(): array
{
return $this->webhooks;
}

public function setWebhooks(array $webhooks): void
{
$this->webhooks = $webhooks;
}

public function getRegisteredAt(): \DateTimeInterface
{
return $this->registeredAt;
}

public function setRegisteredAt(?\DateTimeInterface $registeredAt): void
public function setRegisteredAt(\DateTimeInterface $registeredAt): void
{
$this->registeredAt = $registeredAt;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Model/RegisteredWebhooksInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public function getVersion(): ?string;

public function setVersion(?string $version): void;

public function getWebhooks(): array;

public function setWebhooks(array $webhooks): void;

public function getRegisteredAt(): ?\DateTimeInterface;

public function setRegisteredAt(\DateTimeInterface $registeredAt): void;
Expand Down
51 changes: 51 additions & 0 deletions src/Model/UploadOrderRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusPeakWMSPlugin\Model;

class UploadOrderRequest implements UploadOrderRequestInterface
{
protected ?int $id = null;

protected ?int $version = null;

protected ?string $state = null;

protected ?OrderInterface $order = null;

public function getId(): ?int
{
return $this->id;
}

public function getVersion(): ?int
{
return $this->version;
}

public function setVersion(?int $version): void
{
$this->version = $version;
}

public function getState(): ?string
{
return $this->state;
}

public function setState(?string $state): void
{
$this->state = $state;
}

public function getOrder(): ?OrderInterface
{
return $this->order;
}

public function setOrder(?OrderInterface $order): void
{
$this->order = $order;
}
}
2 changes: 2 additions & 0 deletions src/Model/UploadOrderRequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ public function getState(): ?string;
public function setState(string $state): void;

public function getOrder(): ?OrderInterface;

public function setOrder(?OrderInterface $order): void;
}
47 changes: 41 additions & 6 deletions src/Registrar/WebhookRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,59 @@
use Setono\PeakWMS\Client\ClientInterface;
use Setono\PeakWMS\DataTransferObject\Webhook\Name;
use Setono\PeakWMS\DataTransferObject\Webhook\Webhook;
use Setono\SyliusPeakWMSPlugin\Exception\WebhookRegistrationException;
use Setono\SyliusPeakWMSPlugin\Factory\RegisteredWebhooksFactoryInterface;
use Setono\SyliusPeakWMSPlugin\Repository\RegisteredWebhooksRepositoryInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

final class WebhookRegistrar implements WebhookRegistrarInterface
{
public function __construct(
private readonly ClientInterface $client,
private readonly UrlGeneratorInterface $urlGenerator,
private readonly RegisteredWebhooksRepositoryInterface $registeredWebhooksRepository,
private readonly RegisteredWebhooksFactoryInterface $registeredWebhooksFactory,
) {
}

public function register(): void
{
// todo delete all registered webhooks first
// This will delete all webhooks registered with Peak WMS and also remove the logs from the database
foreach ($this->registeredWebhooksRepository->findAll() as $registeredWebhooks) {
/** @var mixed $webhook */
foreach ($registeredWebhooks->getWebhooks() as $webhook) {
if (!is_array($webhook) || !isset($webhook['id']) || !is_int($webhook['id'])) {
throw new WebhookRegistrationException('The webhooks are not in the correct format');
}

$this->client->webhook()->delete($webhook['id']);
}

$this->registeredWebhooksRepository->remove($registeredWebhooks);
}

foreach ($this->getWebhooks() as $webhook) {
$this->client->webhook()->create($webhook);
}

$registeredWebhooks = $this->registeredWebhooksFactory->createFromData($this->getVersion(), $this->getWebhooks());
$this->registeredWebhooksRepository->add($registeredWebhooks);
}

public function getVersion(): string
public function outOfDate(): bool
{
$webhooks = iterator_to_array($this->getWebhooks());
$registeredWebhooks = $this->registeredWebhooksRepository->findAll();
if (count($registeredWebhooks) !== 1) {
// We should only have one registered webhooks object. If we have more, it's a bug, and we consider it out of date. If we have none, we consider it out of date because they need to be registered.
return true;
}

return $registeredWebhooks[0]->getVersion() !== $this->getVersion();
}

private function getVersion(): string
{
$webhooks = $this->getWebhooks();
usort($webhooks, static fn (Webhook $a, Webhook $b) => $a->name?->value <=> $b->name?->value);

$webhooks = array_map(static fn (Webhook $webhook) => (string) $webhook->name?->value . (string) $webhook->url, $webhooks);
Expand All @@ -37,18 +68,22 @@ public function getVersion(): string
}

/**
* @return \Generator<array-key, Webhook>
* @return list<Webhook>
*/
public function getWebhooks(): \Generator
private function getWebhooks(): array
{
$webhooks = [];

foreach ([Name::StockAdjust, Name::PickOrderFullyPacked] as $name) {
yield new Webhook(
$webhooks[] = new Webhook(
name: $name,
url: $this->urlGenerator->generate(
name: 'setono_sylius_peak_wms_global_webhook',
referenceType: UrlGeneratorInterface::ABSOLUTE_URL,
),
);
}

return $webhooks;
}
}
Loading

0 comments on commit 4cb5dcf

Please sign in to comment.