Passed
Push — 3.0 ( e37bc1...f6f9cd )
by Rubén
04:03
created

DatabaseLogHandler::updateEvent()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 22
nc 10
nop 2
dl 0
loc 34
rs 8.9457
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\Log;
26
27
use DI\Container;
28
use SP\Core\Events\Event;
29
use SP\Core\Events\EventReceiver;
30
use SP\Core\Exceptions\SPException;
31
use SP\Core\Language;
32
use SP\DataModel\EventlogData;
33
use SP\Providers\EventsTrait;
34
use SP\Providers\Provider;
35
use SP\Services\EventLog\EventlogService;
36
use SplSubject;
37
38
/**
39
 * Class LogHandler
40
 *
41
 * @package SP\Providers\Log
42
 */
43
final class DatabaseLogHandler extends Provider implements EventReceiver
44
{
45
    use EventsTrait;
46
47
    /**
48
     * @var EventlogService
49
     */
50
    private $eventlogService;
51
    /**
52
     * @var string
53
     */
54
    private $events;
55
    /**
56
     * @var Language
57
     */
58
    private $language;
59
60
    /**
61
     * Receive update from subject
62
     *
63
     * @link  http://php.net/manual/en/splobserver.update.php
64
     *
65
     * @param SplSubject $subject <p>
66
     *                            The <b>SplSubject</b> notifying the observer of an update.
67
     *                            </p>
68
     *
69
     * @return void
70
     * @throws \SP\Core\Exceptions\InvalidClassException
71
     * @since 5.1.0
72
     */
73
    public function update(SplSubject $subject)
74
    {
75
        $this->updateEvent('update', new Event($subject));
76
    }
77
78
    /**
79
     * Evento de actualización
80
     *
81
     * @param string $eventType Nombre del evento
82
     * @param Event  $event     Objeto del evento
83
     *
84
     * @throws \SP\Core\Exceptions\InvalidClassException
85
     */
86
    public function updateEvent($eventType, Event $event)
87
    {
88
        $this->language->setAppLocales();
89
90
        $eventlogData = new EventlogData();
91
        $eventlogData->setAction($eventType);
92
        $eventlogData->setLevel('INFO');
93
94
        $source = $event->getSource();
95
96
        if ($source instanceof SPException) {
97
            $eventlogData->setLevel('ERROR');
98
99
            $hint = $source->getHint();
100
101
            if ($hint !== null) {
0 ignored issues
show
introduced by
The condition $hint !== null is always true.
Loading history...
102
                $eventlogData->setDescription(__($source->getMessage()) . PHP_EOL . $hint);
103
            } else {
104
                $eventlogData->setDescription(__($source->getMessage()));
105
            }
106
        } elseif ($source instanceof \Exception) {
107
            $eventlogData->setLevel('ERROR');
108
            $eventlogData->setDescription(__($source->getMessage()));
109
        } elseif (($eventMessage = $event->getEventMessage()) !== null) {
110
            $eventlogData->setDescription($eventMessage->composeText());
111
        }
112
113
        try {
114
            $this->eventlogService->create($eventlogData);
115
        } catch (\Exception $e) {
116
            processException($e);
117
        }
118
119
        $this->language->unsetAppLocales();
120
    }
121
122
    /**
123
     * Devuelve los eventos que implementa el observador en formato cadena
124
     *
125
     * @return string
126
     */
127
    public function getEventsString()
128
    {
129
        return $this->events;
130
    }
131
132
    /**
133
     * Devuelve los eventos que implementa el observador
134
     *
135
     * @return array
136
     */
137
    public function getEvents()
138
    {
139
        return LogInterface::EVENTS;
140
    }
141
142
    /**
143
     * @param Container $dic
144
     *
145
     * @throws \DI\DependencyException
146
     * @throws \DI\NotFoundException
147
     */
148
    protected function initialize(Container $dic)
149
    {
150
        $this->language = $dic->get(Language::class);
151
        $this->eventlogService = $dic->get(EventlogService::class);
152
153
        $configEvents = $this->config->getConfigData()->getLogEvents();
154
155
        if (count($configEvents) === 0) {
156
            $this->events = $this->parseEventsToRegex(LogInterface::EVENTS);
157
        } else {
158
            $this->events = $this->parseEventsToRegex($configEvents);
159
        }
160
    }
161
}