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

CartItem::subTotal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
c 7
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace LukePOLO\LaraCart;
4
5
use LukePOLO\LaraCart\Traits\CartOptionsMagicMethodsTrait;
6
7
/**
8
 * Class CartItem
9
 *
10
 * @package LukePOLO\LaraCart
11
 */
12
class CartItem
13
{
14
    use CartOptionsMagicMethodsTrait;
15
16
    protected $itemHash;
17
18
    public $locale;
19
    public $taxable;
20
    public $lineItem;
21
    public $subItems = [];
22
    public $couponInfo = [];
23
    public $internationalFormat;
24
25
    /**
26
     * CartItem constructor.
27
     *
28
     * @param $id
29
     * @param $name
30
     * @param integer $qty
31
     * @param string $price
32
     * @param array $options
33
     * @param boolean $taxable
34
     * @param bool|false $lineItem
35
     */
36
    public function __construct($id, $name, $qty, $price, $options = [], $taxable = true, $lineItem = false)
37
    {
38
        $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...
39
        $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...
40
        $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...
41
        $this->taxable = $taxable;
42
        $this->lineItem = $lineItem;
43
        $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...
44
        $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...
45
46
        foreach($options as $option => $value) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
47
            $this->$option = $value;
48
        }
49
    }
50
51
    /**
52
     * Generates a hash based on the cartItem array
53
     *
54
     * @param bool $force
55
     *
56
     * @return string itemHash
57
     */
58
    public function generateHash($force = false)
59
    {
60
        if ($this->lineItem === false) {
61
            $this->itemHash = null;
62
63
            $cartItemArray = (array)$this;
64
            
65
            unset($cartItemArray['options']['qty']);
66
            
67
            ksort($cartItemArray['options']);
68
69
            $this->itemHash = app(LaraCart::HASH, $cartItemArray);
70
        } elseif ($force || empty($this->itemHash) === true) {
71
            $this->itemHash = app(LaraCart::RANHASH);
72
        }
73
74
        app('events')->fire(
75
            'laracart.updateItem', [
76
                'item' => $this,
77
                'newHash' => $this->itemHash
78
            ]
79
        );
80
81
        return $this->itemHash;
82
    }
83
84
    /**
85
     * Gets the hash for the item
86
     *
87
     * @return mixed
88
     */
89
    public function getHash()
90
    {
91
        return $this->itemHash;
92
    }
93
94
    /**
95
     * Finds a sub item by its hash
96
     *
97
     * @param $subItemHash
98
     * @return mixed
99
     */
100
    public function findSubItem($subItemHash)
101
    {
102
        return array_get($this->subItems, $subItemHash);
103
    }
104
105
    /**
106
     * Adds an sub item to a item
107
     *
108
     * @param array $subItem
109
     *
110
     * @return CartSubItem $itemHash
111
     */
112
    public function addSubItem(array $subItem)
113
    {
114
        $subItem = new CartSubItem($subItem);
115
116
        $this->subItems[$subItem->getHash()] = $subItem;
117
118
        $this->generateHash();
119
120
        return $subItem;
121
    }
122
123
    /**
124
     * Removes a sub item from the item
125
     *
126
     * @param $subItemHash
127
     */
128
    public function removeSubItem($subItemHash)
129
    {
130
        unset($this->subItems[$subItemHash]);
131
132
        $this->generateHash();
133
    }
134
135
    /**
136
     * Gets the price of the item with or without tax, with the proper format
137
     *
138
     * @param bool $format
139
     *
140
     * @return string
141
     */
142
    public function price($format = true)
143
    {
144
        return LaraCart::formatMoney($this->price + $this->subItemsTotal(false), $this->locale, $this->internationalFormat, $format);
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...
145
    }
146
147
    /**
148
     * Gets the sub total of the item based on the qty with or without tax in the proper format
149
     *
150
     * @param bool $format
151
     * @param bool $withDiscount
152
     *
153
     * @return string
154
     */
155
    public function subTotal($format = true, $withDiscount = true)
156
    {
157
        $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...
158
159
        if ($withDiscount) {
160
            $total -= $this->getDiscount(false);
161
        }
162
163
        return LaraCart::formatMoney($total, $this->locale, $this->internationalFormat, $format);
164
    }
165
166
167
    /**
168
     * Gets the totals for the options
169
     *
170
     * @param boolean $format
171
     *
172
     * @return string
173
     */
174
    public function subItemsTotal($format = true)
175
    {
176
        $total = 0;
177
178
        foreach ($this->subItems as $subItem) {
179
            $total += $subItem->price(false);
180
        }
181
182
        $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...
183
184
        return LaraCart::formatMoney($total, $this->locale, $this->internationalFormat, $format);
185
    }
186
187
    /**
188
     * Gets the discount of an item
189
     *
190
     * @param boolean $format
191
     *
192
     * @return string
193
     */
194
    public function getDiscount($format = true)
195
    {
196
        return LaraCart::formatMoney(
197
            $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...
198
            $this->locale,
199
            $this->internationalFormat,
200
            $format
201
        );
202
    }
203
204
    public function tax()
205
    {
206
        $tax = 0;
207
208
        if($this->taxable) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
209
            return $this->tax * $this->price;
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...
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...
210
        }
211
212
        return $tax;
213
    }
214
}
215