Passed
Push — master ( f98a16...362693 )
by Julien
06:52 queued 02:00
created

Logger::beforeQuery()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 44.7465

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 7
eloc 20
c 2
b 1
f 0
nc 4
nop 2
dl 0
loc 32
ccs 2
cts 24
cp 0.0833
crap 44.7465
rs 8.6666
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Db\Events;
13
14
use Phalcon\Db\Adapter\AbstractAdapter;
15
use Phalcon\Events\EventInterface;
16
use Phalcon\Mvc\ModelInterface;
17
use Zemit\Di\Injectable;
18
use Zemit\Models\Interfaces\SessionInterface;
19
20
/**
21
 * @todo review
22
 */
23
class Logger extends Injectable
24
{
25
    public bool $inProgress = false;
26
    
27 4
    public function beforeQuery(EventInterface $event, AbstractAdapter $connection): void
28
    {
29 4
        if ($this->config->path('logger.enable') || $this->config->path('app.logger')) {
30
            if ($this->config->path('logger.logDatabaseQuery')) {
31
                if (!$this->inProgress) {
32
                    
33
                    // deactivate logger
34
                    $this->inProgress = true;
35
                    $sessionId = $this->identity->getSession()?->getId();
36
                    $userId = $this->identity->getUserId() ?: null;
37
                    $userAsId = $this->identity->getUserAsId() ?: null;
38
                    
39
                    $log = json_encode([
40
                        'type' => 'query',
41
                        'sessionId' => $sessionId,
42
                        'userId' => $userId,
43
                        'userAsId' => $userAsId,
44
                        'event' => [
45
                            'type' => $event->getType(),
46
                            'data' => $event->getData(),
47
                        ],
48
                        'meta' => [
49
//                            'identity' => $this->identity->getIdentity(),
50
                            'sqlStatement' => $connection->getSQLStatement(),
51
                            'sqlVariables' => $connection->getSQLVariables(),
52
                        ],
53
                    ]);
54
                    
55
                    $this->logger->info($log);
56
                    
57
                    // reactivate logger
58
                    $this->inProgress = false;
59
                }
60
            }
61
        }
62
    }
63
}
64