Completed
Branch feature/job-plans (e266db)
by Adam
05:31
created

Calculator::calculateDiscount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Coyote\Services\Invoice;
4
5
use Illuminate\Contracts\Support\Arrayable;
6
7
class Calculator implements Arrayable
8
{
9
    /**
10
     * @var float
11
     */
12
    public $price;
13
14
    /**
15
     * @var float
16
     */
17
    public $vatRate;
18
19
    /**
20
     * @var float
21
     */
22
    public $discount;
23
24
    /**
25
     * @param array $attributes
26
     */
27 View Code Duplication
    public function __construct(array $attributes = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
    {
29
        foreach ($attributes as $key => $value) {
30
            $camelCase = camel_case($key);
31
32
            if (property_exists($this, $camelCase)) {
33
                $this->{$camelCase} = $value;
34
            }
35
        }
36
    }
37
38
    /**
39
     * @return float
40
     */
41
    public function netPrice()
42
    {
43
        return round($this->calculateDiscount($this->price), 2);
44
    }
45
46
    /**
47
     * @return float
48
     */
49
    public function grossPrice()
50
    {
51
        return round($this->netPrice() * $this->vatRate, 2);
52
    }
53
54
    /**
55
     * @return float
56
     */
57
    public function vatPrice()
58
    {
59
        return round($this->grossPrice() - $this->netPrice(), 2);
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    public function toArray()
66
    {
67
        return [
68
            'vat_rate'      => $this->vatRate,
69
            'net_price'     => $this->netPrice(),
70
            'gross_price'   => $this->grossPrice(),
71
            'vat_price'     => $this->vatPrice()
72
        ];
73
    }
74
75
    /**
76
     * @param float $price
77
     * @return float
78
     */
79
    private function calculateDiscount($price)
80
    {
81
        return $this->discount > 0 ? $price * $this->discount : $price;
82
    }
83
}
84