PreUpdateEventArgs   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 16
c 1
b 0
f 0
dl 0
loc 87
ccs 24
cts 24
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getEntityChangeSet() 0 3 1
A getOldValue() 0 5 1
A getNewValue() 0 5 1
A hasChangedField() 0 3 1
A setNewValue() 0 5 1
A assertValidField() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Persistence\Event;
6
7
use Doctrine\Persistence\ObjectManager;
8
use InvalidArgumentException;
9
use function get_class;
10
use function sprintf;
11
12
/**
13
 * Class that holds event arguments for a preUpdate event.
14
 */
15
class PreUpdateEventArgs extends LifecycleEventArgs
16
{
17
    /** @var array<string, array<int, mixed>> */
18
    private $entityChangeSet;
19
20
    /**
21
     * @param array<string, array<int, mixed>> $changeSet
22
     */
23 4
    public function __construct(object $entity, ObjectManager $objectManager, array &$changeSet)
24
    {
25 4
        parent::__construct($entity, $objectManager);
26
27 4
        $this->entityChangeSet = &$changeSet;
28 4
    }
29
30
    /**
31
     * Retrieves the entity changeset.
32
     *
33
     * @return array<string, array<int, mixed>>
34
     */
35 1
    public function getEntityChangeSet()
36
    {
37 1
        return $this->entityChangeSet;
38
    }
39
40
    /**
41
     * Checks if field has a changeset.
42
     *
43
     * @return bool
44
     */
45 1
    public function hasChangedField(string $field)
46
    {
47 1
        return isset($this->entityChangeSet[$field]);
48
    }
49
50
    /**
51
     * Gets the old value of the changeset of the changed field.
52
     *
53
     * @return mixed
54
     */
55 2
    public function getOldValue(string $field)
56
    {
57 2
        $this->assertValidField($field);
58
59 1
        return $this->entityChangeSet[$field][0];
60
    }
61
62
    /**
63
     * Gets the new value of the changeset of the changed field.
64
     *
65
     * @return mixed
66
     */
67 2
    public function getNewValue(string $field)
68
    {
69 2
        $this->assertValidField($field);
70
71 1
        return $this->entityChangeSet[$field][1];
72
    }
73
74
    /**
75
     * Sets the new value of this field.
76
     *
77
     * @param mixed $value
78
     *
79
     * @return void
80
     */
81 2
    public function setNewValue(string $field, $value)
82
    {
83 2
        $this->assertValidField($field);
84
85 1
        $this->entityChangeSet[$field][1] = $value;
86 1
    }
87
88
    /**
89
     * Asserts the field exists in changeset.
90
     *
91
     * @return void
92
     *
93
     * @throws InvalidArgumentException
94
     */
95 4
    private function assertValidField(string $field)
96
    {
97 4
        if (! isset($this->entityChangeSet[$field])) {
98 3
            throw new InvalidArgumentException(sprintf(
99 3
                'Field "%s" is not a valid field of the entity "%s" in PreUpdateEventArgs.',
100 3
                $field,
101 3
                get_class($this->getObject())
102
            ));
103
        }
104 1
    }
105
}
106