Completed
Push — master ( 863824...d57b68 )
by recca
05:22
created

Storage::store()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2.032
1
<?php
2
3
namespace Recca0120\Cart;
4
5
use Illuminate\Support\Collection;
6
use Symfony\Component\HttpFoundation\Session\Session;
7
use Symfony\Component\HttpFoundation\Session\SessionInterface;
8
9
class Storage
10
{
11
    /**
12
     * $name.
13
     *
14
     * @var string
15
     */
16
    protected $name;
17
18
    /**
19
     * $session.
20
     *
21
     * @var \Symfony\Component\HttpFoundation\Session\SessionInterface|\Illuminate\Contracts\Session\Session
22
     */
23
    protected $session;
24
25
    /**
26
     * __construct.
27
     *
28
     * @param string $name
29
     * @param \Symfony\Component\HttpFoundation\Session\SessionInterface|\Illuminate\Contracts\Session\Session $session
30
     */
31 2
    public function __construct($name = 'default', $session = null)
32
    {
33 2
        $this->name = $name;
34 2
        $this->session = is_null($session) === true ? new Session() : $session;
35 2
    }
36
37
    /**
38
     * store.
39
     *
40
     * @param mixed $value
41
     * @return $this
42
     */
43 1
    public function store($value)
44
    {
45 1
        if ($this->session instanceof SessionInterface) {
46
            $this->session->set($this->hash(), $value);
47
        } else {
48 1
            $this->session->put($this->hash(), $value);
49
        }
50
51 1
        return $this;
52
    }
53
54
    /**
55
     * restore.
56
     *
57
     * @return mixed
58
     */
59 1
    public function restore()
60
    {
61 1
        return $this->session->get($this->hash(), new Collection);
62
    }
63
64
    /**
65
     * hash.
66
     *
67
     * @return string
68
     */
69 2
    protected function hash()
70
    {
71 2
        return hash('sha256', __NAMESPACE__.$this->name);
72
    }
73
}
74