Completed
Push — master ( 089d30...2307ae )
by Luke
02:12
created

CartSubItem::price()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 15
rs 8.8571
cc 5
eloc 8
nc 2
nop 2
1
<?php
2
3
namespace LukePOLO\LaraCart;
4
5
use LukePOLO\LaraCart\Traits\CartOptionsMagicMethodsTrait;
6
7
/**
8
 * Class CartItemOption
9
 *
10
 * @property float price
11
 * @property array options
12
 * @property array items
13
 *
14
 * @package LukePOLO\LaraCart
15
 */
16
class CartSubItem
17
{
18
    use CartOptionsMagicMethodsTrait;
19
20
    const ITEMS = 'items';
21
22
    public $locale;
23
    public $internationalFormat;
24
25
    private $itemHash;
26
27
    /**
28
     * @param $options
29
     */
30
    public function __construct($options)
31
    {
32
        $this->options['items'] = [];
33
34
        foreach ($options as $option => $value) {
35
            array_set($this->options, $option, $value);
36
        }
37
38
        $this->itemHash = app(LaraCart::HASH, $this->options);
39
    }
40
41
    /**
42
     * Gets the hash for the item
43
     *
44
     * @return mixed
45
     */
46
    public function getHash()
47
    {
48
        return $this->itemHash;
49
    }
50
51
    /**
52
     * Gets the formatted price
53
     *
54
     * @param bool|true $format
55
     * @param bool $taxedItemsOnly
56
     *
57
     * @return string
58
     */
59
    public function price($format = true, $taxedItemsOnly = true)
60
    {
61
        $price = $this->price;
62
63
        if (isset($this->items)) {
64
            foreach ($this->items as $item) {
65
                if($taxedItemsOnly && !$item->taxable) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
66
                    continue;
67
                }
68
                $price += $item->price(false);
69
            }
70
        }
71
72
        return LaraCart::formatMoney($price, $this->locale, $this->internationalFormat, $format);
73
    }
74
}
75