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

FileLogHandler::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\Log;
26
27
28
use DI\Container;
29
use SP\Core\Events\Event;
30
use SP\Core\Events\EventReceiver;
31
use SP\Core\Language;
32
use SP\Providers\EventsTrait;
33
use SP\Providers\Provider;
34
use SplSubject;
35
36
/**
37
 * Class FileLogHandler
38
 *
39
 * @package SP\Providers\Log
40
 */
41
final class FileLogHandler extends Provider implements EventReceiver
42
{
43
    use EventsTrait;
44
45
    const EVENTS = [
46
        'database.'
47
    ];
48
49
    const MESSAGE_FORMAT = '%s;%s';
50
51
    /**
52
     * @var string
53
     */
54
    private $events;
55
    /**
56
     * @var Language
57
     */
58
    private $language;
59
60
    /**
61
     * Devuelve los eventos que implementa el observador
62
     *
63
     * @return array
64
     */
65
    public function getEvents()
66
    {
67
        return self::EVENTS;
68
    }
69
70
    /**
71
     * Devuelve los eventos que implementa el observador en formato cadena
72
     *
73
     * @return string
74
     */
75
    public function getEventsString()
76
    {
77
        return $this->events;
78
    }
79
80
    /**
81
     * Receive update from subject
82
     *
83
     * @link  http://php.net/manual/en/splobserver.update.php
84
     *
85
     * @param SplSubject $subject <p>
86
     *                            The <b>SplSubject</b> notifying the observer of an update.
87
     *                            </p>
88
     *
89
     * @return void
90
     * @since 5.1.0
91
     */
92
    public function update(SplSubject $subject)
93
    {
94
        $this->updateEvent('update', new Event($subject));
95
    }
96
97
    /**
98
     * Evento de actualización
99
     *
100
     * @param string $eventType Nombre del evento
101
     * @param Event  $event     Objeto del evento
102
     */
103
    public function updateEvent($eventType, Event $event)
104
    {
105
        $this->language->setAppLocales();
106
107
        if (($e = $event->getSource()) instanceof \Exception) {
108
            logger(sprintf(self::MESSAGE_FORMAT, $eventType, __($e->getMessage())));
109
        } elseif (($eventMessage = $event->getEventMessage()) !== null) {
110
            logger(sprintf(self::MESSAGE_FORMAT, $eventType, $eventMessage->composeText(';')));
111
        }
112
113
        $this->language->unsetAppLocales();
114
    }
115
116
    /**
117
     * @param Container $dic
118
     *
119
     * @throws \DI\DependencyException
120
     * @throws \DI\NotFoundException
121
     */
122
    protected function initialize(Container $dic)
123
    {
124
        $this->language = $dic->get(Language::class);
125
        $this->events = $this->parseEventsToRegex(self::EVENTS);
126
    }
127
}