Passed
Pull Request — main (#12)
by Michael
12:51
created

CalculatesCosts::lessOrEqualsZero()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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 11
    public function calc(float $using): float
22
    {
23 11
        $discount = (float) $this->{static::$bindable->getValueColumn()};
24
25 11
        if ($this->lessOrEqualsZero($discount)) {
26 2
            throw new InvalidCouponValueException;
27
        }
28
29 9
        $result = match ($this->{static::$bindable->getTypeColumn()}) {
30 9
            static::TYPE_SUBTRACTION => $this->subtract($using, $discount),
0 ignored issues
show
Bug introduced by
The constant MichaelRubel\Couponables...Costs::TYPE_SUBTRACTION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
31 8
            static::TYPE_PERCENTAGE  => $this->percentage($using, $discount),
0 ignored issues
show
Bug introduced by
The constant MichaelRubel\Couponables...sCosts::TYPE_PERCENTAGE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
32 3
            static::TYPE_FIXED       => $this->fixedPrice($discount),
0 ignored issues
show
Bug introduced by
The constant MichaelRubel\Couponables...ulatesCosts::TYPE_FIXED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
33 1
            default => throw new InvalidCouponTypeException,
34
        };
35
36 8
        $rounded = round($result,
37 8
            precision: config('couponables.round') ?? 2,
38 8
            mode: config('couponables.round_mode') ?? PHP_ROUND_HALF_UP
39
        );
40
41 8
        return max($rounded, config('couponables.max') ?? 0);
42
    }
43
44
    /**
45
     * @param  float  $value
46
     * @return bool
47
     */
48 11
    private function lessOrEqualsZero(float $value): bool
49
    {
50 11
        return $value <= 0;
51
    }
52
53
    /**
54
     * Apply the "Subtraction" calculation strategy.
55
     *
56
     * @param  float  $cost
57
     * @param  float  $discount
58
     *
59
     * @return float
60
     */
61 1
    private function subtract(float $cost, float $discount): float
62
    {
63 1
        return $cost - $discount;
64
    }
65
66
    /**
67
     * Apply the "Percentage" calculation strategy.
68
     *
69
     * @param  float  $value
70
     * @param  float  $discount
71
     *
72
     * @return float
73
     */
74 5
    private function percentage(float $value, float $discount): float
75
    {
76 5
        return (1.0 - ($discount / 100)) * $value;
77
    }
78
79
    /**
80
     * Apply the "Fixed Price" calculation strategy.
81
     *
82
     * @param  float  $discount
83
     * @return float
84
     */
85 2
    private function fixedPrice(float $discount): float
86
    {
87 2
        return $discount;
88
    }
89
}
90