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

CalculatesCosts   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 9
c 2
b 0
f 0
dl 0
loc 52
ccs 12
cts 12
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A percentage() 0 3 1
A subtract() 0 3 1
A fixedPrice() 0 3 1
A calc() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\Couponables\Traits\Concerns;
6
7
use MichaelRubel\Couponables\Exceptions\InvalidCouponTypeException;
8
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),
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...
23 4
            static::TYPE_PERCENTAGE  => $this->percentage($value),
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...
24 2
            static::TYPE_FIXED       => $this->fixedPrice(),
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...
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
49
    {
50 2
        return ($this->{static::$bindable->getValueColumn()} / 100) * $value;
51
    }
52
53
    /**
54
     * Apply the "Fixed Price" calculation strategy.
55
     *
56
     * @return float
57
     */
58 1
    private function fixedPrice(): float
59
    {
60 1
        return (float) $this->{static::$bindable->getValueColumn()};
61
    }
62
}
63