Test Setup Failed
Branch master (494f90)
by Carsten
03:14 queued 01:17
created

Item::totalPrice()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 2
nop 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 dev
17
 *
18
 * @link http://github.com/lenius/basket
19
 */
20
21
namespace Lenius\Basket;
22
23
/**
24
 * @property mixed price
25
 * @property mixed quantity
26
 * @property mixed weight
27
 */
28
class Item
29
{
30
    protected $identifier;
31
    protected $store;
32
    protected $tax;
33
34
    protected $data = [];
35
36
    /**
37
     * Construct the item.
38
     *
39
     * @param string           $identifier
40
     * @param array            $item
41
     * @param StorageInterface $store
42
     */
43
    public function __construct($identifier, array $item, StorageInterface $store)
44
    {
45
        $this->identifier = $identifier;
46
        $this->store = $store;
47
48
        foreach ($item as $key => $value) {
49
            $this->data[$key] = $value;
50
        }
51
52
        $item['tax'] = isset($item['tax']) ? $item['tax'] : 0;
53
54
        $this->tax = new Tax($item['tax']);
55
    }
56
57
    /**
58
     * Return the value of protected methods.
59
     *
60
     * @param any $param
61
     *
62
     * @return mixed
63
     */
64
    public function __get($param)
65
    {
66
        return $param == 'identifier' ? $this->identifier : $this->data[$param];
67
    }
68
69
    /**
70
     * Update data array using set magic method.
71
     *
72
     * @param string $param The key to set
73
     * @param mixed  $value The value to set $param to
74
     */
75
    public function __set($param, $value)
76
    {
77
        $this->data[$param] = $value;
78
        if ($param == 'tax') {
79
            $this->tax = new Tax($value);
80
        }
81
    }
82
83
    /**
84
     * Removes the current item from the cart.
85
     *
86
     * @return void
87
     */
88
    public function remove()
89
    {
90
        $this->store->remove($this->identifier);
91
    }
92
93
    /**
94
     * Return the total tax for this item.
95
     *
96
     * @return float
97
     */
98
    public function tax()
99
    {
100
        $price = $this->totalPrice();;
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
101
102
        return $this->tax->rate($price * $this->quantity);
103
    }
104
105
    /**
106
     * Return the total price for this item.
107
     *
108
     * @return float
109
     */
110 View Code Duplication
    private function totalPrice()
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...
111
    {
112
        $price = $this->price;
113
114
        if ($this->hasOptions()) {
115
            foreach ($this->data['options'] as $item) {
116
                if (array_key_exists('price', $item)) {
117
                    $price += $item['price'];
118
                }
119
            }
120
        }
121
122
        return $price;
123
    }
124
125
    /**
126
     * Return the total of the item, with or without tax.
127
     *
128
     * @param bool $includeTax Whether or not to include tax
129
     *
130
     * @return float The total, as a float
131
     */
132
    public function total($includeTax = true)
133
    {
134
        $price = $this->totalPrice();
135
136
        if ($includeTax) {
137
            $price = $this->tax->add($price);
138
        }
139
140
        return (float) ($price * $this->quantity);
141
    }
142
143
    /**
144
     * Return the total weight of the item.
145
     *
146
     * @return float The weight, as a float
147
     */
148 View Code Duplication
    public function weight()
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...
149
    {
150
        $weight = $this->weight;
151
152
        if ($this->hasOptions()) {
153
            foreach ($this->data['options'] as $item) {
154
                if (array_key_exists('weight', $item)) {
155
                    $weight += $item['weight'];
156
                }
157
            }
158
        }
159
160
        return (float) ($weight * $this->quantity);
161
    }
162
163
    /**
164
     * Return the total of the item, with or without tax.
165
     *
166
     * @param bool $includeTax Whether or not to include tax
167
     *
168
     * @return float The total, as a float
169
     */
170
    public function single($includeTax = true)
171
    {
172
        $price = $this->price;
173
174
        if ($this->hasOptions()) {
175
            foreach ($this->data['options'] as $item) {
176
                if (array_key_exists('price', $item)) {
177
                    $price += $item['price'];
178
                }
179
            }
180
        }
181
182
        if ($includeTax) {
183
            $price = $this->tax->add($price);
184
        }
185
186
        return (float) $price;
187
    }
188
189
    /**
190
     * Update a single key for this item, or multiple.
191
     *
192
     * @param array|string $key   The array key to update, or an array of key-value pairs to update
193
     * @param null         $value
194
     *
195
     * @return void
196
     */
197
    public function update($key, $value = null)
198
    {
199
        if (is_array($key)) {
200
            foreach ($key as $updateKey => $updateValue) {
201
                $this->update($updateKey, $updateValue);
202
            }
203
        } else {
204
            // Update the item
205
            $this->data[$key] = $value;
206
            if ($key == 'tax') {
207
                $this->tax = new Tax($value);
208
            }
209
        }
210
    }
211
212
    /**
213
     * Check if this item has options.
214
     *
215
     * @return bool Yes or no?
216
     */
217
    public function hasOptions()
218
    {
219
        return array_key_exists('options', $this->data) && !empty($this->data['options']);
220
    }
221
222
    /**
223
     * Convert the item into an array.
224
     *
225
     * @return array The item data
226
     */
227
    public function toArray()
228
    {
229
        return $this->data;
230
    }
231
}
232