Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
39 / 39 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| OrderStatus | |
100.00% |
39 / 39 |
|
100.00% |
6 / 6 |
35 | |
100.00% |
1 / 1 |
| values | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromStored | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| icon | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
9 | |||
| color | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
9 | |||
| background | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
9 | |||
| badgeClass | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Enums; |
| 4 | |
| 5 | /** |
| 6 | * The order lifecycle (§5.1 of PROJECT_BRAIN.md), replacing the old |
| 7 | * Pending/Recived/Success/Cancel set. `orders.status` stays a plain string |
| 8 | * column (not an Eloquent enum cast) — this enum is the single source of |
| 9 | * truth for which values are valid and how to display them, used to |
| 10 | * validate every write (SaleController::store(), OrderController:: |
| 11 | * changeStatus()) instead of the free-text field it used to be. |
| 12 | * |
| 13 | * No transition state-machine is enforced (e.g. Delivered -> Pending isn't |
| 14 | * blocked) — deliberately: the operator may legitimately need to correct a |
| 15 | * mistake, and no transition table was specified. Every change is still |
| 16 | * captured by Order's Auditable trait. |
| 17 | */ |
| 18 | enum OrderStatus: string |
| 19 | { |
| 20 | case Pending = 'Pending'; |
| 21 | case Confirmed = 'Confirmed'; |
| 22 | case Processing = 'Processing'; |
| 23 | case Shipped = 'Shipped'; |
| 24 | case Delivered = 'Delivered'; |
| 25 | case Cancelled = 'Cancelled'; |
| 26 | case Returned = 'Returned'; |
| 27 | case Refunded = 'Refunded'; |
| 28 | |
| 29 | /** @return string[] */ |
| 30 | public static function values(): array |
| 31 | { |
| 32 | return array_map(fn (self $c) => $c->value, self::cases()); |
| 33 | } |
| 34 | |
| 35 | /** Resolve a stored value, or Pending for anything unrecognised (e.g. legacy data). */ |
| 36 | public static function fromStored(?string $value): self |
| 37 | { |
| 38 | return self::tryFrom((string) $value) ?? self::Pending; |
| 39 | } |
| 40 | |
| 41 | public function icon(): string |
| 42 | { |
| 43 | return match ($this) { |
| 44 | self::Pending => 'fa-clock', |
| 45 | self::Confirmed => 'fa-check', |
| 46 | self::Processing => 'fa-cog', |
| 47 | self::Shipped => 'fa-truck', |
| 48 | self::Delivered => 'fa-check-circle', |
| 49 | self::Cancelled => 'fa-times-circle', |
| 50 | self::Returned => 'fa-rotate-left', |
| 51 | self::Refunded => 'fa-hand-holding-dollar', |
| 52 | }; |
| 53 | } |
| 54 | |
| 55 | /** Text/icon colour. */ |
| 56 | public function color(): string |
| 57 | { |
| 58 | return match ($this) { |
| 59 | self::Pending => '#f97316', |
| 60 | self::Confirmed => '#3b82f6', |
| 61 | self::Processing => '#8b5cf6', |
| 62 | self::Shipped => '#0ea5e9', |
| 63 | self::Delivered => '#22c55e', |
| 64 | self::Cancelled => '#ef4444', |
| 65 | self::Returned => '#f59e0b', |
| 66 | self::Refunded => '#64748b', |
| 67 | }; |
| 68 | } |
| 69 | |
| 70 | /** Soft background to pair with color(). */ |
| 71 | public function background(): string |
| 72 | { |
| 73 | return match ($this) { |
| 74 | self::Pending => '#fff7ed', |
| 75 | self::Confirmed => '#eff6ff', |
| 76 | self::Processing => '#f5f3ff', |
| 77 | self::Shipped => '#f0f9ff', |
| 78 | self::Delivered => '#f0fdf4', |
| 79 | self::Cancelled => '#fff1f2', |
| 80 | self::Returned => '#fffbeb', |
| 81 | self::Refunded => '#f8fafc', |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | /** Bootstrap badge class, for the plainer customer-facing account pages. */ |
| 86 | public function badgeClass(): string |
| 87 | { |
| 88 | return match ($this) { |
| 89 | self::Pending => 'bg-warning text-dark', |
| 90 | self::Confirmed, self::Processing => 'bg-info text-dark', |
| 91 | self::Shipped => 'bg-primary', |
| 92 | self::Delivered => 'bg-success', |
| 93 | self::Cancelled, self::Returned, self::Refunded => 'bg-danger', |
| 94 | }; |
| 95 | } |
| 96 | } |