Passed
Push — master ( c35552...d388e0 )
by Carsten
01:43
created

Item::single()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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