1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace MichaelRubel\Couponables\Traits\Concerns; |
||
6 | |||
7 | use MichaelRubel\Couponables\Exceptions\InvalidCouponTypeException; |
||
8 | use MichaelRubel\Couponables\Exceptions\InvalidCouponValueException; |
||
9 | |||
10 | trait CalculatesCosts |
||
11 | { |
||
12 | /** |
||
13 | * Calculate the output value based on the coupon type. |
||
14 | * |
||
15 | * @param float $using |
||
16 | * |
||
17 | * @return float |
||
18 | * @throws InvalidCouponTypeException |
||
19 | * @throws InvalidCouponValueException |
||
20 | */ |
||
21 | 12 | public function calc(float $using): float |
|
22 | { |
||
23 | 12 | $discount = (float) $this->{static::getValueColumn()}; |
|
24 | |||
25 | 12 | if ($this->lessOrEqualZero($discount)) { |
|
26 | 2 | throw new InvalidCouponValueException; |
|
27 | } |
||
28 | |||
29 | 10 | $result = match ($this->{static::getTypeColumn()}) { |
|
30 | 10 | static::TYPE_SUBTRACTION, null => $this->subtract($using, $discount), |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
31 | 10 | static::TYPE_PERCENTAGE => $this->percentage($using, $discount), |
|
0 ignored issues
–
show
|
|||
32 | 10 | static::TYPE_FIXED => $this->fixedPrice($discount), |
|
0 ignored issues
–
show
|
|||
33 | 10 | default => throw new InvalidCouponTypeException, |
|
34 | 10 | }; |
|
35 | |||
36 | 9 | $rounded = round($result, |
|
37 | 9 | precision: config('couponables.round') ?? 2, |
|
38 | 9 | mode: config('couponables.round_mode') ?? PHP_ROUND_HALF_UP |
|
39 | 9 | ); |
|
40 | |||
41 | 9 | return max($rounded, config('couponables.max') ?? 0); |
|
42 | } |
||
43 | |||
44 | /** |
||
45 | * Apply the "Subtraction" calculation strategy. |
||
46 | * |
||
47 | * @param float $cost |
||
48 | * @param float $discount |
||
49 | * |
||
50 | * @return float |
||
51 | */ |
||
52 | 2 | private function subtract(float $cost, float $discount): float |
|
53 | { |
||
54 | 2 | return $cost - $discount; |
|
55 | } |
||
56 | |||
57 | /** |
||
58 | * Apply the "Percentage" calculation strategy. |
||
59 | * |
||
60 | * @param float $value |
||
61 | * @param float $discount |
||
62 | * |
||
63 | * @return float |
||
64 | */ |
||
65 | 5 | private function percentage(float $value, float $discount): float |
|
66 | { |
||
67 | 5 | return (1.0 - ($discount / 100)) * $value; |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * Apply the "Fixed Price" calculation strategy. |
||
72 | * |
||
73 | * @param float $discount |
||
74 | * @return float |
||
75 | */ |
||
76 | 2 | private function fixedPrice(float $discount): float |
|
77 | { |
||
78 | 2 | return $discount; |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param float $value |
||
83 | * @return bool |
||
84 | */ |
||
85 | 12 | private function lessOrEqualZero(float $value): bool |
|
86 | { |
||
87 | 12 | return $value <= 0; |
|
88 | } |
||
89 | } |
||
90 |