Passed
Push — main ( ebd4af...845ee5 )
by Carsten
12:28
created

LaravelSession::data()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4.125

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 15
ccs 4
cts 8
cp 0.5
crap 4.125
rs 10
1
<?php
2
3
namespace Lenius\LaravelEcommerce\Storage;
4
5
use Illuminate\Support\Facades\Session;
6
use Lenius\Basket\ItemInterface;
7
use Lenius\Basket\Storage\Runtime;
8
use Lenius\Basket\StorageInterface;
9
10
class LaravelSession extends Runtime implements StorageInterface
11
{
12 4
    public function restore(): void
13
    {
14 4
        $carts = Session::get('cart');
15
16 4
        if ($carts) {
17
            static::$cart = $carts;
18
        }
19
    }
20
21
    /**
22
     * Add or update an item in the cart.
23
     *
24
     * @param ItemInterface $item The item to insert or update
25
     */
26 4
    public function insertUpdate(ItemInterface $item): void
27
    {
28 4
        static::$cart[$this->id][$item->identifier] = $item;
29
30 4
        $this->saveCart();
31
    }
32
33
    /**
34
     * Retrieve the cart data.
35
     *
36
     * @param bool $asArray
37
     *
38
     * @return array
39
     */
40 4
    public function &data($asArray = false): array
41
    {
42 4
        $cart = &static::$cart[$this->id];
43
44 4
        if (! $asArray) {
45 4
            return $cart;
46
        }
47
48
        $data = $cart;
49
50
        foreach ($data as &$item) {
51
            $item = $item->toArray();
52
        }
53
54
        return $data;
55
    }
56
57
    /**
58
     * Check if the item exists in the cart.
59
     *
60
     * @param string $identifier
61
     *
62
     * @return bool
63
     *
64
     * @internal param mixed $id
65
     */
66 4
    public function has(string $identifier): bool
67
    {
68 4
        foreach (static::$cart[$this->id] as $item) {
69
            if ($item->identifier == $identifier) {
70
                return true;
71
            }
72
        }
73
74 4
        return false;
75
    }
76
77
    /**
78
     * Get a single cart item by id.
79
     *
80
     * @param string $identifier
81
     *
82
     * @return bool|ItemInterface
83
     *
84
     * @internal param mixed $id The item id
85
     */
86 2
    public function item(string $identifier)
87
    {
88 2
        foreach (static::$cart[$this->id] as $item) {
89 2
            if ($item->identifier == $identifier) {
90 2
                return $item;
91
            }
92
        }
93
94
        return false;
95
    }
96
97
    /**
98
     * Returns the first occurance of an item with a given id.
99
     *
100
     * @param string $id The item id
101
     *
102
     * @return bool|ItemInterface
103
     */
104
    public function find(string $id)
105
    {
106
        foreach (static::$cart[$this->id] as $item) {
107
            if ($item->id == $id) {
108
                return $item;
109
            }
110
        }
111
112
        return false;
113
    }
114
115
    /**
116
     * Remove an item from the cart.
117
     *
118
     * @param mixed $id
119
     */
120 1
    public function remove($id): void
121
    {
122 1
        unset(static::$cart[$this->id][$id]);
123
124 1
        $this->saveCart();
125
    }
126
127
    /**
128
     * Destroy the cart.
129
     */
130 1
    public function destroy(): void
131
    {
132 1
        static::$cart[$this->id] = [];
133
134 1
        $this->saveCart();
135
    }
136
137
    /**
138
     * Set the cart identifier.
139
     *
140
     * @param string $identifier
141
     *
142
     * @internal param string $identifier
143
     */
144 4
    public function setIdentifier(string $identifier): void
145
    {
146 4
        $this->id = $identifier;
147
148 4
        if (! array_key_exists($this->id, static::$cart)) {
149 4
            static::$cart[$this->id] = [];
150
        }
151
152 4
        $this->saveCart();
153
    }
154
155
    /**
156
     * Return the current cart identifier.
157
     */
158
    public function getIdentifier(): string
159
    {
160
        return $this->id;
161
    }
162
163
    /**
164
     * Save Cart.
165
     */
166 4
    protected function saveCart(): void
167
    {
168 4
        $data = static::$cart;
169
170 4
        Session::put('cart', $data);
171
    }
172
}
173