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

CartSubItem   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 7
Bugs 1 Features 0
Metric Value
wmc 8
c 7
b 1
f 0
lcom 1
cbo 2
dl 0
loc 59
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A getHash() 0 4 1
B price() 0 15 5
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