DoctrineSubscriber   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 12
dl 0
loc 35
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 3 1
A __construct() 0 3 1
A onFlush() 0 13 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DH\Auditor\Provider\Doctrine\Auditing\Event;
6
7
use DH\Auditor\Provider\Doctrine\Auditing\Logger\Middleware\DHDriver;
8
use DH\Auditor\Provider\Doctrine\Auditing\Transaction\TransactionManager;
9
use DH\Auditor\Provider\Doctrine\Model\Transaction;
10
use Doctrine\Common\EventSubscriber;
11
use Doctrine\ORM\Event\OnFlushEventArgs;
12
use Doctrine\ORM\Events;
13
14
final class DoctrineSubscriber implements EventSubscriber
15
{
16
    private TransactionManager $transactionManager;
17
18
    public function __construct(TransactionManager $transactionManager)
19
    {
20
        $this->transactionManager = $transactionManager;
21
    }
22
23
    /**
24
     * It is called inside EntityManager#flush() after the changes to all the managed entities
25
     * and their associations have been computed.
26
     *
27
     * @see https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#onflush
28
     */
29
    public function onFlush(OnFlushEventArgs $args): void
30
    {
31
        $entityManager = $args->getObjectManager();
32
        $transaction = new Transaction($entityManager);
33
34
        // Populate transaction
35
        $this->transactionManager->populate($transaction);
36
37
        $driver = $entityManager->getConnection()->getDriver();
0 ignored issues
show
Bug introduced by
The method getConnection() does not exist on Doctrine\Persistence\ObjectManager. It seems like you code against a sub-type of said class. However, the method does not exist in Doctrine\Persistence\ObjectManagerDecorator. Are you sure you never get one of those? ( Ignorable by Annotation )

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

37
        $driver = $entityManager->/** @scrutinizer ignore-call */ getConnection()->getDriver();
Loading history...
38
        if ($driver instanceof DHDriver) {
39
            $driver->addDHFlusher(function () use ($transaction): void {
40
                $this->transactionManager->process($transaction);
41
                $transaction->reset();
42
            });
43
        }
44
    }
45
46
    public function getSubscribedEvents(): array
47
    {
48
        return [Events::onFlush];
49
    }
50
}
51