Failed Conditions
Pull Request — master (#6959)
by Matthew
19:32
created

PreUpdateEventArgs   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 16.66%

Importance

Changes 0
Metric Value
dl 0
loc 97
rs 10
c 0
b 0
f 0
ccs 4
cts 24
cp 0.1666
wmc 8

7 Methods

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