Passed
Pull Request — master (#44)
by Martin
04:50
created

Logger::getManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
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\Common\Persistence\ManagerRegistry;
15
use Xiidea\EasyAuditBundle\Events\DoctrineEvents;
16
use Xiidea\EasyAuditBundle\Model\BaseAuditLog as AuditLog;
17
18
class Logger implements LoggerInterface
19
{
20
    private $entityDeleteLogs = [];
21
22
    /**
23
     * @var \Doctrine\Common\Persistence\ManagerRegistry
24
     */
25
    private $doctrine;
26
27
    public function __construct(ManagerRegistry $doctrine)
28
    {
29
        $this->doctrine = $doctrine;
30
    }
31
32
    public function log(AuditLog $event = null)
33
    {
34
        if (empty($event)) {
35
            return;
36
        }
37
38
        if (DoctrineEvents::ENTITY_DELETED === $event->getTypeId()) {
39
            $this->entityDeleteLogs[] = $event;
40
41
            return;
42
        }
43
44
        $this->saveLog($event);
45
    }
46
47
    /**
48
     * @param AuditLog $event
49
     */
50
    protected function saveLog(AuditLog $event)
51
    {
52
        $manager = $this->doctrine->getManagerForClass($event);
0 ignored issues
show
Bug introduced by
$event of type Xiidea\EasyAuditBundle\Model\BaseAuditLog is incompatible with the type string expected by parameter $class of Doctrine\Common\Persiste...y::getManagerForClass(). ( Ignorable by Annotation )

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

52
        $manager = $this->doctrine->getManagerForClass(/** @scrutinizer ignore-type */ $event);
Loading history...
53
        $manager->persist($event);
54
        $manager->flush($event);
55
    }
56
57
    public function savePendingLogs()
58
    {
59
        foreach ($this->entityDeleteLogs as $log) {
60
            $this->saveLog($log);
61
        }
62
63
        $this->entityDeleteLogs = [];
64
    }
65
}
66