Logger   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 16
dl 0
loc 56
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A saveLog() 0 4 1
A getManager() 0 3 1
A __construct() 0 2 1
A log() 0 14 3
A savePendingLogs() 0 7 2
A getDoctrine() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the XiideaEasyAuditBundle package.
5
 *
6
 * (c) Xiidea <http://www.xiidea.net>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Xiidea\EasyAuditBundle\Logger;
13
14
use Doctrine\Persistence\ManagerRegistry;
15
use Doctrine\Persistence\ObjectManager;
16
use Xiidea\EasyAuditBundle\Model\BaseAuditLog as AuditLog;
17
use Xiidea\EasyAuditBundle\Events\DoctrineEvents;
18
19
class Logger implements LoggerInterface
20
{
21
    private $entityDeleteLogs = [];
22
23
    public function __construct(private ManagerRegistry $doctrine)
24
    {
25
    }
26
27
    #[\Override]
28
    public function log(AuditLog $event = null)
29
    {
30
        if (empty($event)) {
31
            return;
32
        }
33
34
        if (DoctrineEvents::ENTITY_DELETED === $event->getTypeId()) {
35
            $this->entityDeleteLogs[] = $event;
36
37
            return;
38
        }
39
40
        $this->saveLog($event);
41
    }
42
43
    /**
44
     * @return ObjectManager
45
     */
46
    protected function getManager()
47
    {
48
        return $this->getDoctrine()->getManager();
49
    }
50
51
    /**
52
     * @return ManagerRegistry
53
     */
54
    public function getDoctrine()
55
    {
56
        return $this->doctrine;
57
    }
58
59
    /**
60
     * @param AuditLog $event
61
     */
62
    protected function saveLog(AuditLog $event)
63
    {
64
        $this->getManager()->persist($event);
65
        $this->getManager()->flush($event);
0 ignored issues
show
Unused Code introduced by
The call to Doctrine\Persistence\ObjectManager::flush() has too many arguments starting with $event. ( Ignorable by Annotation )

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

65
        $this->getManager()->/** @scrutinizer ignore-call */ flush($event);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
66
    }
67
68
    public function savePendingLogs()
69
    {
70
        foreach ($this->entityDeleteLogs as $log) {
71
            $this->saveLog($log);
72
        }
73
74
        $this->entityDeleteLogs = [];
75
    }
76
}
77