Passed
Push — master ( 9c20ed...a7d58a )
by Carsten
07:10
created

Tax   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 0
dl 0
loc 68
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A deduct() 0 4 1
A add() 0 4 1
A rate() 0 4 1
A __construct() 0 11 3
1
<?php
2
3
/**
4
 * This file is part of Lenius Basket, a PHP package to handle
5
 * your shopping basket.
6
 *
7
 * Copyright (c) 2017 Lenius.
8
 * https://github.com/lenius/basket
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 *
13
 * @author Carsten Jonstrup<[email protected]>
14
 * @copyright 2017 Lenius.
15
 *
16
 * @version production
17
 *
18
 * @see https://github.com/lenius/basket
19
 */
20
21
namespace Lenius\Basket;
22
23
/**
24
 * Class Tax.
25
 */
26
class Tax
27
{
28
    /** @var float */
29
    public $percentage;
30
31
    /** @var float */
32
    public $deductModifier;
33
34
    /** @var float */
35
    public $addModifier;
36
37
    /**
38
     * When constructing the tax class, you can either
39
     * pass in a percentage, or a price before and after
40
     * tax and have the library work out the tax rate
41
     * automatically.
42
     *
43
     * @param float $value The percentage of your tax (or price before tax)
44
     * @param null $after The value after tax
45
     */
46 27
    public function __construct(float $value, $after = null)
47
    {
48 27
        $this->percentage = $value;
49
50 27
        if ($after != null && is_numeric($after)) {
51 1
            $this->percentage = (($after - $value) / $value) * 100;
52
        }
53
54 27
        $this->deductModifier = 1 - ($this->percentage / 100);
55 27
        $this->addModifier = 1 + ($this->percentage / 100);
56 27
    }
57
58
    /**
59
     * Deduct tax from a specified price.
60
     *
61
     * @param float $price The price you want to deduct tax from
62
     *
63
     * @return float $price - tax
64
     */
65 4
    public function deduct(float $price): float
66
    {
67 4
        return (float) ($price * $this->deductModifier);
68
    }
69
70
    /**
71
     * Add tax to a specified price.
72
     *
73
     * @param float $price The value you want to add tax to
74
     *
75
     * @return float $price + tax
76
     */
77 9
    public function add(float $price): float
78
    {
79 9
        return (float) ($price * $this->addModifier);
80
    }
81
82
    /**
83
     * Calculate the tax rate from a price.
84
     *
85
     * @param float $price The price (after tax)
86
     *
87
     * @return float The tax rate
88
     */
89 4
    public function rate(float $price): float
90
    {
91 4
        return (float) ($price - $this->deduct($price));
92
    }
93
}
94