Failed Conditions
Pull Request — 1.3.x (#71)
by Grégoire
02:19
created

PreUpdateEventArgs   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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

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