Completed
Pull Request — master (#50)
by Jonathan
04:19 queued 02:16
created

PreUpdateEventArgs   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getOldValue() 0 5 1
A getNewValue() 0 5 1
A hasChangedField() 0 3 1
A setNewValue() 0 5 1
A __construct() 0 5 1
A assertValidField() 0 7 2
A getEntityChangeSet() 0 3 1
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() : array
36
    {
37 1
        return $this->entityChangeSet;
38
    }
39
40
    /**
41
     * Checks if field has a changeset.
42
     */
43 1
    public function hasChangedField(string $field) : bool
44
    {
45 1
        return isset($this->entityChangeSet[$field]);
46
    }
47
48
    /**
49
     * Gets the old value of the changeset of the changed field.
50
     *
51
     * @return mixed
52
     */
53 2
    public function getOldValue(string $field)
54
    {
55 2
        $this->assertValidField($field);
56
57 1
        return $this->entityChangeSet[$field][0];
58
    }
59
60
    /**
61
     * Gets the new value of the changeset of the changed field.
62
     *
63
     * @return mixed
64
     */
65 2
    public function getNewValue(string $field)
66
    {
67 2
        $this->assertValidField($field);
68
69 1
        return $this->entityChangeSet[$field][1];
70
    }
71
72
    /**
73
     * Sets the new value of this field.
74
     *
75
     * @param mixed $value
76
     */
77 2
    public function setNewValue(string $field, $value) : void
78
    {
79 2
        $this->assertValidField($field);
80
81 1
        $this->entityChangeSet[$field][1] = $value;
82 1
    }
83
84
    /**
85
     * Asserts the field exists in changeset.
86
     *
87
     * @throws InvalidArgumentException
88
     */
89 4
    private function assertValidField(string $field) : void
90
    {
91 4
        if (! isset($this->entityChangeSet[$field])) {
92 3
            throw new InvalidArgumentException(sprintf(
93 3
                'Field "%s" is not a valid field of the entity "%s" in PreUpdateEventArgs.',
94 3
                $field,
95 3
                get_class($this->getObject())
96
            ));
97
        }
98 1
    }
99
}
100