Completed
Push — master ( f1ac10...99c02a )
by Carsten
04:49
created

Item::weight()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
dl 14
loc 14
ccs 7
cts 7
cp 1
rs 9.7998
c 0
b 0
f 0
cc 4
nc 2
nop 0
crap 4
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
 * @property mixed name
25
 * @property mixed price
26
 * @property mixed quantity
27
 * @property mixed weight
28
 * @property mixed options
29
 * @property-read string identifier
30
 */
31
class Item
32
{
33
    protected $identifier;
34
    protected $store;
35
    protected $tax;
36
37
    protected $data = [];
38
39
    /**
40
     * Construct the item.
41
     *
42
     * @param string           $identifier
43
     * @param array            $item
44
     * @param StorageInterface $store
45
     */
46 26
    public function __construct($identifier, array $item, StorageInterface $store)
47
    {
48 26
        $this->identifier = $identifier;
49 26
        $this->store = $store;
50
51 26
        foreach ($item as $key => $value) {
52 26
            $this->data[$key] = $value;
53
        }
54
55 26
        $item['tax'] = isset($item['tax']) ? $item['tax'] : 0;
56
57 26
        $this->tax = new Tax($item['tax']);
58 26
    }
59
60
    /**
61
     * Return the value of protected methods.
62
     *
63
     * @param any $param
64
     *
65
     * @return mixed
66
     */
67 24
    public function __get($param)
68
    {
69 24
        return $param == 'identifier' ? $this->identifier : $this->data[$param];
70
    }
71
72
    /**
73
     * Update data array using set magic method.
74
     *
75
     * @param string $param The key to set
76
     * @param mixed  $value The value to set $param to
77
     */
78 2
    public function __set($param, $value)
79
    {
80 2
        $this->data[$param] = $value;
81 2
        if ($param == 'tax') {
82 1
            $this->tax = new Tax($value);
83
        }
84 2
    }
85
86
    /**
87
     * Removes the current item from the cart.
88
     *
89
     * @return void
90
     */
91 1
    public function remove()
92
    {
93 1
        $this->store->remove($this->identifier);
94 1
    }
95
96
    /**
97
     * Return the total tax for this item.
98
     *
99
     * @return float
100
     */
101 3
    public function tax()
102
    {
103 3
        $price = $this->totalPrice();
104 3
        $quantity = $this->quantity;
105
106 3
        return $this->tax->rate($price * $quantity);
107
    }
108
109
    /**
110
     * Return the total price for this item.
111
     *
112
     * @return float
113
     */
114 11 View Code Duplication
    private function totalPrice()
115
    {
116 11
        $price = $this->price;
117
118 11
        if ($this->hasOptions()) {
119 3
            foreach ($this->data['options'] as $item) {
120 3
                if (array_key_exists('price', $item)) {
121 3
                    $price += $item['price'];
122
                }
123
            }
124
        }
125
126 11
        return $price;
127
    }
128
129
    /**
130
     * Return the total of the item, with or without tax.
131
     *
132
     * @param bool $includeTax Whether or not to include tax
133
     *
134
     * @return float The total, as a float
135
     */
136 7 View Code Duplication
    public function total($includeTax = true)
137
    {
138 7
        $price = $this->totalPrice();
139
140 7
        if ($includeTax) {
141 7
            $price = $this->tax->add($price);
142
        }
143
144 7
        return (float) ($price * $this->quantity);
145
    }
146
147
    /**
148
     * Return the total weight of the item.
149
     *
150
     * @return float The weight, as a float
151
     */
152 4 View Code Duplication
    public function weight()
153
    {
154 4
        $weight = $this->weight;
155
156 4
        if ($this->hasOptions()) {
157 3
            foreach ($this->data['options'] as $item) {
158 3
                if (array_key_exists('weight', $item)) {
159 3
                    $weight += $item['weight'];
160
                }
161
            }
162
        }
163
164 4
        return (float) ($weight * $this->quantity);
165
    }
166
167
    /**
168
     * Return the total of the item, with or without tax.
169
     *
170
     * @param bool $includeTax Whether or not to include tax
171
     *
172
     * @return float The total, as a float
173
     */
174 1 View Code Duplication
    public function single($includeTax = true)
175
    {
176 1
        $price = $this->totalPrice();
177
178 1
        if ($includeTax) {
179 1
            $price = $this->tax->add($price);
180
        }
181
182 1
        return (float) $price;
183
    }
184
185
    /**
186
     * Update a single key for this item, or multiple.
187
     *
188
     * @param array|string $key   The array key to update, or an array of key-value pairs to update
189
     * @param null         $value
190
     *
191
     * @return void
192
     */
193 4
    public function update($key, $value = null)
194
    {
195 4
        if (is_array($key)) {
196 1
            foreach ($key as $updateKey => $updateValue) {
197 1
                $this->update($updateKey, $updateValue);
198
            }
199
        } else {
200
            // Update the item
201 4
            $this->data[$key] = $value;
202 4
            if ($key == 'tax') {
203 2
                $this->tax = new Tax($value);
204
            }
205
        }
206 4
    }
207
208
    /**
209
     * Check if this item has options.
210
     *
211
     * @return bool Yes or no?
212
     */
213 17
    public function hasOptions()
214
    {
215 17
        return array_key_exists('options', $this->data) && !empty($this->data['options']);
216
    }
217
218
    /**
219
     * Convert the item into an array.
220
     *
221
     * @return array The item data
222
     */
223 2
    public function toArray()
224
    {
225 2
        return $this->data;
226
    }
227
}
228