Skip to content

Commit

Permalink
Add helper method for resolving webhook data classes
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Jun 17, 2024
1 parent da0f137 commit eb4397d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Consumer/WebhookConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Setono\PeakWMS\Consumer;

use CuyZ\Valinor\MapperBuilder;
use Setono\PeakWMS\DataTransferObject\Webhook\Name;
use Setono\PeakWMS\DataTransferObject\Webhook\WebhookDataStockAdjust;

final class WebhookConsumer implements WebhookConsumerInterface
{
Expand Down Expand Up @@ -36,4 +38,19 @@ public function getMapperBuilder(): MapperBuilder

return $this->mapperBuilder;
}

/**
* @return class-string
*/
public static function convertNameToDataClass(Name|int $name): string
{
if (is_int($name)) {
$name = Name::from($name);
}

return match ($name) {

Check warning on line 51 in src/Consumer/WebhookConsumer.php

View workflow job for this annotation

GitHub Actions / Mutation tests (8.3, highest)

Escaped Mutant for Mutator "MatchArmRemoval": --- Original +++ New @@ @@ } return match ($name) { Name::StockAdjust => WebhookDataStockAdjust::class, - default => throw new \InvalidArgumentException(sprintf('The name "%d" is not supported', $name->value)), }; } }
Name::StockAdjust => WebhookDataStockAdjust::class,
default => throw new \InvalidArgumentException(sprintf('The name "%d" is not supported', $name->value)),
};
}
}
21 changes: 21 additions & 0 deletions tests/Consumer/WebhookConsumerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Setono\PeakWMS\Consumer;

use PHPUnit\Framework\TestCase;
use Setono\PeakWMS\DataTransferObject\Webhook\Name;
use Setono\PeakWMS\DataTransferObject\Webhook\WebhookDataStockAdjust;

final class WebhookConsumerTest extends TestCase
{
/**
* @test
*/
public function it_converts_name_to_data_class(): void
{
self::assertSame(WebhookDataStockAdjust::class, WebhookConsumer::convertNameToDataClass(100));
self::assertSame(WebhookDataStockAdjust::class, WebhookConsumer::convertNameToDataClass(Name::StockAdjust));
}
}

0 comments on commit eb4397d

Please sign in to comment.