Completed
Pull Request — master (#1419)
by Robert
11:09 queued 01:09
created

PreUpdateEventArgs::getNewValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
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 231
    public function __construct($document, DocumentManager $dm, array $changeSet)
44
    {
45 231
        parent::__construct($document, $dm);
46 231
        $this->documentChangeSet = $changeSet;
47 231
    }
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 1
    public function hasChangedField($field)
67
    {
68 1
        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
        $this->getDocumentManager()->getUnitOfWork()->setDocumentChangeSet($this->getDocument(), $this->documentChangeSet);
109 1
    }
110
111
    /**
112
     * Asserts the field exists in changeset.
113
     *
114
     * @param string $field
115
     * @throws \InvalidArgumentException if the field has no changeset
116
     */
117 1
    private function assertValidField($field)
118
    {
119 1
        if ( ! isset($this->documentChangeSet[$field])) {
120
            throw new \InvalidArgumentException(sprintf(
121
                'Field "%s" is not a valid field of the document "%s" in PreUpdateEventArgs.',
122
                $field,
123
                get_class($this->getDocument())
124
            ));
125
        }
126 1
    }
127
}
128