Skip to content

Commit

Permalink
Log http messages
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Jun 19, 2024
1 parent a8107ad commit d8ebc9b
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 9 deletions.
59 changes: 52 additions & 7 deletions src/Message/CommandHandler/ProcessUploadOrderRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Setono\SyliusPeakWMSPlugin\Message\CommandHandler;

use Doctrine\Persistence\ManagerRegistry;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Setono\Doctrine\ORMTrait;
use Setono\PeakWMS\Client\ClientInterface;
use Setono\PeakWMS\DataTransferObject\SalesOrder\SalesOrder;
Expand Down Expand Up @@ -34,7 +36,9 @@ public function __construct(

public function __invoke(ProcessUploadOrderRequest $message): void
{
$uploadOrderRequest = $this->getManager($this->uploadOrderRequestClass)->find($this->uploadOrderRequestClass, $message->uploadOrderRequest);
$manager = $this->getManager($this->uploadOrderRequestClass);

$uploadOrderRequest = $manager->find($this->uploadOrderRequestClass, $message->uploadOrderRequest);
if (!$uploadOrderRequest instanceof UploadOrderRequestInterface) {
throw new UnrecoverableMessageHandlingException(sprintf('Upload order request with id %d does not exist', $message->uploadOrderRequest));
}
Expand All @@ -43,18 +47,59 @@ public function __invoke(ProcessUploadOrderRequest $message): void
throw new UnrecoverableMessageHandlingException(sprintf('Upload order request with id %d has been updated since it was tried to be processed', $message->uploadOrderRequest));
}

// todo try catch exceptions and log errors

$order = $uploadOrderRequest->getOrder();
if (null === $order) {
throw new UnrecoverableMessageHandlingException(sprintf('The upload order request with id %d does not have an associated order', $message->uploadOrderRequest));
}

$salesOrder = new SalesOrder();
$this->salesOrderDataMapper->map($order, $salesOrder);
try {
$salesOrder = new SalesOrder();
$this->salesOrderDataMapper->map($order, $salesOrder);

$this->peakWMSClient->salesOrder()->create($salesOrder);

$this->uploadOrderRequestWorkflow->apply($order, UploadOrderRequestWorkflow::TRANSITION_UPLOAD);
} catch (\Throwable $e) {
$uploadOrderRequest->setError($e->getMessage());

$this->uploadOrderRequestWorkflow->apply($order, UploadOrderRequestWorkflow::TRANSITION_FAIL);

throw new UnrecoverableMessageHandlingException(
message: sprintf('Failed to process upload order request with id %d', $message->uploadOrderRequest),
previous: $e,
);
} finally {
$uploadOrderRequest->setRequest(self::stringifyMessage($this->peakWMSClient->getLastRequest()));
$uploadOrderRequest->setResponse(self::stringifyMessage($this->peakWMSClient->getLastResponse()));
$manager->flush();
}
}

private static function stringifyMessage(RequestInterface|ResponseInterface|null $message): ?string
{
if (null === $message) {
return null;
}

$result = '';
if ($message instanceof RequestInterface) {
$result = sprintf(
"%s %s HTTP/%s\n",
$message->getMethod(),
$message->getUri(),
$message->getProtocolVersion(),
);
}

foreach ($message->getHeaders() as $name => $values) {
$result .= sprintf("%s: %s\n", $name, implode(', ', $values));
}

$this->peakWMSClient->salesOrder()->create($salesOrder);
$body = trim((string) $message->getBody());
if ('' !== $body) {
$result .= "\n\n" . $body;
}

$this->uploadOrderRequestWorkflow->apply($order, UploadOrderRequestWorkflow::TRANSITION_UPLOAD);
return $result;
}
}
36 changes: 36 additions & 0 deletions src/Model/UploadOrderRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class UploadOrderRequest implements UploadOrderRequestInterface

protected ?OrderInterface $order = null;

protected ?string $request = null;

protected ?string $response = null;

protected ?string $error = null;

public function getId(): ?int
{
return $this->id;
Expand Down Expand Up @@ -48,4 +54,34 @@ public function setOrder(?OrderInterface $order): void
{
$this->order = $order;
}

public function getRequest(): ?string
{
return $this->request;
}

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

public function getResponse(): ?string
{
return $this->response;
}

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

public function getError(): ?string
{
return $this->error;
}

public function setError(?string $error): void
{
$this->error = $error;
}
}
12 changes: 12 additions & 0 deletions src/Model/UploadOrderRequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,16 @@ public function setState(string $state): void;
public function getOrder(): ?OrderInterface;

public function setOrder(?OrderInterface $order): void;

public function getRequest(): ?string;

public function setRequest(?string $request): void;

public function getResponse(): ?string;

public function setResponse(?string $response): void;

public function getError(): ?string;

public function setError(?string $error): void;
}
2 changes: 0 additions & 2 deletions src/Processor/UploadOrderRequestProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public function __construct(

public function process(): void
{
// todo: check for eligibility

foreach ($this->preQualifiedUploadableOrdersProvider->getUploadOrderRequests() as $uploadOrderRequest) {
try {
$this->uploadOrderRequestWorkflow->apply($uploadOrderRequest, UploadOrderRequestWorkflow::TRANSITION_PROCESS);
Expand Down

0 comments on commit d8ebc9b

Please sign in to comment.