Test Failed
Pull Request — main (#12)
by Michael
03:38
created

CalculatesCosts   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 16
c 2
b 0
f 0
dl 0
loc 76
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A percentage() 0 3 1
A subtract() 0 3 1
A calc() 0 18 2
A fixedPrice() 0 3 1
A lessOrEqualsZero() 0 3 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  $value
16
     *
17
     * @return float
18
     * @throws InvalidCouponTypeException
19
     * @throws InvalidCouponValueException
20
     */
21
    public function calc(float $value): float
22
    {
23
        $discount = (float) $this->{static::$bindable->getValueColumn()};
24
25
        if ($this->lessOrEqualsZero($discount)) {
26
            throw new InvalidCouponValueException;
27
        }
28
29
        $result = match ($this->{static::$bindable->getTypeColumn()}) {
30
            static::TYPE_SUBTRACTION => $this->subtract($value, $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
            static::TYPE_PERCENTAGE  => $this->percentage($value, $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
            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
            default => throw new InvalidCouponTypeException,
34
        };
35
36
        return max(
37
            round($result, config('couponables.round') ?? 2),
38
            config('couponables.max') ?? 0
39
        );
40
    }
41
42
    /**
43
     * @param  float  $value
44
     * @return bool
45
     */
46
    private function lessOrEqualsZero(float $value): bool
47
    {
48
        return $value <= 0;
49
    }
50
51
    /**
52
     * Apply the "Subtraction" calculation strategy.
53
     *
54
     * @param  float  $cost
55
     * @param  float  $discount
56
     *
57
     * @return float
58
     */
59
    private function subtract(float $cost, float $discount): float
60
    {
61
        return $cost - $discount;
62
    }
63
64
    /**
65
     * Apply the "Percentage" calculation strategy.
66
     *
67
     * @param  float  $value
68
     * @param  float  $discount
69
     *
70
     * @return float
71
     */
72
    private function percentage(float $value, float $discount): float
73
    {
74
        return (1.0 - ($discount / 100)) * $value;
75
    }
76
77
    /**
78
     * Apply the "Fixed Price" calculation strategy.
79
     *
80
     * @param  float  $discount
81
     * @return float
82
     */
83
    private function fixedPrice(float $discount): float
84
    {
85
        return $discount;
86
    }
87
}
88