Passed
Push — main ( d45bb9...aef137 )
by John
03:56 queued 12s
created

Notification   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 37
rs 10
c 1
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A asArray() 0 6 1
A make() 0 6 1
A __construct() 0 8 2
A push() 0 3 1
A pushOnNextPage() 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
    ) {
14
        $this->message = htmlspecialchars($message, ENT_QUOTES);
15
        $this->title = $title ? htmlspecialchars($title, ENT_QUOTES) : '';
16
        $this->type = $type ?? NotificationType::$info;
17
    }
18
19
    protected function asArray(): array
20
    {
21
        return [
22
            'message' => $this->message,
23
            'title' => $this->title ?? '',
24
            'type' => $this->type,
25
        ];
26
    }
27
28
    public static function make(
29
        string $message,
30
        ?string $title,
31
        ?string $type = null
32
    ): array {
33
        return (new static($message, $title, $type))->asArray();
34
    }
35
36
    public function push(): void
37
    {
38
        session()->push(config('tall-toasts.session_keys.toasts'), $this->asArray());
39
    }
40
41
    public function pushOnNextPage(): void
42
    {
43
        session()->push(config('tall-toasts.session_keys.toasts_next_page'), $this->asArray());
44
    }
45
}
46