Completed
Push — master ( f064a2...98e35e )
by ARCANEDEV
09:12
created

Notify::ready()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
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\SessionStore;
5
use Illuminate\Support\Arr;
6
7
/**
8
 * Class     Notify
9
 *
10
 * @package  Arcanedev\Notify
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class Notify implements NotifyContract
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Properties
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * Session prefix name.
21
     *
22
     * @var string
23
     */
24
    protected $sessionPrefix;
25
26
    /**
27
     * The session writer.
28
     *
29
     * @var \Arcanedev\Notify\Contracts\SessionStore
30
     */
31
    private $session;
32
33
    /* ------------------------------------------------------------------------------------------------
34
     |  Constructor
35
     | ------------------------------------------------------------------------------------------------
36
     */
37
    /**
38
     * Create a new flash notifier instance.
39
     *
40
     * @param  \Arcanedev\Notify\Contracts\SessionStore  $session
41
     * @param  string                                    $prefix
42
     */
43 156
    public function __construct(SessionStore $session, $prefix)
44
    {
45 156
        $this->session       = $session;
46 156
        $this->sessionPrefix = $prefix;
47 156
    }
48
49
    /* ------------------------------------------------------------------------------------------------
50
     |  Getters & Setters
51
     | ------------------------------------------------------------------------------------------------
52
     */
53
    /**
54
     * Get the notification message.
55
     *
56
     * @return string
57
     */
58 156
    public function message()
59
    {
60 156
        return $this->getSession('message');
61
    }
62
63
    /**
64
     * Get the notification type.
65
     *
66
     * @return string
67
     */
68 144
    public function type()
69
    {
70 144
        return $this->getSession('type');
71
    }
72
73
    /**
74
     * Get an additional stored options.
75
     *
76
     * @param  bool  $assoc
77
     *
78
     * @return array
79
     */
80 144
    public function options($assoc = false)
81
    {
82 144
        return json_decode($this->getSession('options'), $assoc);
83
    }
84
85
    /**
86
     * Get a notification option.
87
     *
88
     * @param  string      $key
89
     * @param  mixed|null  $default
90
     *
91
     * @return mixed
92
     */
93 36
    public function option($key, $default = null)
94
    {
95 36
        return Arr::get($this->options(true), $key, $default);
96
    }
97
98
    /**
99
     * Check if the flash notification has an option.
100
     *
101
     * @param  string  $key
102
     *
103
     * @return bool
104
     */
105 12
    public function hasOption($key)
106
    {
107 12
        return Arr::has($this->options(true), $key);
108
    }
109
110
    /**
111
     * If the notification is ready to be shown.
112
     *
113
     * @return bool
114
     */
115 156
    public function ready()
116
    {
117 156
        return ! empty($this->message());
118
    }
119
120
    /* ------------------------------------------------------------------------------------------------
121
     |  Main Functions
122
     | ------------------------------------------------------------------------------------------------
123
     */
124
    /**
125
     * Flash a message.
126
     *
127
     * @param  string  $message
128
     * @param  string  $type
129
     * @param  array   $options
130
     *
131
     * @return self
132
     */
133 144
    public function flash($message, $type = '', array $options = [])
134
    {
135 144
        $this->session->flash([
136 144
            $this->getPrefixedName('message') => $message,
137 144
            $this->getPrefixedName('type')    => $type,
138 144
            $this->getPrefixedName('options') => json_encode($options),
139 48
        ]);
140
141 144
        return $this;
142
    }
143
144
    /* ------------------------------------------------------------------------------------------------
145
     |  Other Functions
146
     | ------------------------------------------------------------------------------------------------
147
     */
148
    /**
149
     * Prefix the name.
150
     *
151
     * @param  string  $name
152
     *
153
     * @return string
154
     */
155 156
    private function getPrefixedName($name)
156
    {
157 156
        return "{$this->sessionPrefix}.$name";
158
    }
159
160
    /**
161
     * Get session value.
162
     *
163
     * @param  string  $name
164
     *
165
     * @return mixed
166
     */
167 156
    private function getSession($name)
168
    {
169 156
        return $this->session->get(
170 156
            $this->getPrefixedName($name)
171 52
        );
172
    }
173
}
174