Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 23 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Redemption | |
0.00% |
0 / 23 |
|
0.00% |
0 / 5 |
30 | |
0.00% |
0 / 1 |
| fromRow | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| getCoupon | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setCoupon | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| toArray | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| wasRedeemedVia | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BuyerKiosk\ComebackCash\Models; |
| 4 | |
| 5 | /** |
| 6 | * Comeback Cash Redemption entity |
| 7 | * |
| 8 | * Represents an audit log entry for when a Comeback Cash coupon is redeemed. |
| 9 | * This is an immutable audit entity - once created, it should not be modified. |
| 10 | * |
| 11 | * Database table: ccRedemptions |
| 12 | */ |
| 13 | class Redemption |
| 14 | { |
| 15 | public int $id; |
| 16 | public int $couponId; |
| 17 | public float $redeemedAmount; |
| 18 | public string $transactionId; |
| 19 | public float $transactionAmount; |
| 20 | public int $redeemedByEmployeeId; |
| 21 | public \DateTime $redeemedAt; |
| 22 | public string $redemptionMethod; |
| 23 | |
| 24 | /** |
| 25 | * Valid redemption methods |
| 26 | */ |
| 27 | public const METHOD_SCAN = 'scan'; |
| 28 | public const METHOD_MANUAL = 'manual'; |
| 29 | public const METHOD_POS = 'pos'; |
| 30 | |
| 31 | /** |
| 32 | * Associated coupon (not lazy-loaded - must be set explicitly) |
| 33 | */ |
| 34 | private ?Coupon $coupon = null; |
| 35 | |
| 36 | /** |
| 37 | * Create a Redemption instance from a database row |
| 38 | * |
| 39 | * @param array $row Database row with snake_case column names |
| 40 | * @return self |
| 41 | */ |
| 42 | public static function fromRow(array $row): self |
| 43 | { |
| 44 | $redemption = new self(); |
| 45 | |
| 46 | $redemption->id = (int) $row['id']; |
| 47 | $redemption->couponId = (int) $row['coupon_id']; |
| 48 | $redemption->redeemedAmount = (float) $row['redeemed_amount']; |
| 49 | $redemption->transactionId = (string) $row['transaction_id']; |
| 50 | $redemption->transactionAmount = (float) $row['transaction_amount']; |
| 51 | $redemption->redeemedByEmployeeId = (int) $row['redeemed_by_employee_id']; |
| 52 | $redemption->redeemedAt = new \DateTime($row['redeemed_at']); |
| 53 | $redemption->redemptionMethod = (string) $row['redemption_method']; |
| 54 | |
| 55 | return $redemption; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Get the associated coupon |
| 60 | * |
| 61 | * Note: The coupon must be explicitly set via setCoupon() - this model |
| 62 | * does not lazy-load from the database. That responsibility belongs |
| 63 | * to the service layer. |
| 64 | * |
| 65 | * @return Coupon|null |
| 66 | */ |
| 67 | public function getCoupon(): ?Coupon |
| 68 | { |
| 69 | return $this->coupon; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Set the associated coupon |
| 74 | * |
| 75 | * @param Coupon $coupon |
| 76 | * @return void |
| 77 | */ |
| 78 | public function setCoupon(Coupon $coupon): void |
| 79 | { |
| 80 | $this->coupon = $coupon; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Convert the redemption to an array representation |
| 85 | * |
| 86 | * @return array<string, mixed> |
| 87 | */ |
| 88 | public function toArray(): array |
| 89 | { |
| 90 | return [ |
| 91 | 'id' => $this->id, |
| 92 | 'coupon_id' => $this->couponId, |
| 93 | 'redeemed_amount' => $this->redeemedAmount, |
| 94 | 'transaction_id' => $this->transactionId, |
| 95 | 'transaction_amount' => $this->transactionAmount, |
| 96 | 'redeemed_by_employee_id' => $this->redeemedByEmployeeId, |
| 97 | 'redeemed_at' => $this->redeemedAt->format('Y-m-d H:i:s'), |
| 98 | 'redemption_method' => $this->redemptionMethod, |
| 99 | ]; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Check if this redemption used a specific method |
| 104 | * |
| 105 | * @param string $method One of the METHOD_* constants |
| 106 | * @return bool |
| 107 | */ |
| 108 | public function wasRedeemedVia(string $method): bool |
| 109 | { |
| 110 | return $this->redemptionMethod === $method; |
| 111 | } |
| 112 | } |