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

Notify::error()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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