Skip to content

Commit

Permalink
Added the PromotionStatus enum
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Aug 24, 2024
1 parent 1fc0464 commit 2763970
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Promotion/Contracts/Promotion.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Vanilo\Promotion\Contracts;

use Illuminate\Support\Collection;
use Vanilo\Promotion\Models\PromotionStatus;

interface Promotion
{
Expand All @@ -28,6 +29,9 @@ public function isExpired(?\DateTimeInterface $at = null): bool;

public function isDepleted(): bool;

/** A read-only, calculated enum */
public function getStatus(): PromotionStatus;

public function isEligible(object $subject): bool;

public function isCouponBased(): bool;
Expand Down
10 changes: 10 additions & 0 deletions src/Promotion/Models/Promotion.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ public function isDepleted(): bool
return $this->usage_count >= $this->usage_limit;
}

public function getStatus(): PromotionStatus
{
return match(true) {
$this->isExpired() => PromotionStatus::Expired,
$this->isDepleted() => PromotionStatus::Depleted,
$this->isValid() => PromotionStatus::Active,
default => PromotionStatus::Inactive,
};
}

public function isEligible(object $subject): bool
{
/** @var PromotionRule $rule */
Expand Down
23 changes: 23 additions & 0 deletions src/Promotion/Models/PromotionStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Vanilo\Promotion\Models;

enum PromotionStatus: string
{
case Inactive = 'inactive';
case Active = 'active';
case Expired = 'expired';
case Depleted = 'depleted';

public function label(): string
{
return match ($this) {
self::Inactive => __('Inactive'),
self::Active => __('Active'),
self::Expired => __('Expired'),
self::Depleted => __('Depleted'),
};
}
}

0 comments on commit 2763970

Please sign in to comment.