Skip to content

Commit

Permalink
feat: add audits (#236)
Browse files Browse the repository at this point in the history
Co-authored-by: Edie Lemoine <[email protected]>
  • Loading branch information
Mark-Ernst and EdieLemoine authored Nov 21, 2023
1 parent e5dfa23 commit 646e6b9
Show file tree
Hide file tree
Showing 40 changed files with 1,025 additions and 21 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@
"pestphp/pest-plugin": true
}
}
}
}
27 changes: 27 additions & 0 deletions src/App/Action/Backend/Order/ExportOrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public function handle(Request $request): Response

$this->saveOrders($exportedOrders, $originalOrders);

$this->addAudits($request, $exportedOrders);

return Actions::execute(PdkBackendActions::FETCH_ORDERS, [
'orderIds' => $this->getOrderIds($request),
]);
Expand Down Expand Up @@ -159,6 +161,11 @@ protected function validateOrders(PdkOrderCollection $orders): PdkOrderCollectio
$orderService = Pdk::get(PdkOrderOptionsServiceInterface::class);

return $orders
->filter(function (PdkOrder $order) {
return ! $order->audits
->automatic()
->hasAction(PdkBackendActions::EXPORT_ORDERS);
})
->map(static function (PdkOrder $order) use ($orderService) {
return $orderService->calculate($order);
})
Expand Down Expand Up @@ -193,6 +200,26 @@ protected function validateOrders(PdkOrderCollection $orders): PdkOrderCollectio
});
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \MyParcelNL\Pdk\App\Order\Collection\PdkOrderCollection $exportedOrders
*
* @return void
* @throws \MyParcelNL\Pdk\Base\Exception\InvalidCastException
*/
private function addAudits(Request $request, PdkOrderCollection $exportedOrders): void
{
$exportedOrders->each(function (PdkOrder $order) use ($request) {
$order->addAudit(
PdkBackendActions::EXPORT_ORDERS,
$request->get('actionType'),
[
'mode' => Settings::get(OrderSettings::ORDER_MODE, OrderSettings::ID) ? 'order' : 'shipment',
]
);
});
}

/**
* Reset shipment options that were originally set to "inherit" because they've been modified by the
* PdkOrderOptionsService.
Expand Down
10 changes: 10 additions & 0 deletions src/App/Api/Contract/PdkActionsServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ interface PdkActionsServiceInterface
*/
public function execute($action, array $parameters = []): Response;

/**
* @param string|\Symfony\Component\HttpFoundation\Request $action
* @param array $parameters
*
* @return \Symfony\Component\HttpFoundation\Response
* @throws \MyParcelNL\Pdk\Api\Exception\ApiException
* @throws \MyParcelNL\Pdk\Api\Exception\PdkEndpointException
*/
public function executeAutomatic($action, array $parameters = []): Response;

/**
* @param string $context
*
Expand Down
14 changes: 14 additions & 0 deletions src/App/Api/Service/PdkActionsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use MyParcelNL\Pdk\Api\Exception\PdkEndpointException;
use MyParcelNL\Pdk\App\Api\Contract\PdkActionsServiceInterface;
use MyParcelNL\Pdk\App\Api\PdkEndpoint;
use MyParcelNL\Pdk\Audit\Model\Audit;
use MyParcelNL\Pdk\Base\Support\Collection;
use MyParcelNL\Pdk\Facade\Config;
use MyParcelNL\Pdk\Facade\Pdk;
Expand Down Expand Up @@ -41,6 +42,19 @@ public function execute($action, array $parameters = []): Response
return $action->handle($request);
}

/**
* @param $action
* @param array $parameters
*
* @return \Symfony\Component\HttpFoundation\Response
* @throws \MyParcelNL\Pdk\Api\Exception\ApiException
* @throws \MyParcelNL\Pdk\Api\Exception\PdkEndpointException
*/
public function executeAutomatic($action, array $parameters = []): Response
{
return $this->execute($action, array_replace($parameters, ['actionType' => Audit::TYPE_AUTOMATIC]));
}

/**
* @param string $context
*
Expand Down
63 changes: 63 additions & 0 deletions src/App/Audit/Concern/HasAudits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\Pdk\App\Audit\Concern;

use DateTime;
use InvalidArgumentException;
use MyParcelNL\Pdk\Audit\Collection\AuditCollection;
use MyParcelNL\Pdk\Audit\Model\Audit;
use MyParcelNL\Pdk\Facade\Audits;

/**
* @property AuditCollection $audits
*/
trait HasAudits
{
/**
* @param string $action
* @param null|string $type
* @param null|array $arguments
*
* @return void
* @throws \MyParcelNL\Pdk\Base\Exception\InvalidCastException
*/
public function addAudit(string $action, ?string $type = null, ?array $arguments = []): void
{
$id = uniqid('', true);
$audit = new Audit([
'id' => $id,
'arguments' => $arguments,
'action' => $action,
'model' => static::class,
'modelIdentifier' => $this->getAttribute($this->auditIdentifier),
'created' => new DateTime(),
'type' => $type,
]);

Audits::add($audit);
}

/**
* @return \MyParcelNL\Pdk\Audit\Collection\AuditCollection
* @throws \MyParcelNL\Pdk\Base\Exception\InvalidCastException
*/
protected function getAuditsAttribute(): AuditCollection
{
return Audits::allByModel(static::class, $this->getAttribute($this->auditIdentifier));
}

/**
* @return void
*/
protected function initializeHasAudits(): void
{
if (null === $this->auditIdentifier) {
throw new InvalidArgumentException('Audit identifier is not set');
}

$this->attributes['audits'] = null;
$this->casts['audits'] = AuditCollection::class;
}
}
21 changes: 14 additions & 7 deletions src/App/Order/Model/PdkOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MyParcelNL\Pdk\App\Order\Model;

