Passed
Push — main ( a4510f...0b0f71 )
by John
05:30 queued 01:52
created

Notification::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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