Completed
Pull Request — master (#4)
by ARCANEDEV
04:20
created

Notify::hasOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
ccs 0
cts 0
cp 0
crap 2
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 120
     */
43
    public function __construct(SessionStore $session, $prefix)
44 120
    {
45 120
        $this->session       = $session;
46 120
        $this->sessionPrefix = $prefix;
47
    }
48
49
    /* ------------------------------------------------------------------------------------------------
50
     |  Getters & Setters
51
     | ------------------------------------------------------------------------------------------------
52
     */
53
    /**
54
     * Get the notification message.
55
     *
56
     * @return string
57 120
     */
58
    public function message()
59 120
    {
60
        return $this->getSession('message');
61
    }
62
63
    /**
64
     * Get the notification type.
65
     *
66
     * @return string
67 120
     */
68
    public function type()
69 120
    {
70
        return $this->getSession('type');
71
    }
72
73
    /**
74
     * Get an additional stored options.
75
     *
76
     * @param  bool  $assoc
77
     *
78
     * @return array
79 120
     */
80
    public function options($assoc = false)
81 120
    {
82
        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 36
     */
93
    public function option($key, $default = null)
94 36
    {
95
        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 120
     *
103
     * @return bool
104 120
     */
105
    public function hasOption($key)
106
    {
107
        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
    public function ready()
116
    {
117
        return ! empty($this->message());
118
    }
119
120 120
    /* ------------------------------------------------------------------------------------------------
121
     |  Main Functions
122 120
     | ------------------------------------------------------------------------------------------------
123 120
     */
124 120
    /**
125 120
     * Flash a message.
126 90
     *
127
     * @param  string  $message
128 120
     * @param  string  $type
129
     * @param  array   $options
130
     *
131
     * @return self
132
     */
133
    public function flash($message, $type = '', array $options = [])
134
    {
135
        $this->session->flash([
136 120
            $this->getPrefixedName('message') => $message,
137
            $this->getPrefixedName('type')    => $type,
138 120
            $this->getPrefixedName('options') => json_encode($options),
139
        ]);
140
141
        return $this;
142
    }
143
144
    /* ------------------------------------------------------------------------------------------------
145
     |  Other Functions
146
     | ------------------------------------------------------------------------------------------------
147
     */
148 120
    /**
149
     * Prefix the name.
150 120
     *
151 120
     * @param  string  $name
152 90
     *
153
     * @return string
154
     */
155
    private function getPrefixedName($name)
156
    {
157
        return "{$this->sessionPrefix}.$name";
158
    }
159
160
    /**
161
     * Get session value.
162
     *
163
     * @param  string  $name
164
     *
165
     * @return mixed
166
     */
167
    private function getSession($name)
168
    {
169
        return $this->session->get(
170
            $this->getPrefixedName($name)
171
        );
172
    }
173
}
174