Skip to content

Commit

Permalink
Made the versioning more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Apr 5, 2024
1 parent ee46bbd commit e6f3e4b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 17 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"lexik/jwt-authentication-bundle": "^2.16",
"matthiasnoback/symfony-dependency-injection-test": "^4.3 || ^5.0",
"nyholm/psr7": "^1.8",
"phpspec/prophecy-phpunit": "^2.2",
"phpunit/phpunit": "^9.6",
"psalm/plugin-phpunit": "^0.18",
"psalm/plugin-symfony": "^5.1",
Expand Down
17 changes: 17 additions & 0 deletions src/Registrar/Webhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusShipmondoPlugin\Registrar;

final class Webhook
{
public function __construct(
public readonly string $name,
public readonly string $endpoint,
public readonly string $key,
public readonly string $action,
public readonly string $resource,
) {
}
}
32 changes: 16 additions & 16 deletions src/Registrar/WebhookRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,29 @@ public function register(): void

foreach ($this->getWebhooks() as $webhook) {
$this->client->webhooks()->create(new WebhookRequest(
$webhook['name'],
$webhook['endpoint'],
$webhook['key'],
$webhook['action'],
$webhook['resource'],
$webhook->name,
$webhook->endpoint,
$webhook->key,
$webhook->action,
$webhook->resource,
));
}
}

public function getVersion(): string
{
$webhooks = iterator_to_array($this->getWebhooks());
usort($webhooks, static fn (array $a, array $b) => $a['name'] <=> $b['name']);
usort($webhooks, static fn (Webhook $a, Webhook $b) => $a->name <=> $b->name);

$webhooks = array_map(static fn (array $webhook) => $webhook['name'] . $webhook['endpoint'] . $webhook['key'] . $webhook['action'] . $webhook['resource'], $webhooks);
$webhooks = array_map(static fn (Webhook $webhook) => $webhook->name . $webhook->endpoint . $webhook->key . $webhook->action . $webhook->resource, $webhooks);

return md5(implode('', $webhooks));
}

/**
* @return \Generator<array-key, array{name: string, endpoint: string, key: string, action: string, resource: string}>
* @return \Generator<array-key, Webhook>
*/
private function getWebhooks(): \Generator
public function getWebhooks(): \Generator
{
$resources = [
'Shipments' => ['create', 'cancel'],
Expand All @@ -60,9 +60,9 @@ private function getWebhooks(): \Generator

foreach ($resources as $resource => $actions) {
foreach ($actions as $action) {
yield [
'name' => sprintf('%s - [%s:%s]', $this->namePrefix, u($resource)->snake()->toString(), $action),
'endpoint' => $this->urlGenerator->generate(
yield new Webhook(
sprintf('%s - [%s:%s]', $this->namePrefix, u($resource)->snake()->toString(), $action),
$this->urlGenerator->generate(
'_webhook_controller',
[
'type' => 'shipmondo',
Expand All @@ -71,10 +71,10 @@ private function getWebhooks(): \Generator
],
UrlGeneratorInterface::ABSOLUTE_URL,
),
'key' => $this->webhooksKey,
'action' => $action,
'resource' => $resource,
];
$this->webhooksKey,
$action,
$resource,
);
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/Registrar/WebhookRegistrarInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ interface WebhookRegistrarInterface
public function register(): void;

/**
* Should return a version (string) that uniquely identifies the webhooks being registered.
* Will return a list of webhooks that this registrar will register
*
* @return iterable<Webhook>
*/
public function getWebhooks(): iterable;

/**
* Should return a version (string) that uniquely identifies the webhooks being registered
*/
public function getVersion(): string;
}
31 changes: 31 additions & 0 deletions tests/Registrar/WebhookRegistrarTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Tests\Setono\SyliusShipmondoPlugin\Registrar;

use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Setono\Shipmondo\Client\ClientInterface;
use Setono\SyliusShipmondoPlugin\Registrar\WebhookRegistrar;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

final class WebhookRegistrarTest extends TestCase
{
use ProphecyTrait;

/**
* @test
*/
public function it_returns_version(): void
{
$client = $this->prophesize(ClientInterface::class);
$urlGenerator = $this->prophesize(UrlGeneratorInterface::class);
$urlGenerator->generate('_webhook_controller', Argument::type('array'), UrlGeneratorInterface::ABSOLUTE_URL)->willReturn('https://example.com/webhook');

$registrar = new WebhookRegistrar($client->reveal(), $urlGenerator->reveal(), 'key', 'prefix');

self::assertSame('49fb4dab33cd8cf27e6a9d1944dcbbdb', $registrar->getVersion());
}
}

0 comments on commit e6f3e4b

Please sign in to comment.