php-commerce/Data/CouponValidationResult.php
Snider a774f4e285 refactor: migrate namespace from Core\Commerce to Core\Mod\Commerce
Align commerce module with the monorepo module structure by updating
all namespaces to use the Core\Mod\Commerce convention. This change
supports the recent monorepo separation and ensures consistency with
other modules.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-27 16:23:12 +00:00

41 lines
830 B
PHP

<?php
namespace Core\Mod\Commerce\Data;
use Core\Mod\Commerce\Models\Coupon;
/**
* 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);
}
}