Skip to content

Commit

Permalink
Add webhook consumer
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Jun 17, 2024
1 parent c7ed4a0 commit da0f137
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/Consumer/WebhookConsumer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Setono\PeakWMS\Consumer;

use CuyZ\Valinor\MapperBuilder;

final class WebhookConsumer implements WebhookConsumerInterface
{
private ?MapperBuilder $mapperBuilder = null;

public function consume(string $json, string $dataClass): object
{
return $this
->getMapperBuilder()
->mapper()
->map(
$dataClass,
json_decode($json, true, 512, \JSON_THROW_ON_ERROR),
);
}

public function setMapperBuilder(MapperBuilder $mapperBuilder): void
{
$this->mapperBuilder = $mapperBuilder;
}

public function getMapperBuilder(): MapperBuilder
{
if (null === $this->mapperBuilder) {
$this->mapperBuilder = (new MapperBuilder())
->allowSuperfluousKeys()
;
}

return $this->mapperBuilder;
}
}
17 changes: 17 additions & 0 deletions src/Consumer/WebhookConsumerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Setono\PeakWMS\Consumer;

interface WebhookConsumerInterface
{
/**
* @template T
*
* @param class-string<T> $dataClass
*
* @return T
*/
public function consume(string $json, string $dataClass): object;
}
21 changes: 21 additions & 0 deletions src/DataTransferObject/Webhook/WebhookDataStockAdjust.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Setono\PeakWMS\DataTransferObject\Webhook;

final class WebhookDataStockAdjust
{
public function __construct(
public string $productId,
public int $adjustedQuantity,
public int $quantity,
public ?string $variantId = null,
public ?string $stockHostIdentifier = null,
public ?string $warehouseHostId = null,
public ?string $lotNumber = null,
public ?int $adjustmentReason = null,
public ?int $stockItemId = null,
) {
}
}

0 comments on commit da0f137

Please sign in to comment.