FlashMessage::hops()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Coderello\Laraflash\FlashMessage;
4
5
use ArrayAccess;
6
use Coderello\Laraflash\Exceptions\InvalidDelayException;
7
use Coderello\Laraflash\Exceptions\InvalidHopsAmountException;
8
use Illuminate\Contracts\Support\Arrayable;
9
use Illuminate\Contracts\Support\Jsonable;
10
use JsonSerializable;
11
12
class FlashMessage implements ArrayAccess, Arrayable, Jsonable, JsonSerializable
13
{
14
    /** @var string|null */
15
    protected $content;
16
17
    /** @var string|null */
18
    protected $title;
19
20
    /** @var string|null */
21
    protected $type;
22
23
    /** @var int|null */
24
    protected $hops = 1;
25
26
    /** @var int|null */
27
    protected $delay = 1;
28
29
    /** @var array */
30
    protected $attributes = [];
31
32 1
    public function content(?string $content): self
33
    {
34 1
        if (! is_null($content)) {
35 1
            $this->content = $content;
36
        } else {
37
            unset($this->content);
38
        }
39
40 1
        return $this;
41
    }
42
43 1
    public function title(?string $title): self
44
    {
45 1
        if (! is_null($title)) {
46 1
            $this->title = $title;
47
        } else {
48
            unset($this->title);
49
        }
50
51 1
        return $this;
52
    }
53
54 6
    public function type(?string $type): self
55
    {
56 6
        if (! is_null($type)) {
57 6
            $this->type = $type;
58
        } else {
59
            unset($this->type);
60
        }
61
62 6
        return $this;
63
    }
64
65 1
    public function danger(): self
66
    {
67 1
        $this->type('danger');
68
69 1
        return $this;
70
    }
71
72 1
    public function warning(): self
73
    {
74 1
        $this->type('warning');
75
76 1
        return $this;
77
    }
78
79 2
    public function info(): self
80
    {
81 2
        $this->type('info');
82
83 2
        return $this;
84
    }
85
86 1
    public function success(): self
87
    {
88 1
        $this->type('success');
89
90 1
        return $this;
91
    }
92
93 7
    public function hops(int $hops): self
94
    {
95 7
        if ($hops < 1) {
96 1
            throw new InvalidHopsAmountException;
97
        }
98
99 6
        $this->hops = $hops;
100
101 6
        return $this;
102
    }
103
104 11
    public function delay(int $delay): self
105
    {
106 11
        if ($delay < 0) {
107 1
            throw new InvalidDelayException;
108
        }
109
110 10
        $this->delay = $delay;
111
112 10
        return $this;
113
    }
114
115 5
    public function now(): self
116
    {
117 5
        $this->delay(0);
118
119 5
        return $this;
120
    }
121
122 2
    public function keep(): self
123
    {
124 2
        $this->hops++;
125
126 2
        return $this;
127
    }
128
129 12
    public function attribute(string $key, $value = null): self
130
    {
131 12
        if (! is_null($value)) {
132 12
            $this->attributes[$key] = $value;
133
        } else {
134 1
            unset($this->attributes[$key]);
135
        }
136
137 12
        return $this;
138
    }
139
140 9
    public function get(string $key)
141
    {
142 9
        if (in_array($key, ['title', 'content', 'hops', 'delay', 'type'])) {
143 9
            return $this->{$key};
144
        }
145
146 1
        return $this->attributes[$key] ?? null;
147
    }
148
149 24
    public function toArray()
150
    {
151 24
        return array_merge(
152 24
            $this->attributes, [
153 24
                'content' => $this->content,
154 24
                'title' => $this->title,
155 24
                'type' => $this->type,
156 24
                'hops' => $this->hops,
157 24
                'delay' => $this->delay,
158
            ]
159
        );
160
    }
161
162 1
    public function toJson($options = 0)
163
    {
164 1
        return json_encode($this, $options);
165
    }
166
167 2
    public function jsonSerialize()
168
    {
169 2
        return $this->toArray();
170
    }
171
172 1
    public function offsetExists($offset)
173
    {
174 1
        return isset($this->attributes[$offset]);
175
    }
176
177 2
    public function offsetGet($offset)
178
    {
179 2
        return $this->attributes[$offset];
180
    }
181
182 1
    public function offsetSet($offset, $value)
183
    {
184 1
        $this->attribute($offset, $value);
185
    }
186
187 1
    public function offsetUnset($offset)
188
    {
189 1
        unset($this->attributes[$offset]);
190
    }
191
192 2
    public function __get($name)
193
    {
194 2
        return $this->attributes[$name] ?? null;
195
    }
196
197 1
    public function __set($name, $value)
198
    {
199 1
        $this->attribute($name, $value);
200
    }
201
202 1
    public function __isset($name)
203
    {
204 1
        return isset($this->attributes[$name]);
205
    }
206
207 1
    public function __unset($name)
208
    {
209 1
        unset($this->attributes[$name]);
210
    }
211
}
212