Notifications::warning()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 5
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: php_r
5
 * Date: 25.2.2018
6
 * Time: 21.50
7
 */
8
9
namespace eXpansion\Framework\Notifications\Services;
10
11
12
use eXpansion\Framework\Core\Model\UserGroups\Group;
13
use eXpansion\Framework\Core\Plugins\UserGroups\Factory as GroupFactory;
14
use eXpansion\Framework\Notifications\Plugins\Gui\NotificationUpdater;
15
use Psr\Log\LoggerInterface;
16
17
/**
18
 * Class Notifications
19
 * @package eXpansion\Framework\Notifications\Services
20
 */
21
class Notifications
22
{
23
    /**
24
     * @var NotificationUpdater
25
     */
26
    private $notificationsUpdater;
27
28
    /** @var Group */
29
    private $group;
30
    /**
31
     * @var LoggerInterface
32
     */
33
    private $logger;
34
    /**
35
     * @var GroupFactory
36
     */
37
    private $groupFactory;
38
39
    /**
40
     * Notifications constructor.
41
     * @param Group               $group
42
     * @param NotificationUpdater $notificationUpdater
43
     * @param GroupFactory        $groupFactory
44
     * @param LoggerInterface     $logger
45
     */
46
    public function __construct(
47
        $group,
48
        NotificationUpdater $notificationUpdater,
49
        GroupFactory $groupFactory,
50
        LoggerInterface $logger
51
    ) {
52
        $this->notificationsUpdater = $notificationUpdater;
53
        $this->group = $group;
54
        $this->logger = $logger;
55
        $this->groupFactory = $groupFactory;
56
    }
57
58
    /**
59
     * Sends notification as info level
60
     * Notifications are automatically logged
61
     *
62
     * @param string            $message Message of notification
63
     * @param array             $params replacements for translation, note: same params are used for title and message replacement
64
     * @param string            $title Title of the notification
65
     * @param int               $duration use 0 for permanent
66
     * @param string|Group|null $group login, group instance or null for everyone
67
     */
68
    public function info($message, $params = [], $title = "Info", $duration = 5500, $group = null)
69
    {
70
        $this->sendNotification('$3af ', $title, $message, $params, $duration, $group);
71
    }
72
73
    /**
74
     * Sends notification as info level
75
     * Notifications are automatically logged
76
     *
77
     * @param string            $message Message of notification
78
     * @param array             $params replacements for translation, note: same params are used for title and message replacement
79
     * @param string            $title Title of the notification
80
     * @param int               $duration use 0 for permanent
81
     * @param string|Group|null $group login, group instance or null for everyone
82
     */
83
84
    public function notice($message, $params = [], $title = "Notice", $duration = 5500, $group = null)
85
    {
86
        $this->sendNotification("", $title, $message, $params, $duration, $group);
87
    }
88
89
    /**
90
     * Sends notification as info level
91
     * Notifications are automatically logged
92
     *
93
     * @param string     $message Message of notification
94
     * @param array      $params replacements for translation, note: same params are used for title and message replacement
95
     * @param string     $title Title of the notification
96
     * @param int        $duration use 0 for permanent
97
     * @param Group|null $group
98
     */
99
100
    public function warning($message, $params = [], $title = "Warning", $duration = 8500, $group = null)
101
    {
102
103
        $this->sendNotification('$ff0 ', $title, $message, $params, $duration, $group);
104
    }
105
106
    /**
107
     * Sends notification as info level
108
     * Notifications are automatically logged
109
     *
110
     * @param string            $message Message of notification
111
     * @param array             $params replacements for translation, note: same params are used for title and message replacement
112
     * @param string            $title Title of the notification
113
     * @param int               $duration use 0 for permanent
114
     * @param string|Group|null $group login, group instance or null for everyone
115
     */
116
117
    public function error($message, $params = [], $title = "Error", $duration = 10500, $group = null)
118
    {
119
        $this->sendNotification('$f00 ', $title, $message, $params, $duration, $group);
120
    }
121
122
123
    /**
124
     * @param                    $prefix
125
     * @param                    $title
126
     * @param                    $message
127
     * @param                    $params
128
     * @param                    $duration
129
     * @param  string|Group|null $group
130
     */
131
    protected function sendNotification($prefix, $title, $message, $params, $duration, $group = null)
132
    {
133
        if ($group == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $group of type string|eXpansion\Framewo...l\UserGroups\Group|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
134
            $group = $this->group;
135
        }
136
137
        if (is_string($group)) {
138
            $group = $this->groupFactory->createForPlayer($group);
139
        }
140
141
        if (is_array($group)) {
142
            $group = $this->groupFactory->createForPlayers($group);
143
        }
144
145
        $this->notificationsUpdater->setNotification($prefix, $title, $message, $params, $duration, $group);
146
    }
147
148
149
}