TaxManager::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace FeiMx\Tax;
4
5
use FeiMx\Tax\Exceptions\TaxErrorException;
6
7
class TaxManager
8
{
9
    /**
10
     * Amount for use when calculate taxes.
11
     *
12
     * @var int
13
     */
14
    public $amount;
15
    /**
16
     * List of taxes for calculate final amount.
17
     *
18
     * @var array
19
     */
20
    public $taxes = [];
21
22
    /**
23
     * @param int $amount
24
     */
25
    public function __construct($amount = 0)
26
    {
27
        $this->amount = $amount;
28
    }
29
30
    /**
31
     * @param string|FeiMx\Tax\Contracts\TaxContract $tax
32
     * @param bool|string                            $retention
33
     *
34
     * @return mixed
35
     */
36
    public function addTax($tax, $retention = false)
37
    {
38
        if (is_string($tax)) {
39
            $className = $this->stringToClassName($tax);
40
            $tax = new $className($retention);
41
        }
42
43
        if (!in_array($tax, $this->taxes)) {
44
            $this->taxes[] = $tax;
45
        }
46
47
        return $this;
48
    }
49
50
    /**
51
     * @param array|\ArrayAccess
52
     */
53
    public function addTaxes(...$taxes)
54
    {
55
        if (is_array($taxes)) {
56
            $taxes = array_flatten($taxes);
57
        }
58
59
        collect($taxes)->each(function ($tax) {
60
            $this->addTax($tax);
61
        });
62
63
        return $this;
64
    }
65
66
    /**
67
     * Get total amount after taxes.
68
     *
69
     * @return float Total amount
70
     */
71
    public function total()
72
    {
73
        $total = 0;
74
        foreach ($this->taxes as $tax) {
75
            $total += $tax->calculate($this->amount);
76
        }
77
78
        return number_format($this->amount + $total, 6, '.', '');
79
    }
80
81
    /**
82
     * Get a list of taxes with amount calculated.
83
     *
84
     * @return float Total amount
85
     */
86
    public function get()
87
    {
88
        $taxes = array_map(function ($tax) {
89
            return [
90
                'tax' => $tax->name,
91
                'amount' => $tax->calculate($this->amount),
92
            ];
93
        }, $this->taxes);
94
95
        return array_merge([
96
            'amount' => $this->amount,
97
            'total' => $this->total,
0 ignored issues
show
Documentation introduced by
The property total does not exist on object<FeiMx\Tax\TaxManager>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
98
            'taxes' => $taxes,
99
        ]);
100
    }
101
102
    /**
103
     * @param string $tax
104
     *
105
     * @return string $className
106
     */
107
    public function stringToClassName($tax)
108
    {
109
        $className = 'FeiMx\\Tax\\Taxes\\'.strtoupper($tax);
110
        if (!class_exists($className)) {
111
            throw new TaxErrorException("The tax '{$tax}' is not valid");
112
        }
113
114
        return $className;
115
    }
116
117
    public function __get($property)
118
    {
119
        if ('total' == $property) {
120
            return $this->total();
121
        }
122
123
        if (property_exists($this, $property)) {
124
            return $this->{$property};
125
        }
126
127
        throw new \Exception('Property not exists');
128
    }
129
}
130