Completed
Push — master ( 86fb23...6085dd )
by ARCANEDEV
25s
created

SessionStore::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 4
cts 4
cp 1
crap 1
1
<?php namespace Arcanedev\Notify\Stores;
2
3
use Arcanedev\Notify\Contracts\Store;
4
use Illuminate\Contracts\Session\Session as SessionContract;
5
use Illuminate\Support\Collection;
6
7
/**
8
 * Class     Session
9
 *
10
 * @package  Arcanedev\Notify
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class SessionStore implements Store
14
{
15
    /* -----------------------------------------------------------------
16
     |  Properties
17
     | -----------------------------------------------------------------
18
     */
19
20
    /**
21
     * The Illuminate Session instance.
22
     *
23
     * @var \Illuminate\Contracts\Session\Session
24
     */
25
    private $session;
26
27
    /**
28
     * The store's options.
29
     *
30
     * @var array
31
     */
32
    protected $options;
33
34
    /* -----------------------------------------------------------------
35
     |  Constructor
36
     | -----------------------------------------------------------------
37
     */
38
39
    /**
40
     * Make session store instance.
41
     *
42
     * @param  \Illuminate\Contracts\Session\Session  $session
43
     * @param  array                                  $options
44
     */
45 48
    public function __construct(SessionContract $session, array $options)
46
    {
47 48
        $this->session = $session;
48 48
        $this->options = $options;
49 48
    }
50
51
    /* -----------------------------------------------------------------
52
     |  Getters
53
     | -----------------------------------------------------------------
54
     */
55
56
    /**
57
     * Get the session key.
58
     *
59
     * @return string
60
     */
61 48
    protected function getSessionKey(): string
62
    {
63 48
        return $this->options['key'];
64
    }
65
66
    /* -----------------------------------------------------------------
67
     |  Main Methods
68
     | -----------------------------------------------------------------
69
     */
70
71
    /**
72
     * Get all the notifications.
73
     *
74
     * @return \Illuminate\Support\Collection
75
     */
76 48
    public function all(): Collection
77
    {
78 48
        return new Collection(
79 48
            $this->session->get($this->getSessionKey(), [])
80
        );
81
    }
82
83
    /**
84
     * Push the new notification.
85
     *
86
     * @param  array  $notification
87
     */
88 40
    public function push(array $notification)
89
    {
90 40
        $this->session->flash(
91 40
            $this->getSessionKey(),
92 40
            $this->all()->push($notification)
93
        );
94 40
    }
95
96
    /**
97
     * Forget the notifications.
98
     *
99
     * @return void
100
     */
101 8
    public function forget()
102
    {
103 8
        $this->session->forget($this->getSessionKey());
104 8
    }
105
106
    /**
107
     * Check if it has notifications.
108
     *
109
     * @return bool
110
     */
111 40
    public function isEmpty(): bool
112
    {
113 40
        return $this->all()->isEmpty();
114
    }
115
116
    /**
117
     * Check if there is no notifications.
118
     *
119
     * @return bool
120
     */
121 40
    public function isNotEmpty(): bool
122
    {
123 40
        return ! $this->isEmpty();
124
    }
125
}
126