Passed
Push — master ( 9491b2...270076 )
by Adam
11:03
created

Calculator::setCountry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Calculator::netPrice() 0 3 1
1
<?php
2
3
namespace Coyote\Services\Invoice;
4
5
use Coyote\Coupon;
6
use Illuminate\Contracts\Support\Arrayable;
7
8
class Calculator implements Arrayable
9
{
10
    /**
11
     * @var float
12
     */
13
    public $price;
14
15
    /**
16
     * @var float
17
     */
18
    public $vatRate;
19
20
    /**
21
     * @var float
22
     */
23
    public $discount;
24
25
    /**
26
     * @var Coupon|null
27
     */
28
    protected $coupon;
29
30
    /**
31
     * @param array $attributes
32
     */
33
    public function __construct(array $attributes = [])
34
    {
35
        foreach ($attributes as $key => $value) {
36
            $camelCase = camel_case($key);
37
38
            if (property_exists($this, $camelCase)) {
39
                $this->{$camelCase} = $value;
40
            }
41
        }
42
    }
43
44
    /**
45
     * @param Coupon|null $coupon
46
     * @return $this
47
     */
48
    public function setCoupon(Coupon $coupon = null): Calculator
49
    {
50
        $this->coupon = $coupon;
51
52
        return $this;
53
    }
54
55
    /**
56
     * @return float
57
     */
58
    public function netPrice()
59
    {
60
        return round($this->calculateDiscount($this->price), 2);
61
    }
62
63
    /**
64
     * @return float
65
     */
66
    public function grossPrice()
67
    {
68
        return round($this->netPrice() * $this->vatRate, 2);
69
    }
70
71
    /**
72
     * @return float
73
     */
74
    public function vatPrice()
75
    {
76
        return round($this->grossPrice() - $this->netPrice(), 2);
77
    }
78
79
    /**
80
     * @return array
81
     */
82
    public function toArray()
83
    {
84
        return [
85
            'vat_rate'      => $this->vatRate,
86
            'net_price'     => $this->netPrice(),
87
            'gross_price'   => $this->grossPrice(),
88
            'vat_price'     => $this->vatPrice()
89
        ];
90
    }
91
92
    /**
93
     * @param float $price
94
     * @return float
95
     */
96
    private function calculateDiscount($price)
97
    {
98
        return max(0, ($this->discount > 0 ? ($price - ($price * $this->discount)) : $price) - ($this->coupon !== null ? $this->coupon->amount : 0));
99
    }
100
}
101