Notification::doNotSanitize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Usernotnull\Toast;
6
7
class Notification
8
{
9
    protected bool $sanitize;
10
11
    protected string $message;
12
13
    protected ?string $title;
14
15
    protected ?string $type;
16
17
    public function __construct(
18
        string $message,
19
        ?string $title,
20
        ?string $type = null
21
    ) {
22
        $this->type = $type;
23
        $this->title = $title;
24
        $this->message = $message;
25
        $this->sanitize = $type !== NotificationType::$debug;
26
    }
27
28
    protected function asArray(): array
29
    {
30
        $message = $this->sanitize ? htmlspecialchars($this->message, ENT_QUOTES) : $this->message;
31
        $title = $this->sanitize && $this->title ? htmlspecialchars($this->title, ENT_QUOTES) : $this->title;
32
        $type = $this->type ?? NotificationType::$info;
33
34
        return compact('message', 'title', 'type');
35
    }
36
37
    public function doNotSanitize(): Notification
38
    {
39
        $this->sanitize = false;
40
41
        return $this;
42
    }
43
44
    public static function make(
45
        string $message,
46
        ?string $title,
47
        ?string $type = null
48
    ): array {
49
        return (new static($message, $title, $type))->asArray();
50
    }
51
52
    public function push(): void
53
    {
54
        session()->push(config('tall-toasts.session_keys.toasts'), $this->asArray());
55
    }
56
57
    public function pushOnNextPage(): void
58
    {
59
        session()->push(config('tall-toasts.session_keys.toasts_next_page'), $this->asArray());
60
    }
61
}
62