Completed
Pull Request — master (#72)
by Luke
02:47
created

CartItem::setModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
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
     * Finds a sub item by its hash
99
     *
100
     * @param $subItemHash
101
     * @return mixed
102
     */
103
    public function findSubItem($subItemHash)
104
    {
105
        return array_get($this->subItems, $subItemHash);
106
    }
107
108
    /**
109
     * Adds an sub item to a item
110
     *
111
     * @param array $subItem
112
     *
113
     * @return CartSubItem $itemHash
114
     */
115
    public function addSubItem(array $subItem)
116
    {
117
        $subItem = new CartSubItem($subItem);
118
119
        $this->subItems[$subItem->getHash()] = $subItem;
120
121
        $this->generateHash();
122
123
        return $subItem;
124
    }
125
126
    /**
127
     * Removes a sub item from the item
128
     *
129
     * @param $subItemHash
130
     */
131
    public function removeSubItem($subItemHash)
132
    {
133
        unset($this->subItems[$subItemHash]);
134
135
        $this->generateHash();
136
    }
137
138
    /**
139
     * Gets the price of the item with or without tax, with the proper format
140
     *
141
     * @param bool $format
142
     *
143
     * @return string
144
     */
145
    public function price($format = true)
146
    {
147
        return LaraCart::formatMoney($this->price + $this->subItemsTotal(false), $this->locale,
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...
148
            $this->internationalFormat, $format);
149
    }
150
151
    /**
152
     * Gets the sub total of the item based on the qty with or without tax in the proper format
153
     *
154
     * @param bool $format
155
     * @param bool $withDiscount
156
     *
157
     * @return string
158
     */
159
    public function subTotal($format = true, $withDiscount = true)
160
    {
161
        $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...
162
163
        if ($withDiscount) {
164
            $total -= $this->getDiscount(false);
165
        }
166
167
        return LaraCart::formatMoney($total, $this->locale, $this->internationalFormat, $format);
168
    }
169
170
171
    /**
172
     * Gets the totals for the options
173
     *
174
     * @param boolean $format
175
     *
176
     * @return string
177
     */
178
    public function subItemsTotal($format = true)
179
    {
180
        $total = 0;
181
182
        foreach ($this->subItems as $subItem) {
183
            $total += $subItem->price(false);
184
        }
185
186
        $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...
187
188
        return LaraCart::formatMoney($total, $this->locale, $this->internationalFormat, $format);
189
    }
190
191
    /**
192
     * Gets the discount of an item
193
     *
194
     * @param boolean $format
195
     *
196
     * @return string
197
     */
198
    public function getDiscount($format = true)
199
    {
200
        return LaraCart::formatMoney(
201
            $this->discount,
0 ignored issues
show
Documentation introduced by
The property discount 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...
202
            $this->locale,
203
            $this->internationalFormat,
204
            $format
205
        );
206
    }
207
208
    /**
209
     * Gets the tax for the item
210
     *
211
     * @return int|mixed
212
     */
213
    public function tax()
214
    {
215
        $tax = 0;
216
217
        if ($this->taxable) {
218
            return $this->tax * $this->price(false);
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...
219
        }
220
221
        return $tax;
222
    }
223
224
    /**
225
     * Sets the related model to the item
226
     *
227
     * @param $itemModel
228
     *
229
     * @throws ModelNotFound
230
     */
231
    public function setModel($itemModel)
232
    {
233
        if(!class_exists($itemModel)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
234
            throw new ModelNotFound();
235
        }
236
        $this->itemModel = $itemModel;
237
    }
238
239
    /**
240
     * Returns a Model
241
     *
242
     * @throws
243
     */
244
    public function getModel()
245
    {
246
        $itemModel = new $this->itemModel;
247
248
        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...
249
    }
250
}
251