Passed
Push — 3.0 ( 8cdcee...90ba03 )
by Rubén
07:01
created

MailHandler::getEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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\Core\Messages\TextFormatter;
32
use SP\Http\Request;
33
use SP\Providers\EventsTrait;
34
use SP\Providers\Provider;
35
use SP\Services\Mail\MailService;
36
use SplSubject;
37
38
/**
39
 * Class MailHandler
40
 *
41
 * @package SP\Providers\Mail
42
 */
43
final class MailHandler extends Provider implements EventReceiver
44
{
45
    use EventsTrait;
46
47
    const EVENTS = [
48
        'create.',
49
        'delete.',
50
        'edit.',
51
        'save.',
52
        'clear.eventlog',
53
        'refresh.masterPassword',
54
        'update.masterPassword.end',
55
        'import.ldap.end',
56
        'run.backup.end',
57
        'run.import.end',
58
        'request.account'
59
    ];
60
61
    /**
62
     * @var \SP\Services\Mail\MailService
63
     */
64
    private $mailService;
65
    /**
66
     * @var string
67
     */
68
    private $events;
69
    /**
70
     * @var Request
71
     */
72
    private $request;
73
74
    /**
75
     * Devuelve los eventos que implementa el observador
76
     *
77
     * @return array
78
     */
79
    public function getEvents()
80
    {
81
        return self::EVENTS;
82
    }
83
84
    /**
85
     * Devuelve los eventos que implementa el observador en formato cadena
86
     *
87
     * @return string
88
     */
89
    public function getEventsString()
90
    {
91
        return $this->events;
92
    }
93
94
    /**
95
     * Receive update from subject
96
     *
97
     * @link  http://php.net/manual/en/splobserver.update.php
98
     *
99
     * @param SplSubject $subject <p>
100
     *                            The <b>SplSubject</b> notifying the observer of an update.
101
     *                            </p>
102
     *
103
     * @return void
104
     * @since 5.1.0
105
     */
106
    public function update(SplSubject $subject)
107
    {
108
        $this->updateEvent('update', new Event($subject));
109
    }
110
111
    /**
112
     * Evento de actualización
113
     *
114
     * @param string $eventType Nombre del evento
115
     * @param Event  $event     Objeto del evento
116
     */
117
    public function updateEvent($eventType, Event $event)
118
    {
119
        if (($eventMessage = $event->getEventMessage()) !== null) {
120
            try {
121
                $configData = $this->config->getConfigData();
122
                $userData = $this->context->getUserData();
123
124
                $mailMessage = new MailMessage();
125
126
                if ($eventMessage->getDescriptionCounter() === 0
127
                    && $eventMessage->getDetailsCounter() === 0
128
                ) {
129
                    $mailMessage->addDescription(sprintf(__('Event: %s'), $eventType));
130
                } else {
131
                    $mailMessage->addDescription($eventMessage->composeText('<br>'));
132
                }
133
134
                $mailMessage->addDescriptionLine();
135
                $mailMessage->addDescription(sprintf(__('Performed by: %s (%s)'), $userData->getName(), $userData->getLogin()));
136
                $mailMessage->addDescription(sprintf(__('IP Address: %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; 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

136
                $mailMessage->addDescription(sprintf(__('IP Address: %s'), /** @scrutinizer ignore-type */ $this->request->getClientAddress(true)));
Loading history...
137
138
                $subject = $eventMessage->getDescription(new TextFormatter(), true) ?: $eventType;
139
140
                $this->mailService->send(
141
                    $subject,
142
                    $configData->getMailRecipients(),
143
                    $mailMessage
144
                );
145
            } catch (\Exception $e) {
146
                processException($e);
147
            }
148
        }
149
    }
150
151
    /**
152
     * @param Container $dic
153
     *
154
     * @throws \DI\DependencyException
155
     * @throws \DI\NotFoundException
156
     */
157
    protected function initialize(Container $dic)
158
    {
159
        $this->mailService = $dic->get(MailService::class);
160
        $this->request = $dic->get(Request::class);
161
162
        $configEvents = $this->config->getConfigData()->getMailEvents();
163
164
        if (count($configEvents) === 0) {
165
            $this->events = $this->parseEventsToRegex(self::EVENTS);
166
        } else {
167
            $this->events = $this->parseEventsToRegex($configEvents);
168
        }
169
    }
170
}