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

Notification   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 43
rs 10
c 1
b 0
f 0
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 7 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
    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