Skip to content

Commit

Permalink
Merge pull request #6 from Setono/rename-registered-webhooks
Browse files Browse the repository at this point in the history
Renamed RegisteredWebhooks entity to WebhookRegistration
  • Loading branch information
loevgaard authored Aug 12, 2024
2 parents bb2e586 + 1574f44 commit 0b54e38
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ jobs:
run: "composer config --unset require-dev"

- name: "Add shipmonk/composer-dependency-analyser to composer.json"
run: "composer require --dev --no-install --no-plugins --no-scripts shipmonk/composer-dependency-analyser"
run: "composer require --dev --no-install --no-update --no-plugins --no-scripts shipmonk/composer-dependency-analyser"

- name: "Install composer dependencies"
uses: "ramsey/composer-install@v3"
Expand Down
6 changes: 3 additions & 3 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace Setono\SyliusPeakPlugin\DependencyInjection;

use Setono\SyliusPeakPlugin\Model\InventoryUpdate;
use Setono\SyliusPeakPlugin\Model\RegisteredWebhooks;
use Setono\SyliusPeakPlugin\Model\UploadOrderRequest;
use Setono\SyliusPeakPlugin\Model\UploadProductVariantRequest;
use Setono\SyliusPeakPlugin\Model\WebhookRegistration;
use Sylius\Component\Resource\Factory\Factory;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
Expand Down Expand Up @@ -64,14 +64,14 @@ private function addResourcesSection(ArrayNodeDefinition $node): void
->end()
->end()
->end()
->arrayNode('registered_webhooks') // todo rename to webhook_registration?
->arrayNode('webhook_registration')
->addDefaultsIfNotSet()
->children()
->variableNode('options')->end()
->arrayNode('classes')
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(RegisteredWebhooks::class)->cannotBeEmpty()->end()
->scalarNode('model')->defaultValue(WebhookRegistration::class)->cannotBeEmpty()->end()
->scalarNode('repository')->cannotBeEmpty()->end()
->scalarNode('factory')->defaultValue(Factory::class)->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@

namespace Setono\SyliusPeakPlugin\Factory;

use Setono\SyliusPeakPlugin\Model\RegisteredWebhooksInterface;
use Setono\SyliusPeakPlugin\Model\WebhookRegistrationInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

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

/** @psalm-suppress MoreSpecificReturnType */
public function createNew(): RegisteredWebhooksInterface
public function createNew(): WebhookRegistrationInterface
{
/** @psalm-suppress LessSpecificReturnStatement */
return $this->decoratedFactory->createNew();
}

