AbstractNotifier::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Yoeunes\Notify\Notifiers;
4
5
use function basename;
6
7
abstract class AbstractNotifier
8
{
9
    /** @var array */
10
    protected $config;
11
12
    /**
13
     * Toastr constructor.
14
     *
15
     * @param array $config
16
     */
17
    public function __construct(array $config)
18
    {
19
        $this->config = $config;
20
    }
21
22
    /**
23
     * Render the notifications' script tag.
24
     *
25
     * @param array $notifications
26
     *
27
     * @return string
28
     */
29
    public function render(array $notifications): string
30
    {
31
        return '<script type="text/javascript">'.$this->options().$this->notificationsAsString($notifications).'</script>';
32
    }
33
34
    /**
35
     * Get global toastr options.
36
     *
37
     * @return string
38
     */
39
    public function options(): string
40
    {
41
        return '';
42
    }
43
44
    /**
45
     * @param array $notifications
46
     *
47
     * @return string
48
     */
49
    public function notificationsAsString(array $notifications): string
50
    {
51
        return implode('', $this->notifications($notifications));
52
    }
53
54
    /**
55
     * map over all notifications and create an array of toastrs.
56
     *
57
     * @param array $notifications
58
     *
59
     * @return array
60
     */
61
    public function notifications(array $notifications): array
62
    {
63
        return array_map(
64
            function ($n) {
65
                return $this->notify($n['type'], $n['message'], $n['title'], $n['options']);
66
            },
67
            $notifications
68
        );
69
    }
70
71
    /**
72
     * Create a single notification.
73
     *
74
     * @param string $type
75
     * @param string $message
76
     * @param string|null $title
77
     * @param string|null $options
78
     *
79
     * @return string
80
     */
81
    abstract public function notify(string $type, string $message = '', string $title = '', string $options = ''): string;
82
83
    /**
84
     * Get Allowed Types
85
     *
86
     * @return array
87
     */
88
    public function getAllowedTypes(): array
89
    {
90
        return $this->config['types'];
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function getName(): string
97
    {
98
        return basename(\get_class($this));
99
    }
100
}
101