64 lines
1.6 KiB
PHP
64 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Catalog\Services;
|
|
|
|
use App\Domains\Catalog\Enums\CatalogAction;
|
|
|
|
final readonly class AvailabilityDecision
|
|
{
|
|
/**
|
|
* @param list<CatalogAction> $allowedActions
|
|
* @param list<array{code: string, message: string}> $reasons
|
|
*/
|
|
private function __construct(
|
|
private bool $visible,
|
|
private ?int $maximumQuantity,
|
|
private array $allowedActions,
|
|
private array $reasons,
|
|
) {}
|
|
|
|
/** @param list<array{code: string, message: string}> $reasons */
|
|
public static function hidden(array $reasons): self
|
|
{
|
|
return new self(false, null, [], $reasons);
|
|
}
|
|
|
|
/**
|
|
* @param list<CatalogAction> $allowedActions
|
|
* @param list<array{code: string, message: string}> $reasons
|
|
*/
|
|
public static function visible(
|
|
?int $maximumQuantity,
|
|
array $allowedActions,
|
|
array $reasons,
|
|
): self {
|
|
return new self(true, $maximumQuantity, $allowedActions, $reasons);
|
|
}
|
|
|
|
public function isVisible(): bool
|
|
{
|
|
return $this->visible;
|
|
}
|
|
|
|
/** @return array<string, mixed> */
|
|
public function toArray(): array
|
|
{
|
|
if (! $this->visible) {
|
|
return [
|
|
'state' => 'hidden',
|
|
'reasons' => $this->reasons,
|
|
];
|
|
}
|
|
|
|
return [
|
|
'state' => 'visible',
|
|
'maximum_quantity' => $this->maximumQuantity,
|
|
'allowed_actions' => array_map(
|
|
fn (CatalogAction $action): string => $action->value,
|
|
$this->allowedActions,
|
|
),
|
|
'reasons' => $this->reasons,
|
|
];
|
|
}
|
|
}
|