Passed
Push — devel-3.0 ( f3e30e...950ad4 )
by Rubén
03:36
created

NotificationHandler::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Providers\Notification;
26
27
use DI\Container;
28
use SP\Core\Events\Event;
29
use SP\Core\Events\EventReceiver;
30
use SP\DataModel\NotificationData;
31
use SP\Providers\EventsTrait;
32
use SP\Providers\Provider;
33
use SP\Services\Notification\NotificationService;
34
use SplSubject;
35
36
/**
37
 * Class NotificationHandler
38
 *
39
 * @package SP\Providers\Notification
40
 */
41
final class NotificationHandler extends Provider implements EventReceiver
42
{
43
    use EventsTrait;
44
45
    const EVENTS = [
46
        'request.account',
47
        'show.account.link'
48
    ];
49
50
    /**
51
     * @var NotificationService
52
     */
53
    private $notificationService;
54
    /**
55
     * @var string
56
     */
57
    private $events;
58
59
    /**
60
     * Devuelve los eventos que implementa el observador
61
     *
62
     * @return array
63
     */
64
    public function getEvents()
65
    {
66
        return self::EVENTS;
67
    }
68
69
    /**
70
     * Devuelve los eventos que implementa el observador en formato cadena
71
     *
72
     * @return string
73
     */
74
    public function getEventsString()
75
    {
76
        return $this->events;
77
    }
78
79
    /**
80
     * Receive update from subject
81
     *
82
     * @link  http://php.net/manual/en/splobserver.update.php
83
     *
84
     * @param SplSubject $subject <p>
85
     *                            The <b>SplSubject</b> notifying the observer of an update.
86
     *                            </p>
87
     *
88
     * @return void
89
     * @since 5.1.0
90
     */
91
    public function update(SplSubject $subject)
92
    {
93
        $this->updateEvent('update', new Event($subject));
94
    }
95
96
    /**
97
     * Evento de actualización
98
     *
99
     * @param string $eventType Nombre del evento
100
     * @param Event  $event     Objeto del evento
101
     */
102
    public function updateEvent($eventType, Event $event)
103
    {
104
        switch ($eventType) {
105
            case 'request.account':
106
                $this->requestAccountNotification($event);
107
                break;
108
            case 'show.account.link':
109
                $this->showAccountLinkNotification($event);
110
                break;
111
        }
112
    }
113
114
    /**
115
     * @param Event $event
116
     */
117
    private function requestAccountNotification(Event $event)
118
    {
119
        $eventMessage = $event->getEventMessage();
120
        $data = $eventMessage->getData();
121
122
        foreach ($data['userId'] as $userId) {
123
            $notificationData = new NotificationData();
124
            $notificationData->setType(__('Solicitud'));
125
            $notificationData->setComponent(__('Cuentas'));
126
            $notificationData->setUserId($userId);
127
            $notificationData->setDescription($eventMessage);
128
129
            $this->notify($notificationData);
130
        }
131
    }
132
133
    /**
134
     * @param NotificationData $notificationData
135
     */
136
    private function notify(NotificationData $notificationData)
137
    {
138
        try {
139
            $this->notificationService->create($notificationData);
140
        } catch (\Exception $e) {
141
            processException($e);
142
        }
143
    }
144
145
    /**
146
     * @param Event $event
147
     */
148
    private function showAccountLinkNotification(Event $event)
149
    {
150
        $eventMessage = $event->getEventMessage();
151
        $data = $eventMessage->getData();
152
153
        if ($data['notify'] === true) {
154
            $notificationData = new NotificationData();
155
            $notificationData->setType(__('Notificación'));
156
            $notificationData->setComponent(__('Cuentas'));
157
            $notificationData->setUserId($data['userId']);
158
            $notificationData->setDescription($eventMessage);
159
160
            $this->notify($notificationData);
161
        }
162
    }
163
164
    /**
165
     * @param Container $dic
166
     *
167
     * @throws \DI\DependencyException
168
     * @throws \DI\NotFoundException
169
     */
170
    protected function initialize(Container $dic)
171
    {
172
        $this->notificationService = $dic->get(NotificationService::class);
173
174
        $this->events = $this->parseEventsToRegex(self::EVENTS);
175
    }
176
}