public function createFromData(string $version, array $webhooks): RegisteredWebhooksInterface
public function createFromData(string $version, array $webhooks): WebhookRegistrationInterface
{
$obj = $this->createNew();
$obj->setVersion($version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
namespace Setono\SyliusPeakPlugin\Factory;

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

/**
* @extends FactoryInterface<RegisteredWebhooksInterface>
* @extends FactoryInterface<WebhookRegistrationInterface>
*/
interface RegisteredWebhooksFactoryInterface extends FactoryInterface
interface WebhookRegistrationFactoryInterface extends FactoryInterface
{
public function createNew(): RegisteredWebhooksInterface;
public function createNew(): WebhookRegistrationInterface;

/**
* @param list<Webhook> $webhooks
*/
public function createFromData(string $version, array $webhooks): RegisteredWebhooksInterface;
public function createFromData(string $version, array $webhooks): WebhookRegistrationInterface;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Setono\SyliusPeakPlugin\Model;

class RegisteredWebhooks implements RegisteredWebhooksInterface
class WebhookRegistration implements WebhookRegistrationInterface
{
protected ?int $id = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Sylius\Component\Resource\Model\ResourceInterface;

interface RegisteredWebhooksInterface extends ResourceInterface
interface WebhookRegistrationInterface extends ResourceInterface
{
public function getId(): ?int;

Expand Down
28 changes: 14 additions & 14 deletions src/Registrar/WebhookRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use Setono\PeakWMS\DataTransferObject\Webhook\Name;
use Setono\PeakWMS\DataTransferObject\Webhook\Webhook;
use Setono\SyliusPeakPlugin\Exception\WebhookRegistrationException;
use Setono\SyliusPeakPlugin\Factory\RegisteredWebhooksFactoryInterface;
use Setono\SyliusPeakPlugin\Model\RegisteredWebhooksInterface;
use Setono\SyliusPeakPlugin\Factory\WebhookRegistrationFactoryInterface;
use Setono\SyliusPeakPlugin\Model\WebhookRegistrationInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

Expand All @@ -18,8 +18,8 @@ final class WebhookRegistrar implements WebhookRegistrarInterface
public function __construct(
private readonly ClientInterface $client,
private readonly UrlGeneratorInterface $urlGenerator,
private readonly RepositoryInterface $registeredWebhooksRepository,
private readonly RegisteredWebhooksFactoryInterface $registeredWebhooksFactory,
private readonly RepositoryInterface $webhookRegistrationRepository,
private readonly WebhookRegistrationFactoryInterface $webhookRegistrationFactory,
) {
}

Expand All @@ -28,19 +28,19 @@ public function register(): void
/**
* This will delete all webhooks registered with Peak WMS and also remove the logs from the database
*
* @var RegisteredWebhooksInterface $registeredWebhooks
* @var WebhookRegistrationInterface $webhookRegistration
*/
foreach ($this->registeredWebhooksRepository->findAll() as $registeredWebhooks) {
foreach ($this->webhookRegistrationRepository->findAll() as $webhookRegistration) {
/** @var mixed $webhook */
foreach ($registeredWebhooks->getWebhooks() as $webhook) {
foreach ($webhookRegistration->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);
$this->webhookRegistrationRepository->remove($webhookRegistration);
}

$postedWebhooks = [];
Expand All @@ -49,21 +49,21 @@ public function register(): void
$postedWebhooks[] = $this->client->webhook()->create($webhook);
}

$registeredWebhooks = $this->registeredWebhooksFactory->createFromData($this->getVersion(), $postedWebhooks);
$this->registeredWebhooksRepository->add($registeredWebhooks);
$webhookRegistration = $this->webhookRegistrationFactory->createFromData($this->getVersion(), $postedWebhooks);
$this->webhookRegistrationRepository->add($webhookRegistration);
}

public function outOfDate(): bool
{
/** @var list<RegisteredWebhooksInterface> $registeredWebhooks */
$registeredWebhooks = $this->registeredWebhooksRepository->findAll();
if (count($registeredWebhooks) !== 1) {
/** @var list<WebhookRegistrationInterface> $webhookRegistrations */
$webhookRegistrations = $this->webhookRegistrationRepository->findAll();
if (count($webhookRegistrations) !== 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();
return $webhookRegistrations[0]->getVersion() !== $this->getVersion();
}

private function getVersion(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<mapped-superclass name="Setono\SyliusPeakPlugin\Model\RegisteredWebhooks"
table="setono_sylius_peak_wms__registered_webhooks">
<mapped-superclass name="Setono\SyliusPeakPlugin\Model\WebhookRegistration"
table="setono_sylius_peak_wms__webhook_registration">
<id name="id" type="integer">
<generator strategy="AUTO"/>
</id>
Expand Down
6 changes: 3 additions & 3 deletions src/Resources/config/services/factory.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<services>
<!-- todo missing aliases -->

<service id="Setono\SyliusPeakPlugin\Factory\RegisteredWebhooksFactory"
decorates="setono_sylius_peak.factory.registered_webhooks" decoration-priority="64">
<argument type="service" id="Setono\SyliusPeakPlugin\Factory\RegisteredWebhooksFactory.inner"/>
<service id="Setono\SyliusPeakPlugin\Factory\WebhookRegistrationFactory"
decorates="setono_sylius_peak.factory.webhook_registration" decoration-priority="64">
<argument type="service" id="Setono\SyliusPeakPlugin\Factory\WebhookRegistrationFactory.inner"/>
</service>

<service id="Setono\SyliusPeakPlugin\Factory\UploadOrderRequestFactory"
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/config/services/registrar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<service id="Setono\SyliusPeakPlugin\Registrar\WebhookRegistrar">
<argument type="service" id="Setono\PeakWMS\Client\ClientInterface"/>
<argument type="service" id="router"/>
<argument type="service" id="setono_sylius_peak.repository.registered_webhooks"/>
<argument type="service" id="setono_sylius_peak.factory.registered_webhooks"/>
<argument type="service" id="setono_sylius_peak.repository.webhook_registration"/>
<argument type="service" id="setono_sylius_peak.factory.webhook_registration"/>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public function after_loading_the_correct_parameter_has_been_set(): void
{
$this->load();

$this->assertContainerBuilderHasParameter('setono_sylius_peak.api_key', '%env(PEAK_WMS_API_KEY)%');
$this->assertContainerBuilderHasParameter('setono_sylius_peak.api.api_key', '%env(PEAK_WMS_API_KEY)%');
}
}

0 comments on commit 0b54e38

Please sign in to comment.