Passed
Pull Request — master (#50)
by Damien
02:13
created

AuditSubscriber   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A onFlush() 0 33 2
A preSoftDelete() 0 3 1
A getSubscribedEvents() 0 3 1
1
<?php
2
3
namespace DH\DoctrineAuditBundle\EventSubscriber;
4
5
use DH\DoctrineAuditBundle\AuditManager;
6
use DH\DoctrineAuditBundle\DBAL\AuditLogger;
7
use Doctrine\Common\EventSubscriber;
8
use Doctrine\DBAL\Logging\LoggerChain;
9
use Doctrine\DBAL\Logging\SQLLogger;
10
use Doctrine\ORM\EntityManager;
11
use Doctrine\ORM\Event\LifecycleEventArgs;
12
use Doctrine\ORM\Event\OnFlushEventArgs;
13
use Doctrine\ORM\Events;
14
15
class AuditSubscriber implements EventSubscriber
16
{
17
    /**
18
     * @var AuditManager
19
     */
20
    private $manager;
21
22
    /**
23
     * @var ?SQLLogger
24
     */
25
    private $loggerBackup;
26
27
    public function __construct(AuditManager $manager)
28
    {
29
        $this->manager = $manager;
30
    }
31
32
    /**
33
     * It is called inside EntityManager#flush() after the changes to all the managed entities
34
     * and their associations have been computed.
35
     *
36
     * @see https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#onflush
37
     *
38
     * @param OnFlushEventArgs $args
39
     *
40
     * @throws \Doctrine\DBAL\DBALException
41
     * @throws \Doctrine\ORM\Mapping\MappingException
42
     */
43
    public function onFlush(OnFlushEventArgs $args): void
44
    {
45
        $em = $args->getEntityManager();
46
        $uow = $em->getUnitOfWork();
47
48
        // extend the SQL logger
49
        $this->loggerBackup = $em->getConnection()->getConfiguration()->getSQLLogger();
50
        $loggerChain = new LoggerChain();
51
        $loggerChain->addLogger(new AuditLogger(function () use ($em) {
52
            // flushes pending data
53
            $em->getConnection()->getConfiguration()->setSQLLogger($this->loggerBackup);
54
            $uow = $em->getUnitOfWork();
55
56
            $this->manager->processInsertions($em, $uow);
57
            $this->manager->processUpdates($em, $uow);
58
            $this->manager->processAssociations($em);
59
            $this->manager->processDissociations($em);
60
            $this->manager->processDeletions($em);
61
62
            $this->manager->reset();
63
        }));
64
65
        if ($this->loggerBackup instanceof SQLLogger) {
66
            $loggerChain->addLogger($this->loggerBackup);
67
        }
68
69
        $em->getConnection()->getConfiguration()->setSQLLogger($loggerChain);
70
71
        $this->manager->collectScheduledInsertions($uow);
72
        $this->manager->collectScheduledUpdates($uow);
73
        $this->manager->collectScheduledDeletions($uow, $em);
74
        $this->manager->collectScheduledCollectionUpdates($uow, $em);
75
        $this->manager->collectScheduledCollectionDeletions($uow, $em);
76
    }
77
78
    /**
79
     * Handles soft-delete events from Gedmo\SoftDeleteable filter.
80
     *
81
     * @see https://symfony.com/doc/current/bundles/StofDoctrineExtensionsBundle/index.html
82
     *
83
     * @param LifecycleEventArgs $args
84
     *
85
     * @throws \Doctrine\DBAL\DBALException
86
     * @throws \Doctrine\ORM\Mapping\MappingException
87
     */
88
    public function preSoftDelete(LifecycleEventArgs $args): void
89
    {
90
        $this->manager->softDelete($args->getEntityManager(), $args->getEntity());
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function getSubscribedEvents(): array
97
    {
98
        return [Events::onFlush, 'preSoftDelete'];
99
    }
100
}
101