ChangesetFactory::hasChanges()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BenTools\DoctrineWatcher\Changeset;
4
5
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
6
use Doctrine\ORM\UnitOfWork;
7
8
class ChangesetFactory
9
{
10
    /**
11
     * @param object        $entity
12
     * @param UnitOfWork    $unitOfWork
13
     * @param ClassMetadata $classMetadata
14
     * @return array
15
     */
16
    public function getChangedProperties($entity, UnitOfWork $unitOfWork, ClassMetadata $classMetadata): array
17
    {
18
        return \array_keys($this->getEntityChangeset($entity, $unitOfWork, $classMetadata));
19
    }
20
21
    /**
22
     * @param object        $entity
23
     * @param string        $property
24
     * @param UnitOfWork    $unitOfWork
25
     * @param ClassMetadata $classMetadata
26
     * @return PropertyChangeset
27
     * @throws \InvalidArgumentException
28
     */
29
    public function getChangeset($entity, string $property, UnitOfWork $unitOfWork, ClassMetadata $classMetadata): PropertyChangeset
30
    {
31
        $changeset = $this->getEntityChangeset($entity, $unitOfWork, $classMetadata)[$property] ?? [];
32
        return new PropertyChangeset(...$changeset);
0 ignored issues
show
Bug introduced by
$changeset is expanded, but the parameter $oldValue of BenTools\DoctrineWatcher...hangeset::__construct() does not expect variable arguments. ( Ignorable by Annotation )

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

32
        return new PropertyChangeset(/** @scrutinizer ignore-type */ ...$changeset);
Loading history...
33
    }
34
35
    /**
36
     * @param object        $entity
37
     * @param UnitOfWork    $unitOfWork
38
     * @param ClassMetadata $classMetadata
39
     * @return array
40
     */
41
    private function getEntityChangeset($entity, UnitOfWork $unitOfWork, ClassMetadata $classMetadata): array
42
    {
43
        if (UnitOfWork::STATE_NEW !== $unitOfWork->getEntityState($entity)) {
44
            return $unitOfWork->getEntityChangeSet($entity);
45
        }
46
        $changeset = [];
47
        $fieldNames = $classMetadata->getFieldNames();
48
        foreach ($fieldNames as $fieldName) {
49
            $reflectionProperty = $classMetadata->getReflectionClass()->getProperty($fieldName);
50
            $reflectionProperty->setAccessible(true);
51
            $changeset[$fieldName][] = $classMetadata->getReflectionClass()->getDefaultProperties()[$fieldName] ?? null;
52
            $changeset[$fieldName][] = $reflectionProperty->getValue($entity);
53
        }
54
        $changeset = \array_filter($changeset, function ($changes) {
55
            list($old, $new) = $changes;
56
            return $old !== $new;
57
        });
58
        return $changeset;
59
    }
60
61
    /**
62
     * @param object        $entity
63
     * @param string        $property
64
     * @param UnitOfWork    $unitOfWork
65
     * @param ClassMetadata $classMetadata
66
     * @return bool
67
     */
68
    public function hasChanges($entity, string $property, UnitOfWork $unitOfWork, ClassMetadata $classMetadata): bool
69
    {
70
        $changeset = $this->getEntityChangeSet($entity, $unitOfWork, $classMetadata);
71
        return \array_key_exists($property, $changeset);
72
    }
73
74
    /**
75
     * @param object     $entity
76
     * @param UnitOfWork $unitOfWork
77
     * @return bool
78
     */
79
    public function isNotManagedYet($entity, UnitOfWork $unitOfWork): bool
80
    {
81
        return UnitOfWork::STATE_NEW === $unitOfWork->getEntityState($entity);
82
    }
83
}
84