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

CalculatesCosts::subtract()   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 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
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
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