EntityModificationListener::__construct()   A
last analyzed

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
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php declare(strict_types = 1);
2
3
namespace Actiane\EntityChangeWatchBundle\Listener;
4
5
use Actiane\EntityChangeWatchBundle\Generator\LifecycleCallableGenerator;
6
use Doctrine\ORM\Event\OnFlushEventArgs;
7
use Doctrine\ORM\Event\PostFlushEventArgs;
8
9
/**
10
 * Class EntityModificationListener
11
 */
12
class EntityModificationListener
13
{
14
    private array $callable = [];
15
    private LifecycleCallableGenerator $lifecycleCallableGenerator;
16
17
    /**
18
     * @param LifecycleCallableGenerator $lifecycleCallableGenerator
19
     */
20
    public function __construct(LifecycleCallableGenerator $lifecycleCallableGenerator)
21
    {
22
        $this->lifecycleCallableGenerator = $lifecycleCallableGenerator;
23
    }
24
25
    /**
26
     * Called before the database queries, execute all the Generate $this->callable
27
     *
28
     * @param OnFlushEventArgs $eventArgs
29
     */
30
    public function onFlush(OnFlushEventArgs $eventArgs): void
31
    {
32
        $entityManager = $eventArgs->getEntityManager();
33
        $this->callable = $this->lifecycleCallableGenerator->generateLifeCycleCallable(
34
            $entityManager->getUnitOfWork()
35
        );
36
37
        foreach ($this->callable as $key => $callableItem) {
38
            if ($callableItem['flush'] !== false) {
39
                continue;
40
            }
41
            unset($this->callable[$key]);
42
            call_user_func_array(
43
                $callableItem['callable'],
44
                $callableItem['parameters'] + ['entityManager' => $entityManager]
45
            );
46
        }
47
    }
48
49
    /**
50
     * Called before the database queries, execute all the Generate $this->callableFlush
51
     *
52
     * @param PostFlushEventArgs $eventArgs
53
     */
54
    public function postFlush(PostFlushEventArgs $eventArgs): void
55
    {
56
        foreach ($this->callable as $key => $callableItem) {
57
            unset($this->callable[$key]);
58
            call_user_func_array(
59
                $callableItem['callable'],
60
                $callableItem['parameters'] + ['entityManager' => $eventArgs->getEntityManager()]
61
            );
62
        }
63
    }
64
}
65