Passed
Push — master ( 307db6...828bd5 )
by Carsten
12:04 queued 06:19
created

Item::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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