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

Storage   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 92.31%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 4
dl 0
loc 65
ccs 12
cts 13
cp 0.9231
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A restore() 0 4 1
A hash() 0 4 1
A __construct() 0 5 2
A store() 0 10 2
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