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