Passed
Push — main ( f8215d...34c757 )
by Michael
11:27 queued 07:12
created

CalculatesCosts::percentage()   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 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 2
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->lessOrEqualZero($discount)) {
26 2
            throw new InvalidCouponValueException;
27
        }
28
29 9
        $result = match ($this->{static::$bindable->getTypeColumn()}) {
30 9
            static::TYPE_SUBTRACTION => static::$bindable->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  => static::$bindable->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       => static::$bindable->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
     * Apply the "Subtraction" calculation strategy.
46
     *
47
     * @param  float  $cost
48
     * @param  float  $discount
49
     *
50
     * @return float
51
     */
52 1
    public function subtract(float $cost, float $discount): float
53
    {
54 1
        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
    public 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
    public function fixedPrice(float $discount): float
77
    {
78 2
        return $discount;
79
    }
80
81
    /**
82
     * @param  float  $value
83
     * @return bool
84
     */
85 11
    private function lessOrEqualZero(float $value): bool
86
    {
87 11
        return $value <= 0;
88
    }
89
}
90