Completed
Pull Request — master (#34)
by
unknown
09:45
created

Logger::saveLog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 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 Xiidea\EasyAuditBundle\Document\BaseAuditLog as AuditLog;
15
use Doctrine\Bundle\MongoDBBundle\ManagerRegistry;
16
use Doctrine\Common\Persistence\ObjectManager;
17
use Xiidea\EasyAuditBundle\Events\DoctrineEvents;
18
19
class Logger implements LoggerInterface
20
{
21
    private $documentDeleteLogs = [];
22
23
    /**
24
     * @var ManagerRegistry
25
     */
26
    private $doctrine;
27
28
    /**
29
     * Logger constructor.
30
     * @param ManagerRegistry $doctrine
31
     */
32
    public function __construct(ManagerRegistry $doctrine)
33
    {
34
        $this->doctrine = $doctrine;
35
    }
36
37
    public function log(AuditLog $event = null)
38
    {
39
        if ($event === null) {
40
            return;
41
        }
42
43
        if ($event->getTypeId() === DoctrineEvents::ENTITY_DELETED) {
44
            $this->documentDeleteLogs[] = $event;
45
46
            return;
47
        }
48
49
        $this->saveLog($event);
50
    }
51
52
    /**
53
     * @return ObjectManager
54
     */
55
    protected function getDocumentManager()
56
    {
57
        return $this->getDoctrine()->getManager();
58
    }
59
60
    /**
61
     * @return ManagerRegistry
62
     */
63
    public function getDoctrine()
64
    {
65
        return $this->doctrine;
66
    }
67
68
    /**
69
     * @param AuditLog $event
70
     */
71
    protected function saveLog(AuditLog $event)
72
    {
73
        $this->getDocumentManager()->persist($event);
74
        $this->getDocumentManager()->flush($event);
75
    }
76
77
    public function savePendingLogs()
78
    {
79
        foreach ($this->documentDeleteLogs as $log) {
80
            $this->saveLog($log);
81
        }
82
83
        $this->documentDeleteLogs = [];
84
    }
85
}
86