DatabaseStorage::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of ibrand/laravel-shopping-cart.
5
 *
6
 * (c) iBrand <https://www.ibrand.cc>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace iBrand\Shoppingcart\Storage;
13
14
use DB;
15
use iBrand\Shoppingcart\Item;
16
use Illuminate\Database\Eloquent\Collection;
17
18
/**
19
 * Class DatabaseStorage.
20
 */
21
class DatabaseStorage implements Storage
22
{
23
    /**
24
     * @var string
25
     */
26
    private $table = 'shopping_cart';
27
28
    /**
29
     * @var array
30
     */
31
    private $filed = ['__raw_id', 'id', 'name', 'qty', 'price', 'total', '__model', 'type', 'status'];
32
33
    /**
34
     * @param $key
35
     * @param $values
36
     */
37
    public function set($key, $values)
38
    {
39
        if (is_null($values)) {
40
            $this->forget($key);
41
42
            return;
43
        }
44
45
        $rawIds = $values->pluck('__raw_id')->toArray();
46
47
        //Delete the data that has been removed from cart.
48
        DB::table($this->table)->whereNotIn('__raw_id', $rawIds)->where('key', $key)->delete();
49
50
        $keys = explode('.', $key);
51
52
        $userId = end($keys);
53
        $guard = prev($keys);
54
55
        $values = $values->toArray();
56
        foreach ($values as $value) {
57
            $item = array_only($value, $this->filed);
0 ignored issues
show
Deprecated Code introduced by
The function array_only() has been deprecated with message: Arr::only() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
58
            $attr = json_encode(array_except($value, $this->filed));
0 ignored issues
show
Deprecated Code introduced by
The function array_except() has been deprecated with message: Arr::except() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
59
            $insert = array_merge($item, ['attributes' => $attr, 'key' => $key, 'guard' => $guard, 'user_id' => $userId]);
60
            if (DB::table($this->table)->where(['key' => $key, '__raw_id' => $item['__raw_id']])->first()) {
61
                DB::table($this->table)->where(['key' => $key, '__raw_id' => $item['__raw_id']])
62
                    ->update(array_except($insert, ['key', '__raw_id']));
0 ignored issues
show
Deprecated Code introduced by
The function array_except() has been deprecated with message: Arr::except() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
63
            } else {
64
                DB::table($this->table)->insert($insert);
65
            }
66
        }
67
    }
68
69
    /**
70
     * @param $key
71
     * @param null $default
72
     *
73
     * @return Collection
74
     */
75
    public function get($key, $default = null)
76
    {
77
        $items = DB::table($this->table)->where('key', $key)->get();
78
79
        $items = $items->toArray();
80
        $collection = [];
81
        foreach ($items as $item) {
82
            $item = json_decode(json_encode($item), true);
83
            $attr = json_decode($item['attributes'], true);
84
            $item = array_only($item, $this->filed);
0 ignored issues
show
Deprecated Code introduced by
The function array_only() has been deprecated with message: Arr::only() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
85
            $item = array_merge($item, $attr);
86
            $collection[$item['__raw_id']] = new Item($item);
87
        }
88
89
        return new Collection($collection);
90
    }
91
92
    /**
93
     * @param $key
94
     */
95
    public function forget($key)
96
    {
97
        DB::table($this->table)->where('key', $key)->delete();
98
    }
99
}
100