Completed
Pull Request — master (#75)
by
unknown
02:15
created

CartItem::find()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
3
namespace LukePOLO\LaraCart;
4
5
use LukePOLO\LaraCart\Exceptions\ModelNotFound;
6
use LukePOLO\LaraCart\Traits\CartOptionsMagicMethodsTrait;
7
8
/**
9
 * Class CartItem
10
 *
11
 * @package LukePOLO\LaraCart
12
 */
13
class CartItem
14
{
15
    use CartOptionsMagicMethodsTrait;
16
17
    protected $itemHash;
18
    protected $itemModel;
19
20
    public $locale;
21
    public $taxable;
22
    public $lineItem;
23
    public $subItems = [];
24
    public $couponInfo = [];
25
    public $internationalFormat;
26
27
    /**
28
     * CartItem constructor.
29
     *
30
     * @param $id
31
     * @param $name
32
     * @param integer $qty
33
     * @param string $price
34
     * @param array $options
35
     * @param boolean $taxable
36
     * @param bool|false $lineItem
37
     */
38
    public function __construct($id, $name, $qty, $price, $options = [], $taxable = true, $lineItem = false)
39
    {
40
        $this->id = $id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<LukePOLO\LaraCart\CartItem>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
41
        $this->qty = $qty;
0 ignored issues
show
Documentation introduced by
The property qty does not exist on object<LukePOLO\LaraCart\CartItem>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
42
        $this->name = $name;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<LukePOLO\LaraCart\CartItem>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
43
        $this->taxable = $taxable;
44
        $this->lineItem = $lineItem;
45
        $this->price = floatval($price);
0 ignored issues
show
Documentation introduced by
The property price does not exist on object<LukePOLO\LaraCart\CartItem>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
46
        $this->tax = config('laracart.tax');
0 ignored issues
show
Documentation introduced by
The property tax does not exist on object<LukePOLO\LaraCart\CartItem>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
47
        $this->itemModel = config('laracart.item_model');
48
49
        foreach ($options as $option => $value) {
50
            $this->$option = $value;
51
        }
52
    }
53
54
    /**
55
     * Generates a hash based on the cartItem array
56
     *
57
     * @param bool $force
58
     *
59
     * @return string itemHash
60
     */
61
    public function generateHash($force = false)
62
    {
63
        if ($this->lineItem === false) {
64
            $this->itemHash = null;
65
66
            $cartItemArray = (array)$this;
67
68
            unset($cartItemArray['options']['qty']);
69
70
            ksort($cartItemArray['options']);
71
72
            $this->itemHash = app(LaraCart::HASH, $cartItemArray);
73
        } elseif ($force || empty($this->itemHash) === true) {
74
            $this->itemHash = app(LaraCart::RANHASH);
75
        }
76
77
        app('events')->fire(
78
            'laracart.updateItem', [
79
                'item' => $this,
80
                'newHash' => $this->itemHash
81
            ]
82
        );
83
84
        return $this->itemHash;
85
    }
86
87
    /**
88
     * Gets the hash for the item
89
     *
90
     * @return mixed
91
     */
92
    public function getHash()
93
    {
94
        return $this->itemHash;
95
    }
96
97
    /**
98
     * Search for matching options on the item
99
     *
100
     * @return mixed
101
     */
102
    public function find($data)
103
    {
104
        foreach ($data as $key => $value) {
105
            if ($this->$key !== $value) {
106
                return false;
107
            }
108
        }
109
110
        return $this;
111
    }
112
113
    /**
114
     * Finds a sub item by its hash
115
     *
116
     * @param $subItemHash
117
     * @return mixed
118
     */
119
    public function findSubItem($subItemHash)
120
    {
121
        return array_get($this->subItems, $subItemHash);
122
    }
123
124
    /**
125
     * Adds an sub item to a item
126
     *
127
     * @param array $subItem
128
     *
129
     * @return CartSubItem $itemHash
130
     */
131
    public function addSubItem(array $subItem)
132
    {
133
        $subItem = new CartSubItem($subItem);
134
135
        $this->subItems[$subItem->getHash()] = $subItem;
136
137
        $this->generateHash();
138
139
        return $subItem;
140
    }
141
142
    /**
143
     * Removes a sub item from the item
144
     *
145
     * @param $subItemHash
146
     */
147
    public function removeSubItem($subItemHash)
148
    {
149
        unset($this->subItems[$subItemHash]);
150
151
        $this->generateHash();
152
    }
153
154
    /**
155
     * Gets the price of the item with or without tax, with the proper format
156
     *
157
     * @param bool $format
158
     *
159
     * @return string
160
     */
161
    public function price($format = true)
162
    {
163
        return LaraCart::formatMoney(
164
            $this->price + $this->subItemsTotal(false),
0 ignored issues
show
Documentation introduced by
The property price does not exist on object<LukePOLO\LaraCart\CartItem>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
165
            $this->locale,
166
            $this->internationalFormat, $format
167
        );
168
    }
169
170
    /**
171
     * Gets the sub total of the item based on the qty with or without tax in the proper format
172
     *
173
     * @param bool $format
174
     * @param bool $withDiscount
175
     *
176
     * @return string
177
     */
178
    public function subTotal($format = true, $withDiscount = true)
179
    {
180
        $total = $this->price(false) * $this->qty;
0 ignored issues
show
Documentation introduced by
The property qty does not exist on object<LukePOLO\LaraCart\CartItem>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
181
182
        if ($withDiscount) {
183
            $total -= $this->getDiscount(false);
184
        }
185
186
        return LaraCart::formatMoney($total, $this->locale, $this->internationalFormat, $format);
187
    }
188
189
190
    /**
191
     * Gets the totals for the options
192
     *
193
     * @param boolean $format
194
     *
195
     * @return string
196
     */
197
    public function subItemsTotal($format = true)
198
    {
199
        $total = 0;
200
201
        foreach ($this->subItems as $subItem) {
202
            $total += $subItem->price(false);
203
        }
204
205
        $total *= $this->qty;
0 ignored issues
show
Documentation introduced by
The property qty does not exist on object<LukePOLO\LaraCart\CartItem>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
206
207
        return LaraCart::formatMoney($total, $this->locale, $this->internationalFormat, $format);
208
    }
209
210
    /**
211
     * Gets the discount of an item
212
     *
213
     * @param boolean $format
214
     *
215
     * @return string
216
     */
217
    public function getDiscount($format = true)
218
    {
219
        $amount = 0;
220
221
        if (app('laracart')->findCoupon($this->code)) {
0 ignored issues
show
Documentation introduced by
The property code does not exist on object<LukePOLO\LaraCart\CartItem>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
222
            $amount = $this->discount;
0 ignored issues
show
Documentation introduced by
The property discount does not exist on object<LukePOLO\LaraCart\CartItem>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
223
        }
224
225
        return LaraCart::formatMoney(
226
            $amount,
227
            $this->locale,
228
            $this->internationalFormat,
229
            $format
230
        );
231
    }
232
233
    /**
234
     * Gets the tax for the item
235
     *
236
     * @return int|mixed
237
     */
238
    public function tax($amountNotTaxable = 0)
239
    {
240
        $tax = 0;
241
242
        if ($this->taxable) {
243
            return $this->tax * ($this->subTotal(false) - $amountNotTaxable);
0 ignored issues
show
Documentation introduced by
The property tax does not exist on object<LukePOLO\LaraCart\CartItem>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
244
        }
245
246
        return $tax;
247
    }
248
249
    /**
250
     * Sets the related model to the item
251
     *
252
     * @param $itemModel
253
     *
254
     * @throws ModelNotFound
255
     */
256
    public function setModel($itemModel)
257
    {
258
        if (!class_exists($itemModel)) {
259
            throw new ModelNotFound();
260
        }
261
        $this->itemModel = $itemModel;
262
    }
263
264
    /**
265
     * Returns a Model
266
     *
267
     * @throws
268
     */
269
    public function getModel()
270
    {
271
        $itemModel = new $this->itemModel;
272
273
        return $itemModel->findOrFail($this->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<LukePOLO\LaraCart\CartItem>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
274
    }
275
}
276