Notify   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 176
ccs 29
cts 29
cp 1
c 0
b 0
f 0
rs 10
wmc 12
lcom 1
cbo 2

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A store() 0 4 1
A setStore() 0 6 1
A notifications() 0 4 1
A info() 0 4 1
A success() 0 4 1
A error() 0 4 1
A warning() 0 4 1
A flash() 0 6 1
A forget() 0 4 1
A isEmpty() 0 4 1
A isNotEmpty() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\Notify;
6
7
use Arcanedev\Notify\Contracts\Notify as NotifyContract;
8
use Arcanedev\Notify\Contracts\Store as StoreContract;
9
use Illuminate\Support\Collection;
10
use Illuminate\Support\Traits\Macroable;
11
12
/**
13
 * Class     Notify
14
 *
15
 * @author   ARCANEDEV <[email protected]>
16
 */
17
class Notify implements NotifyContract
18
{
19
    /* -----------------------------------------------------------------
20
     |  Traits
21
     | -----------------------------------------------------------------
22
     */
23
24 1
    use Macroable;
25
26
    /* -----------------------------------------------------------------
27
     |  Properties
28
     | -----------------------------------------------------------------
29
     */
30
31
    /**
32
     * The session writer.
33
     *
34
     * @var \Arcanedev\Notify\Contracts\Store
35
     */
36
    private $store;
37
38
    /* -----------------------------------------------------------------
39
     |  Constructor
40
     | -----------------------------------------------------------------
41
     */
42
43
    /**
44
     * Create a new flash notifier instance.
45
     *
46
     * @param  \Arcanedev\Notify\Contracts\Store  $store
47
     */
48 52
    public function __construct(StoreContract $store)
49
    {
50 52
        $this->setStore($store);
51 52
    }
52
53
    /* -----------------------------------------------------------------
54
     |  Getters & Setters
55
     | -----------------------------------------------------------------
56
     */
57
58
    /**
59
     * Get the store.
60
     *
61
     * @return \Arcanedev\Notify\Contracts\Store
62
     */
63 52
    public function store(): StoreContract
64
    {
65 52
        return $this->store;
66
    }
67
68
    /**
69
     * Set the store.
70
     *
71
     * @param  \Arcanedev\Notify\Contracts\Store  $store
72
     *
73
     * @return $this
74
     */
75 52
    public function setStore(StoreContract $store)
76
    {
77 52
        $this->store = $store;
78
79 52
        return $this;
80
    }
81
82
    /**
83
     * Get all the notifications.
84
     *
85
     * @return \Illuminate\Support\Collection
86
     */
87 40
    public function notifications(): Collection
88
    {
89 40
        return $this->store()->all();
90
    }
91
92
    /* -----------------------------------------------------------------
93
     |  Main Methods
94
     | -----------------------------------------------------------------
95
     */
96
97
    /**
98
     * Flash an information message.
99
     *
100
     * @param  string  $message
101
     * @param  array   $extra
102
     *
103
     * @return $this
104
     */
105 8
    public function info(string $message, array $extra = [])
106
    {
107 8
        return $this->flash($message, 'info', $extra);
108
    }
109
110
    /**
111
     * Flash a success message.
112
     *
113
     * @param  string  $message
114
     * @param  array   $extra
115
     *
116
     * @return $this
117
     */
118 8
    public function success(string $message, array $extra = [])
119
    {
120 8
        return $this->flash($message, 'success', $extra);
121
    }
122
123
    /**
124
     * Flash an error message.
125
     *
126
     * @param  string  $message
127
     * @param  array   $extra
128
     *
129
     * @return $this
130
     */
131 8
    public function error(string $message, array $extra = [])
132
    {
133 8
        return $this->flash($message, 'danger', $extra);
134
    }
135
136
    /**
137
     * Flash a warning message.
138
     *
139
     * @param  string  $message
140
     * @param  array   $extra
141
     *
142
     * @return $this
143
     */
144 8
    public function warning(string $message, array $extra = [])
145
    {
146 8
        return $this->flash($message, 'warning', $extra);
147
    }
148
149
    /**
150
     * Flash a new notification.
151
     *
152
     * @param  string       $message
153
     * @param  string|null  $type
154
     * @param  array        $extra
155
     *
156
     * @return $this
157
     */
158 40
    public function flash($message, $type = 'info', array $extra = [])
159
    {
160 40
        $this->store()->push(compact('message', 'type', 'extra'));
161
162 40
        return $this;
163
    }
164
165
    /**
166
     * Forget the notification.
167
     */
168 8
    public function forget(): void
169
    {
170 8
        $this->store->forget();
171 8
    }
172
173
    /**
174
     * Check if it has notifications.
175
     *
176
     * @return bool
177
     */
178 32
    public function isEmpty(): bool
179
    {
180 32
        return $this->store()->isEmpty();
181
    }
182
183
    /**
184
     * Check if there is no notifications.
185
     *
186
     * @return bool
187
     */
188 44
    public function isNotEmpty(): bool
189
    {
190 44
        return $this->store()->isNotEmpty();
191
    }
192
}
193