| Total Complexity | 4 |
| Total Lines | 52 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 9 | trait CalculatesCosts |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Calculate the output value based on the coupon type. |
||
| 13 | * |
||
| 14 | * @param float $value |
||
| 15 | * |
||
| 16 | * @return float |
||
| 17 | * @throws InvalidCouponTypeException |
||
| 18 | */ |
||
| 19 | 5 | public function calc(float $value): float |
|
| 20 | { |
||
| 21 | 5 | return match ($this->{static::$bindable->getTypeColumn()}) { |
|
| 22 | 5 | static::TYPE_SUBTRACTION => $this->subtract($value), |
|
|
|
|||
| 23 | 4 | static::TYPE_PERCENTAGE => $this->percentage($value), |
|
| 24 | 2 | static::TYPE_FIXED => $this->fixedPrice(), |
|
| 25 | 5 | default => throw new InvalidCouponTypeException, |
|
| 26 | }; |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Apply the "Subtraction" calculation strategy. |
||
| 31 | * |
||
| 32 | * @param float $cost |
||
| 33 | * |
||
| 34 | * @return float |
||
| 35 | */ |
||
| 36 | 1 | private function subtract(float $cost): float |
|
| 37 | { |
||
| 38 | 1 | return $cost - $this->{static::$bindable->getValueColumn()}; |
|
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Apply the "Percentage" calculation strategy. |
||
| 43 | * |
||
| 44 | * @param float $value |
||
| 45 | * |
||
| 46 | * @return float |
||
| 47 | */ |
||
| 48 | 2 | private function percentage(float $value): float |
|
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Apply the "Fixed Price" calculation strategy. |
||
| 55 | * |
||
| 56 | * @return float |
||
| 57 | */ |
||
| 58 | 1 | private function fixedPrice(): float |
|
| 61 | } |
||
| 62 | } |
||
| 63 |