Notification   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 18
c 2
b 0
f 0
dl 0
loc 53
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A doNotSanitize() 0 5 1
A pushOnNextPage() 0 3 1
A asArray() 0 7 4
A make() 0 6 1
A __construct() 0 9 1
A push() 0 3 1
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