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