Cart::offsetExists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Recca0120\Cart;
4
5
use ArrayAccess;
6
use ArrayIterator;
7
use IteratorAggregate;
8
9
class Cart implements ArrayAccess, IteratorAggregate
10
{
11
    /**
12
     * $items.
13
     *
14
     * @var \Illuminate\Support\Collection
15
     */
16
    public $items;
17
18
    /**
19
     * @param \Recca0120\Cart\Storage $storage
20
     */
21
    protected $storage;
22
23
    /**
24
     * __construct.
25
     *
26
     * @param \Recca0120\Cart\Storage $storage
27
     */
28 1
    public function __construct(Storage $storage = null)
29
    {
30 1
        $this->storage = is_null($storage) === true ? new Storage() : $storage;
31 1
        $this->restore();
32 1
    }
33
34
    /**
35
     * __destruct.
36
     */
37 1
    public function __destruct()
38
    {
39 1
        $this->store();
40 1
    }
41
42
    /**
43
     * put.
44
     *
45
     * @param \Recca0120\Cart\Item $item
46
     * @return $this
47
     */
48 1
    public function put(Item $item)
49
    {
50 1
        $this->items->put($item->getId(), $item);
51
52 1
        return $this;
53
    }
54
55
    /**
56
     * get.
57
     *
58
     * @param string $itemId
59
     * @return $this
60
     */
61
    public function get($itemId)
62
    {
63
        return $this->items->get($itemId);
64
    }
65
66
    /**
67
     * remove.
68
     *
69
     * @param  \Recca0120\Cart\Item | string $item
70
     * @return \Illuminate\Support\Collection
71
     */
72 1
    public function remove($item)
73
    {
74 1
        return $this->items->forget($item instanceof Item ? $item->getId() : $item);
75
    }
76
77
    /**
78
     * clear.
79
     *
80
     * @return $this
81
     */
82
    public function clear()
83
    {
84
        $this->items = $this->items->filter(function () {
85
            return false;
86
        });
87
88
        return $this;
89
    }
90
91
    /**
92
     * items.
93
     *
94
     * @return \Illuminate\Support\Collection
95
     */
96 1
    public function items()
97
    {
98 1
        return $this->items;
99
    }
100
101
    /**
102
     * count.
103
     *
104
     * @return int
105
     */
106 1
    public function count()
107
    {
108 1
        return $this->items->count();
109
    }
110
111
    /**
112
     * total.
113
     *
114
     * @return float
115
     */
116 1
    public function total()
117
    {
118
        return $this->items->sum(function ($item) {
119 1
            return $item->getPrice() * $item->getQuantity();
120 1
        });
121
    }
122
123
    /**
124
     * restore.
125
     *
126
     * @return $this
127
     */
128 1
    public function restore()
129
    {
130 1
        $this->items = $this->storage->restore();
131
132 1
        return $this;
133
    }
134
135
    /**
136
     * store.
137
     *
138
     * @return $this
139
     */
140 1
    public function store()
141
    {
142 1
        $this->storage->store($this->items);
143
144 1
        return $this;
145
    }
146
147
    /**
148
     * toArray.
149
     *
150
     * @return array
151
     */
152
    public function toArray()
153
    {
154
        return $this->items->map(function ($item) {
155
            return $item->toArray();
156
        })->toArray();
157
    }
158
159
    /**
160
     * toJson.
161
     *
162
     * @param int $option
163
     * @return string
164
     */
165
    public function toJson($option = 0)
166
    {
167
        return json_encode($this->toArray(), $option);
168
    }
169
170
    /**
171
     * Determine if an item exists at an offset.
172
     *
173
     * @param  mixed  $key
174
     * @return bool
175
     */
176
    public function offsetExists($key)
177
    {
178
        return $this->items->offsetExists($key);
179
    }
180
181
    /**
182
     * Get an item at a given offset.
183
     *
184
     * @param  mixed  $key
185
     * @return mixed
186
     */
187
    public function offsetGet($key)
188
    {
189
        return $this->items->offsetGet($key);
190
    }
191
192
    /**
193
     * Set the item at a given offset.
194
     *
195
     * @param  mixed  $key
196
     * @param  mixed  $value
197
     * @return void
198
     */
199
    public function offsetSet($key, $value)
200
    {
201
        $this->items->offsetSet($key, $value);
202
    }
203
204
    /**
205
     * Unset the item at a given offset.
206
     *
207
     * @param  string  $key
208
     * @return void
209
     */
210
    public function offsetUnset($key)
211
    {
212
        $this->items->offsetUnset($key);
213
    }
214
215
    /**
216
     * Get an iterator for the items.
217
     *
218
     * @return \ArrayIterator
219
     */
220
    public function getIterator()
221
    {
222
        return new ArrayIterator($this->items->all());
223
    }
224
}
225