use MyParcelNL\Pdk\App\Audit\Concern\HasAudits;
use MyParcelNL\Pdk\App\Order\Collection\PdkOrderLineCollection;
use MyParcelNL\Pdk\App\Order\Collection\PdkOrderNoteCollection;
use MyParcelNL\Pdk\App\Order\Contract\PdkOrderNoteRepositoryInterface;
Expand Down Expand Up @@ -47,7 +48,9 @@
*/
class PdkOrder extends Model
{
protected $attributes = [
use HasAudits;

protected $attributes = [
/** Plugin order id */
'externalIdentifier' => null,

Expand Down Expand Up @@ -100,7 +103,9 @@ class PdkOrder extends Model
'totalPriceAfterVat' => 0,
];

protected $casts = [
protected $auditIdentifier = 'externalIdentifier';

protected $casts = [
'externalIdentifier' => 'string',
'apiIdentifier' => 'string',

Expand Down Expand Up @@ -187,11 +192,13 @@ public static function fromFulfilmentOrder(Order $order): self
*/
public function createShipment(bool $store = true): Shipment
{
$shipment = $this->synchronizeShipment(new Shipment([
'carrier' => $this->deliveryOptions->carrier,
'customsDeclaration' => $this->customsDeclaration,
'deliveryOptions' => $this->deliveryOptions,
]));
$shipment = $this->synchronizeShipment(
new Shipment([
'carrier' => $this->deliveryOptions->carrier,
'customsDeclaration' => $this->customsDeclaration,
'deliveryOptions' => $this->deliveryOptions,
])
);

if ($store) {
$this->shipments->push($shipment);
Expand Down
34 changes: 34 additions & 0 deletions src/Audit/Collection/AuditCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\Pdk\Audit\Collection;

use MyParcelNL\Pdk\Audit\Model\Audit;
use MyParcelNL\Pdk\Base\Support\Collection;

/**
* @property \MyParcelNL\Pdk\Audit\Model\Audit[] $items
*/
class AuditCollection extends Collection
{
protected $cast = Audit::class;

/**
* @return self
*/
public function automatic(): self
{
return $this->where('type', Audit::TYPE_AUTOMATIC);
}

/**
* @param string $action
*
* @return bool
*/
public function hasAction(string $action): bool
{
return $this->contains('action', $action);
}
}
22 changes: 22 additions & 0 deletions src/Audit/Contract/AuditRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\Pdk\Audit\Contract;

use MyParcelNL\Pdk\Audit\Collection\AuditCollection;
use MyParcelNL\Pdk\Audit\Model\Audit;
use MyParcelNL\Pdk\Base\Contract\RepositoryInterface;

interface AuditRepositoryInterface extends RepositoryInterface
{
/**
* Get all audits from the database.
*/
public function all(): AuditCollection;

/**
* Store a single audit in the database of the platform.
*/
public function store(Audit $audit): void;
}
26 changes: 26 additions & 0 deletions src/Audit/Contract/AuditServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\Pdk\Audit\Contract;

use MyParcelNL\Pdk\Audit\Collection\AuditCollection;
use MyParcelNL\Pdk\Audit\Model\Audit;

interface AuditServiceInterface
{
/**
* @param \MyParcelNL\Pdk\Audit\Model\Audit $audit
*
* @return \MyParcelNL\Pdk\Audit\Model\Audit
*/
public function add(Audit $audit): Audit;

/**
* @param string $model
* @param string $identifier
*
* @return \MyParcelNL\Pdk\Audit\Collection\AuditCollection
*/
public function allByModel(string $model, string $identifier): AuditCollection;
}
59 changes: 59 additions & 0 deletions src/Audit/Model/Audit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\Pdk\Audit\Model;

use MyParcelNL\Pdk\Base\Model\Model;

/**
* @property null|string $id
* @property array $arguments
* @property string $type
* @property null|string $action
* @property null|class-string<Model> $model
* @property null|string $modelIdentifier
* @property null|\DateTime $created
*/
class Audit extends Model
{
public const TYPE_AUTOMATIC = 'automatic';
public const TYPE_MANUAL = 'manual';

/**
* @var array
*/
protected $attributes = [
'id' => null,
'arguments' => [],
'type' => self::TYPE_MANUAL,
'action' => null,

/**
* FQCN of the model that was audited.
*
* @see \MyParcelNL\Pdk\App\Audit\Concern\HasAudits
*/
'model' => null,

/**
* Identifier of the model that was audited.
*/
'modelIdentifier' => null,

'created' => null,
];

/**
* @var array
*/
protected $casts = [
'id' => 'string',
'type' => 'string',
'action' => 'string',
'model' => 'string',
'modelIdentifier' => 'string',
'arguments' => 'array',
'created' => 'createdtime',
];
}
Loading

0 comments on commit 646e6b9

Please sign in to comment.