2026-01-22 15:51:05 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Magdev\WcLicensedProductClient\Dto;
|
|
|
|
|
|
|
|
|
|
enum LicenseState: string
|
|
|
|
|
{
|
|
|
|
|
case Active = 'active';
|
|
|
|
|
case Inactive = 'inactive';
|
|
|
|
|
case Expired = 'expired';
|
|
|
|
|
case Revoked = 'revoked';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final readonly class LicenseStatus
|
|
|
|
|
{
|
|
|
|
|
public function __construct(
|
|
|
|
|
public bool $valid,
|
|
|
|
|
public LicenseState $status,
|
|
|
|
|
public string $domain,
|
|
|
|
|
public ?\DateTimeImmutable $expiresAt,
|
|
|
|
|
public int $activationsCount,
|
|
|
|
|
public int $maxActivations,
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function fromArray(array $data): self
|
|
|
|
|
{
|
2026-01-24 14:31:13 +01:00
|
|
|
// Validate required fields
|
|
|
|
|
if (!isset($data['valid']) || !is_bool($data['valid'])) {
|
|
|
|
|
throw new \InvalidArgumentException('Invalid response: missing or invalid valid field');
|
|
|
|
|
}
|
|
|
|
|
if (!isset($data['status']) || !is_string($data['status'])) {
|
|
|
|
|
throw new \InvalidArgumentException('Invalid response: missing or invalid status field');
|
|
|
|
|
}
|
|
|
|
|
if (!isset($data['domain']) || !is_string($data['domain'])) {
|
|
|
|
|
throw new \InvalidArgumentException('Invalid response: missing or invalid domain field');
|
|
|
|
|
}
|
|
|
|
|
if (!isset($data['activations_count']) || !is_int($data['activations_count'])) {
|
|
|
|
|
throw new \InvalidArgumentException('Invalid response: missing or invalid activations_count field');
|
|
|
|
|
}
|
|
|
|
|
if (!isset($data['max_activations']) || !is_int($data['max_activations'])) {
|
|
|
|
|
throw new \InvalidArgumentException('Invalid response: missing or invalid max_activations field');
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-22 15:51:05 +01:00
|
|
|
$expiresAt = null;
|
|
|
|
|
if (isset($data['expires_at']) && $data['expires_at'] !== null) {
|
2026-01-24 14:31:13 +01:00
|
|
|
try {
|
|
|
|
|
$expiresAt = new \DateTimeImmutable($data['expires_at']);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
|
'Invalid response: invalid date format for expires_at',
|
|
|
|
|
0,
|
|
|
|
|
$e
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$status = LicenseState::from($data['status']);
|
|
|
|
|
} catch (\ValueError $e) {
|
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
|
'Invalid response: unknown license status value',
|
|
|
|
|
0,
|
|
|
|
|
$e
|
|
|
|
|
);
|
2026-01-22 15:51:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new self(
|
|
|
|
|
valid: $data['valid'],
|
2026-01-24 14:31:13 +01:00
|
|
|
status: $status,
|
2026-01-22 15:51:05 +01:00
|
|
|
domain: $data['domain'],
|
|
|
|
|
expiresAt: $expiresAt,
|
|
|
|
|
activationsCount: $data['activations_count'],
|
|
|
|
|
maxActivations: $data['max_activations'],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function isLifetime(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->expiresAt === null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function hasAvailableActivations(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->activationsCount < $this->maxActivations;
|
|
|
|
|
}
|
|
|
|
|
}
|