Passed
Push — master ( 307db6...828bd5 )
by Carsten
12:04 queued 06:19
created

Tax::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.0116
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
 * @link https://github.com/lenius/basket
19
 */
20
21
namespace Lenius\Basket;
22
23
/**
24
 * Class Tax.
25
 */
26
class Tax
27
{
28
    protected $percentage;
29
    protected $deductModifier;
30
    protected $addModifier;
31
32
    /**
33
     * When constructing the tax class, you can either
34
     * pass in a percentage, or a price before and after
35
     * tax and have the library work out the tax rate
36
     * automatically.
37
     *
38
     * @param float $value The percentage of your tax (or price before tax)
39
     * @param float $after The value after tax
40
     */
41 26
    public function __construct($value, $after = null)
42
    {
43 26
        $this->percentage = $value;
44
45 26
        if (is_numeric($after)) {
46
            $this->percentage = (($after - $value) / $value) * 100;
47
        }
48
49 26
        $this->deductModifier = 1 - ($this->percentage / 100);
50 26
        $this->addModifier = 1 + ($this->percentage / 100);
51 26
    }
52
53
    /**
54
     * Deduct tax from a specified price.
55
     *
56
     * @param float $price The price you want to deduct tax from
57
     *
58
     * @return float $price - tax
59
     */
60 3
    public function deduct($price)
61
    {
62 3
        return $price * $this->deductModifier;
63
    }
64
65
    /**
66
     * Add tax to a specified price.
67
     *
68
     * @param float $price The value you want to add tax to
69
     *
70
     * @return float $price + tax
71
     */
72 8
    public function add($price)
73
    {
74 8
        return $price * $this->addModifier;
75
    }
76
77
    /**
78
     * Calculate the tax rate from a price.
79
     *
80
     * @param float $price The price (after tax)
81
     *
82
     * @return float The tax rate
83
     */
84 3
    public function rate($price)
85
    {
86 3
        return $price - $this->deduct($price);
87
    }
88
89
    /**
90
     * Return the value of protected properties.
91
     *
92
     * @param mixed $property The property
93
     *
94
     * @return mixed The value of the property
95
     */
96
    public function __get($property)
97
    {
98
        return $this->$property;
99
    }
100
}
101