Completed
Pull Request — master (#1331)
by Maciej
09:05
created

PreUpdateEventArgs::setNewValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ODM\MongoDB\Event;
21
22
use Doctrine\ODM\MongoDB\DocumentManager;
23
24
/**
25
 * Class that holds event arguments for a preUpdate event.
26
 *
27
 * @since 1.0
28
 */
29
class PreUpdateEventArgs extends LifecycleEventArgs
30
{
31
    /**
32
     * @var array
33
     */
34
    private $documentChangeSet;
35
36
    /**
37
     * Constructor.
38
     *
39
     * @param object          $document
40
     * @param DocumentManager $dm
41
     * @param array           $changeSet
42
     */
43 219
    public function __construct($document, DocumentManager $dm, array &$changeSet)
44
    {
45 219
        parent::__construct($document, $dm);
46 219
        $this->documentChangeSet = &$changeSet;
47 219
    }
48
49
    /**
50
     * Retrieves the document changeset.
51
     *
52
     * @return array
53
     */
54
    public function getDocumentChangeSet()
55
    {
56
        return $this->documentChangeSet;
57
    }
58
59
    /**
60
     * Checks if field has a changeset.
61
     *
62
     * @param string $field
63
     *
64
     * @return boolean
65
     */
66
    public function hasChangedField($field)
67
    {
68
        return isset($this->documentChangeSet[$field]);
69
    }
70
71
    /**
72
     * Gets the old value of the changeset of the changed field.
73
     *
74
     * @param string $field
75
     * @return mixed
76
     */
77
    public function getOldValue($field)
78
    {
79
        $this->assertValidField($field);
80
81
        return $this->documentChangeSet[$field][0];
82
    }
83
84
    /**
85
     * Gets the new value of the changeset of the changed field.
86
     *
87
     * @param string $field
88
     * @return mixed
89
     */
90
    public function getNewValue($field)
91
    {
92
        $this->assertValidField($field);
93
94
        return $this->documentChangeSet[$field][1];
95
    }
96
97
    /**
98
     * Sets the new value of this field.
99
     *
100
     * @param string $field
101
     * @param mixed  $value
102
     */
103 1
    public function setNewValue($field, $value)
104
    {
105 1
        $this->assertValidField($field);
106
107 1
        $this->documentChangeSet[$field][1] = $value;
108 1
    }
109
110
    /**
111
     * Asserts the field exists in changeset.
112
     *
113
     * @param string $field
114
     * @throws \InvalidArgumentException if the field has no changeset
115
     */
116 1
    private function assertValidField($field)
117
    {
118 1
        if ( ! isset($this->documentChangeSet[$field])) {
119
            throw new \InvalidArgumentException(sprintf(
120
                'Field "%s" is not a valid field of the document "%s" in PreUpdateEventArgs.',
121
                $field,
122
                get_class($this->getDocument())
123
            ));
124
        }
125 1
    }
126
}
127