Passed
Push — master ( 004f45...b6a6d3 )
by Damien
03:00
created

AuditSubscriber::preSoftDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace DH\DoctrineAuditBundle\EventSubscriber;
4
5
use DH\DoctrineAuditBundle\AuditManager;
6
use DH\DoctrineAuditBundle\DBAL\AuditLogger;
7
use DH\DoctrineAuditBundle\DBAL\AuditLoggerChain;
8
use Doctrine\Common\EventSubscriber;
9
use Doctrine\DBAL\Logging\SQLLogger;
10
use Doctrine\ORM\Event\OnFlushEventArgs;
11
use Doctrine\ORM\Events;
12
13
class AuditSubscriber implements EventSubscriber
14
{
15
    /**
16
     * @var AuditManager
17
     */
18
    private $manager;
19
20
    /**
21
     * @var ?SQLLogger
22
     */
23
    private $loggerBackup;
24
25
    public function __construct(AuditManager $manager)
26
    {
27
        $this->manager = $manager;
28
    }
29
30
    /**
31
     * It is called inside EntityManager#flush() after the changes to all the managed entities
32
     * and their associations have been computed.
33
     *
34
     * @see https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#onflush
35
     *
36
     * @param OnFlushEventArgs $args
37
     *
38
     * @throws \Doctrine\DBAL\DBALException
39
     * @throws \Doctrine\ORM\Mapping\MappingException
40
     */
41
    public function onFlush(OnFlushEventArgs $args): void
42
    {
43
        $em = $args->getEntityManager();
44
        $uow = $em->getUnitOfWork();
45
46
        // extend the SQL logger
47
        $this->loggerBackup = $em->getConnection()->getConfiguration()->getSQLLogger();
48
        $auditLogger = new AuditLogger(function () use ($em) {
49
            // flushes pending data
50
            $em->getConnection()->getConfiguration()->setSQLLogger($this->loggerBackup);
51
            $uow = $em->getUnitOfWork();
52
53
            $this->manager->processInsertions($em, $uow);
54
            $this->manager->processUpdates($em, $uow);
55
            $this->manager->processAssociations($em);
56
            $this->manager->processDissociations($em);
57
            $this->manager->processDeletions($em);
58
59
            $this->manager->reset();
60
        });
61
62
        // Initialize a new LoggerChain with the new AuditLogger + the existing SQLLoggers.
63
        $loggerChain = new AuditLoggerChain();
64
        $loggerChain->addLogger($auditLogger);
65
        if ($this->loggerBackup instanceof AuditLoggerChain) {
66
            /** @var SQLLogger $logger */
67
            foreach ($this->loggerBackup->getLoggers() as $logger) {
68
                $loggerChain->addLogger($logger);
69
            }
70
        } elseif ($this->loggerBackup instanceof SQLLogger) {
71
            $loggerChain->addLogger($this->loggerBackup);
72
        }
73
        $em->getConnection()->getConfiguration()->setSQLLogger($loggerChain);
74
75
        $this->manager->collectScheduledInsertions($uow);
76
        $this->manager->collectScheduledUpdates($uow);
77
        $this->manager->collectScheduledDeletions($uow, $em);
78
        $this->manager->collectScheduledCollectionUpdates($uow, $em);
79
        $this->manager->collectScheduledCollectionDeletions($uow, $em);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function getSubscribedEvents(): array
86
    {
87
        return [Events::onFlush];
88
    }
89
}
90