Item::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Ksdev\ShoppingCart;
4
5
class Item
6
{
7
    /**
8
     * Item Stock Keeping Unit
9
     *
10
     * @var string $sku
11
     */
12
    protected $sku;
13
14
    /**
15
     * Item name
16
     *
17
     * @var string $name
18
     */
19
    protected $name;
20
21
    /**
22
     * Item price
23
     *
24
     * @var string $price
25
     */
26
    protected $price;
27
28
    /**
29
     * Item tax
30
     *
31
     * @var string $price
32
     */
33
    protected $tax;
34
35
    /**
36
     * @param string $sku Item Stock Keeping Unit
37
     * @param string $name Item name
38
     * @param string $price Item price with two digits after decimal point, e.g. '123.00'
39
     * @param string $tax Optional tax with two digits after decimal point, e.g. '23.00'
40
     *
41
     * @throws \Exception When the price format is invalid
42
     */
43
    public function __construct($sku, $name, $price, $tax = '0.00')
44
    {
45
        $this->setSku($sku);
46
        $this->setName($name);
47
        $this->setPrice($price);
48
        $this->setTax($tax);
49
    }
50
51
    // Setters
52
53
    /**
54
     * @param string $name
55
     */
56
    public function setName($name)
57
    {
58
        $this->name = (string)$name;
59
    }
60
61
    /**
62
     * @param string $sku
63
     */
64
    public function setSku($sku)
65
    {
66
        $this->sku = (string)$sku;
67
    }
68
69
    /**
70
     * @param string $price Item price with two digits after decimal point, e.g. '123.00'
71
     *
72
     * @throws \Exception When the price format is invalid
73
     */
74
    public function setPrice($price)
75
    {
76
        $this->validateAmountFormat($price);
77
        $this->price = (string)$price;
78
    }
79
80
    /**
81
     * @param string $tax Optional tax with two digits after decimal point, e.g. '23.00'
82
     *
83
     * @throws \Exception When the tax format is invalid
84
     */
85
    public function setTax($tax)
86
    {
87
        $this->validateAmountFormat($tax);
88
        $this->tax = (string)$tax;
89
    }
90
91
    // Getters
92
93
    /**
94
     * @return string
95
     */
96
    public function getSku()
97
    {
98
        return $this->sku;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getName()
105
    {
106
        return $this->name;
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getPrice()
113
    {
114
        return $this->price;
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function getTax()
121
    {
122
        return $this->tax;
123
    }
124
125
    /**
126
     * Validate amount format
127
     *
128
     * Checks if amount is a number with two digits after decimal point
129
     *
130
     * @param mixed $amount
131
     *
132
     * @throws \Exception When the amount format is invalid
133
     */
134
    protected function validateAmountFormat($amount)
135
    {
136
        if (! preg_match('/^\d+\.\d{2}$/', $amount)) {
137
            throw new \Exception('Invalid amount format');
138
        }
139
    }
140
}
141