2026-01-27 00:24:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
2026-01-27 16:23:12 +00:00
|
|
|
namespace Core\Mod\Commerce\Data;
|
2026-01-27 00:24:22 +00:00
|
|
|
|
2026-01-27 16:23:12 +00:00
|
|
|
use Core\Mod\Commerce\Models\Coupon;
|
2026-01-27 00:24:22 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Coupon validation result.
|
|
|
|
|
*/
|
|
|
|
|
class CouponValidationResult
|
|
|
|
|
{
|
|
|
|
|
public function __construct(
|
|
|
|
|
public readonly bool $isValid,
|
|
|
|
|
public readonly ?Coupon $coupon,
|
|
|
|
|
public readonly ?string $error,
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
public static function valid(Coupon $coupon): self
|
|
|
|
|
{
|
|
|
|
|
return new self(true, $coupon, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function invalid(string $error): self
|
|
|
|
|
{
|
|
|
|
|
return new self(false, null, $error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function isValid(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->isValid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getDiscount(float $amount): float
|
|
|
|
|
{
|
|
|
|
|
if (! $this->isValid || ! $this->coupon) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->coupon->calculateDiscount($amount);
|
|
|
|
|
}
|
|
|
|
|
}
|