-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusPeakPlugin\Factory; | ||
|
||
use Setono\SyliusPeakPlugin\Model\WebhookInterface; | ||
use Sylius\Component\Resource\Factory\FactoryInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class WebhookFactory implements WebhookFactoryInterface | ||
{ | ||
public function __construct(private readonly FactoryInterface $decorated) | ||
{ | ||
} | ||
|
||
public function createNew(): WebhookInterface | ||
{ | ||
$obj = $this->decorated->createNew(); | ||
Assert::isInstanceOf($obj, WebhookInterface::class); | ||
|
||
return $obj; | ||
} | ||
|
||
public function createFromRequest(Request $request): WebhookInterface | ||
{ | ||
$obj = $this->createNew(); | ||
$obj->setMethod($request->getMethod()); | ||
$obj->setUrl($request->getUri()); | ||
$obj->setHeaders($request->headers->all()); | ||
Check failure on line 31 in src/Factory/WebhookFactory.php GitHub Actions / Static Code Analysis (PHP8.1 | Deps: lowest | SF~5.4.0)InvalidArgument
Check failure on line 31 in src/Factory/WebhookFactory.php GitHub Actions / Static Code Analysis (PHP8.1 | Deps: highest | SF~5.4.0)InvalidArgument
Check failure on line 31 in src/Factory/WebhookFactory.php GitHub Actions / Static Code Analysis (PHP8.2 | Deps: lowest | SF~5.4.0)InvalidArgument
Check failure on line 31 in src/Factory/WebhookFactory.php GitHub Actions / Static Code Analysis (PHP8.2 | Deps: highest | SF~5.4.0)InvalidArgument
|
||
$obj->setBody($request->getContent()); | ||
$obj->setRemoteIp($request->getClientIp()); | ||
|
||
return $obj; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusPeakPlugin\Factory; | ||
|
||
use Setono\SyliusPeakPlugin\Model\WebhookInterface; | ||
use Sylius\Component\Resource\Factory\FactoryInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* @extends FactoryInterface<WebhookInterface> | ||
*/ | ||
interface WebhookFactoryInterface extends FactoryInterface | ||
{ | ||
public function createNew(): WebhookInterface; | ||
|
||
public function createFromRequest(Request $request): WebhookInterface; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusPeakPlugin\Logger; | ||
|
||
use Psr\Log\AbstractLogger; | ||
use Setono\SyliusPeakPlugin\Model\WebhookInterface; | ||
|
||
final class WebhookLogger extends AbstractLogger | ||
{ | ||
public function __construct(private readonly WebhookInterface $webhook) | ||
{ | ||
} | ||
|
||
public function log($level, \Stringable|string $message, array $context = []): void | ||
Check failure on line 16 in src/Logger/WebhookLogger.php GitHub Actions / Static Code Analysis (PHP8.1 | Deps: lowest | SF~5.4.0)MethodSignatureMismatch
Check failure on line 16 in src/Logger/WebhookLogger.php GitHub Actions / Static Code Analysis (PHP8.2 | Deps: lowest | SF~5.4.0)MethodSignatureMismatch
Check failure on line 16 in src/Logger/WebhookLogger.php GitHub Actions / Static Code Analysis (PHP8.2 | Deps: lowest | SF~6.4.0)MethodSignatureMismatch
|
||
{ | ||
$this->webhook->addLog(sprintf('[%s] %s', (new \DateTimeImmutable())->format(\DATE_ATOM), (string) $message)); | ||
Check failure on line 18 in src/Logger/WebhookLogger.php GitHub Actions / Static Code Analysis (PHP8.1 | Deps: lowest | SF~5.4.0)RedundantCastGivenDocblockType
Check failure on line 18 in src/Logger/WebhookLogger.php GitHub Actions / Static Code Analysis (PHP8.2 | Deps: lowest | SF~5.4.0)RedundantCastGivenDocblockType
Check failure on line 18 in src/Logger/WebhookLogger.php GitHub Actions / Static Code Analysis (PHP8.2 | Deps: lowest | SF~6.4.0)RedundantCastGivenDocblockType
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusPeakPlugin\Model; | ||
|
||
class Webhook implements WebhookInterface | ||
{ | ||
protected ?int $id = null; | ||
|
||
protected ?string $method = null; | ||
|
||
protected ?string $url = null; | ||
|
||
/** @var array<string, list<string|null>>|null */ | ||
protected ?array $headers = null; | ||
|
||
protected ?string $body = null; | ||
|
||
protected ?string $remoteIp = null; | ||
|
||
protected ?string $log = null; | ||
|
||
protected \DateTimeInterface $createdAt; | ||
|
||
public function __construct() | ||
{ | ||
$this->createdAt = new \DateTimeImmutable(); | ||
} | ||
|
||
public function getId(): ?int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getMethod(): ?string | ||
{ | ||
return $this->method; | ||
} | ||
|
||
public function setMethod(?string $method): void | ||
{ | ||
$this->method = $method; | ||
} | ||
|
||
public function getUrl(): ?string | ||
{ | ||
return $this->url; | ||
} | ||
|
||
public function setUrl(?string $url): void | ||
{ | ||
$this->url = $url; | ||
} | ||
|
||
public function getHeaders(): array | ||
{ | ||
return $this->headers ?? []; | ||
} | ||
|
||
public function setHeaders(?array $headers): void | ||
{ | ||
$this->headers = $headers; | ||
} | ||
|
||
public function getBody(): ?string | ||
{ | ||
return $this->body; | ||
} | ||
|
||
public function setBody(?string $body): void | ||
{ | ||
$this->body = $body; | ||
} | ||
|
||
public function getRemoteIp(): ?string | ||
{ | ||
return $this->remoteIp; | ||
} | ||
|
||
public function setRemoteIp(?string $remoteIp): void | ||
{ | ||
$this->remoteIp = $remoteIp; | ||
} | ||
|
||
public function getLog(): ?string | ||
{ | ||
return $this->log; | ||
} | ||
|
||
public function setLog(?string $log): void | ||
{ | ||
$this->log = $log; | ||
} | ||
|
||
public function addLog(string $log): void | ||
{ | ||
if (null === $this->log) { | ||
$this->log = ''; | ||
} | ||
|
||
$this->log = $log . \PHP_EOL . $this->log; | ||
} | ||
|
||
public function getCreatedAt(): \DateTimeInterface | ||
{ | ||
return $this->createdAt; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusPeakPlugin\Model; | ||
|
||
use Sylius\Component\Resource\Model\ResourceInterface; | ||
|
||
interface WebhookInterface extends ResourceInterface | ||
{ | ||
public function getId(): ?int; | ||
|
||
public function getMethod(): ?string; | ||
|
||
public function setMethod(?string $method): void; | ||
|
||
public function getUrl(): ?string; | ||
|
||
public function setUrl(?string $url): void; | ||
|
||
/** | ||
* @return array<string, list<string|null>> | ||
*/ | ||
public function getHeaders(): array; | ||
|
||
/** | ||
* @param array<string, list<string|null>>|null $headers | ||
*/ | ||
public function setHeaders(?array $headers): void; | ||
|
||
public function getBody(): ?string; | ||
|
||
public function setBody(?string $body): void; | ||
|
||
public function getRemoteIp(): ?string; | ||
|
||
public function setRemoteIp(?string $remoteIp): void; | ||
|
||
public function getLog(): ?string; | ||
|
||
public function setLog(?string $log): void; | ||
|
||
public function addLog(string $log): void; | ||
|
||
public function getCreatedAt(): \DateTimeInterface; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<doctrine-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
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\Webhook" | ||
table="setono_sylius_peak_wms__webhook"> | ||
<id name="id" type="integer"> | ||
<generator strategy="AUTO"/> | ||
</id> | ||
|
||
<field name="method" type="string"/> | ||
<field name="url" type="string"/> | ||
<field name="headers" type="json"/> | ||
<field name="body" type="text"/> | ||
<field name="remoteIp" type="string"/> | ||
<field name="log" type="text" nullable="true"/> | ||
<field name="createdAt" type="datetime"/> | ||
</mapped-superclass> | ||
</doctrine-mapping> |