Failed Conditions
Push — master ( 3a6c46...2fbe69 )
by Luís
36s queued 11s
created

PreUpdateEventArgs::getEntityChangeSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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