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

MailHandler::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\Mail;
26
27
use DI\Container;
28
use SP\Core\Events\Event;
29
use SP\Core\Events\EventReceiver;
30
use SP\Core\Messages\MailMessage;
31
use SP\Http\Request;
32
use SP\Providers\EventsTrait;
33
use SP\Providers\Provider;
34
use SP\Services\Mail\MailService;
35
use SplSubject;
36
37
/**
38
 * Class MailHandler
39
 *
40
 * @package SP\Providers\Mail
41
 */
42
final class MailHandler extends Provider implements EventReceiver
43
{
44
    use EventsTrait;
45
46
    const EVENTS = [
47
        'create.',
48
        'delete.',
49
        'edit.',
50
        'save.',
51
        'clear.eventlog',
52
        'refresh.masterPassword',
53
        'update.masterPassword.end',
54
        'import.ldap.end',
55
        'run.backup.end',
56
        'run.import.end'
57
    ];
58
59
    /**
60
     * @var \SP\Services\Mail\MailService
61
     */
62
    private $mailService;
63
    /**
64
     * @var string
65
     */
66
    private $events;
67
    /**
68
     * @var Request
69
     */
70
    private $request;
71
72
    /**
73
     * Evento de actualización
74
     *
75
     * @param string $eventType Nombre del evento
76
     * @param Event  $event     Objeto del evento
77
     */
78
    public function updateEvent($eventType, Event $event)
79
    {
80
        if (($eventMessage = $event->getEventMessage()) !== null) {
81
            try {
82
                $configData = $this->config->getConfigData();
83
                $userData = $this->context->getUserData();
84
85
                $mailMessage = new MailMessage();
86
                $mailMessage->addDescription($eventMessage->composeText());
87
                $mailMessage->addDescription(sprintf(__('Realizado por: %s (%s)'), $userData->getName(), $userData->getLogin()));
88
                $mailMessage->addDescription(sprintf(__('Dirección IP: %s'), $this->request->getClientAddress(true)));
0 ignored issues
show
Bug introduced by
It seems like $this->request->getClientAddress(true) can also be of type array<mixed,array> and array; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
                $mailMessage->addDescription(sprintf(__('Dirección IP: %s'), /** @scrutinizer ignore-type */ $this->request->getClientAddress(true)));
Loading history...
89
90
                $this->mailService->send($eventMessage->getDescription(), $configData->getMailFrom(), $mailMessage);
91
            } catch (\Exception $e) {
92
                processException($e);
93
            }
94
        }
95
    }
96
97
    /**
98
     * Devuelve los eventos que implementa el observador
99
     *
100
     * @return array
101
     */
102
    public function getEvents()
103
    {
104
        return self::EVENTS;
105
    }
106
107
    /**
108
     * Devuelve los eventos que implementa el observador en formato cadena
109
     *
110
     * @return string
111
     */
112
    public function getEventsString()
113
    {
114
        return $this->events;
115
    }
116
117
    /**
118
     * Receive update from subject
119
     *
120
     * @link  http://php.net/manual/en/splobserver.update.php
121
     *
122
     * @param SplSubject $subject <p>
123
     *                            The <b>SplSubject</b> notifying the observer of an update.
124
     *                            </p>
125
     *
126
     * @return void
127
     * @since 5.1.0
128
     */
129
    public function update(SplSubject $subject)
130
    {
131
        $this->updateEvent('update', new Event($subject));
132
    }
133
134
    /**
135
     * @param Container $dic
136
     *
137
     * @throws \DI\DependencyException
138
     * @throws \DI\NotFoundException
139
     */
140
    protected function initialize(Container $dic)
141
    {
142
        $this->mailService = $dic->get(MailService::class);
143
        $this->request = $dic->get(Request::class);
144
145
        $configEvents = $this->config->getConfigData()->getMailEvents();
146
147
        if (count($configEvents) === 0) {
148
            $this->events = $this->parseEventsToRegex(self::EVENTS);
149
        } else {
150
            $this->events = $this->parseEventsToRegex($configEvents);
151
        }
152
    }
153
}