LifecycleCallableGenerator   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 17
eloc 46
c 2
b 0
f 0
dl 0
loc 100
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A generateCreateDeleteCallables() 0 12 3
B generateUpdateCallables() 0 23 7
A generateLifeCycleCallable() 0 32 6
A __construct() 0 5 1
1
<?php declare(strict_types = 1);
2
3
namespace Actiane\EntityChangeWatchBundle\Generator;
4
5
use Doctrine\Common\Util\ClassUtils;
6
use Doctrine\ORM\UnitOfWork;
7
use Symfony\Component\PropertyAccess\PropertyAccessor;
8
9
/**
10
 * Class LifecycleCallableGenerator
11
 */
12
class LifecycleCallableGenerator
13
{
14
    private array $entityWatch;
15
    private PropertyAccessor $propertyAccessor;
16
    private CallableGenerator $callableGenerator;
17
18
    public function __construct(array $entityWatch, CallableGenerator $callableGenerator, PropertyAccessor $propertyAccessor)
19
    {
20
        $this->entityWatch = $entityWatch;
21
        $this->propertyAccessor = $propertyAccessor;
22
        $this->callableGenerator = $callableGenerator;
23
    }
24
25
    /**
26
     * @param UnitOfWork $unitOfWork
27
     *
28
     * @return array
29
     */
30
    public function generateLifeCycleCallable(UnitOfWork $unitOfWork): array
31
    {
32
        $callable = [];
33
        $lifeCycleEntities = [];
34
        foreach ($unitOfWork->getScheduledEntityInsertions() as $entityInsertion) {
35
            $this->generateCreateDeleteCallables($callable, $lifeCycleEntities, $entityInsertion, 'create');
36
        }
37
38
        foreach ($unitOfWork->getScheduledEntityDeletions() as $entityDeletion) {
39
            $this->generateCreateDeleteCallables($callable, $lifeCycleEntities, $entityDeletion, 'delete');
40
        }
41
42
        foreach ($unitOfWork->getScheduledCollectionUpdates() as $collectionUpdate) {
43
            if (array_key_exists(spl_object_hash($collectionUpdate->getOwner()), $lifeCycleEntities)) {
44
                continue;
45
            }
46
            $this->generateUpdateCallables(
47
                $callable,
48
                [$collectionUpdate->getMapping()['fieldName'] => $collectionUpdate->getValues()],
49
                $collectionUpdate->getOwner()
50
            );
51
        }
52
53
        foreach ($unitOfWork->getScheduledEntityUpdates() as $entityUpdate) {
54
            $this->generateUpdateCallables(
55
                $callable,
56
                $unitOfWork->getEntityChangeSet($entityUpdate),
57
                $entityUpdate
58
            );
59
        }
60
61
        return $callable;
62
    }
63
64
    /**
65
     * @param $callable
66
     * @param $entity
67
     * @param $state
68
     */
69
    private function generateCreateDeleteCallables(&$callable, &$lifeCycleEntities, $entity, $state): void
70
    {
71
        $lifeCycleEntities[spl_object_hash($entity)] = $entity;
72
        // we might have a doctrine proxyfied object
73
        $className = ClassUtils::getClass($entity);
74
75
        if (!array_key_exists($className, $this->entityWatch)) {
76
            return;
77
        }
78
79
        foreach ($this->entityWatch[$className][$state] as $action) {
80
            $callable += $this->callableGenerator->generateCallable($action, $entity);
81
        }
82
    }
83
84
    /**
85
     * @param $callable
86
     * @param $changedProperties
87
     * @param $entity
88
     */
89
    private function generateUpdateCallables(&$callable, $changedProperties, $entity): void
90
    {
91
        // we might have a doctrine proxyfied object
92
        $className = ClassUtils::getClass($entity);
93
94
        if (!array_key_exists($className, $this->entityWatch)
95
            || !array_key_exists('update', $this->entityWatch[$className])
96
        ) {
97
            return;
98
        }
99
100
        $entityWatch = $this->entityWatch[$className]['update'];
101
        foreach ($entityWatch['all'] as $action) {
102
            $callable += $this->callableGenerator->generateCallable($action, $entity, $changedProperties);
103
        }
104
105
        foreach ($entityWatch['properties'] as $propertyName => $actions) {
106
            if (array_key_exists($propertyName, $changedProperties)) {
107
                foreach ($actions as $action) {
108
                    $callable += $this->callableGenerator->generateCallable(
109
                        $action,
110
                        $entity,
111
                        $changedProperties
112
                    );
113
                }
114
            }
115
        }
116
    }
117
}
118