Completed
Push — master ( 4cc80e...c361a4 )
by De Cramer
13s
created

NotificationUpdater::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 4
cp 0
cc 1
eloc 9
nc 1
nop 6
crap 2
1
<?php
2
3
namespace eXpansion\Framework\Notifications\Plugins\Gui;
4
5
use eXpansion\Framework\Core\Helpers\ChatNotification;
6
use eXpansion\Framework\Core\Model\Gui\WidgetFactoryContext;
7
use eXpansion\Framework\Core\Model\UserGroups\Group;
8
use eXpansion\Framework\Core\Plugins\Gui\ScriptVariableUpdateFactory;
9
use FML\Script\Builder;
10
11
class NotificationUpdater extends ScriptVariableUpdateFactory
12
{
13
    /**
14
     * @var ChatNotification
15
     */
16
    private $chatNotification;
17
18
    /**
19
     * NotificationUpdater constructor.
20
     * @param                      $name
21
     * @param array                $variables
22
     * @param int                  $maxUpdateFrequency
23
     * @param Group                $playerGroup
24
     * @param WidgetFactoryContext $context
25
     * @param ChatNotification     $chatNotification
26
     */
27
    public function __construct(
28
        $name,
29
        array $variables,
30
        int $maxUpdateFrequency = 1,
31
        Group $playerGroup,
32
        WidgetFactoryContext $context,
33
        ChatNotification $chatNotification
34
    ) {
35
        parent::__construct($name, $variables, $maxUpdateFrequency, $playerGroup, $context);
36
        $this->chatNotification = $chatNotification;
37
    }
38
39
40
    /**
41
     * Update
42
     *
43
     * @param string $prefix
44
     * @param string $title title of the notification
45
     * @param string $message message part of notification
46
     * @param string $params parameters for multilingual string
47
     * @param int    $timeout timeout in milliseconds
48
     * @param Group  $group
49
     */
50
    public function setNotification($prefix, $title, $message, $params, $timeout, $group)
51
    {
52
53
        $toast = [
54
            "title" => $this->getTranslations($title, $params),
55
            "message" => $this->getTranslations($message, $params),
56
            "params" => ["prefix" => $prefix, "duration" => "".$timeout],
57
        ];
58
59
60
        $this->updateValue('notification', Builder::getArray($toast, true));
61
        $this->update($group);
62
    }
63
64
65
    /**
66
     * Generates the needed data structure
67
     * @param $string
68
     * @param $params
69
     * @return array
70
     */
71
    private function getTranslations($string, $params)
72
    {
73
        $out = [];
74
        $messages = $this->translationsHelper->getTranslations($string, $params);
75
        foreach ($messages as $message) {
76
            $out[$message['Lang']] = $message['Text'];
77
        }
78
79
        return $out;
80
    }
81
}
82