Failed Conditions
Pull Request — 2.7 (#7857)
by Luís
08:11
created

ReflectionEmbeddedProperty   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
wmc 10
eloc 21
c 0
b 0
f 0
dl 0
loc 80
rs 10
ccs 22
cts 23
cp 0.9565

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setValue() 0 13 3
A getParentPropertyValue() 0 7 4
A getValue() 0 9 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\ORM\Mapping;
21
22
use Doctrine\Instantiator\Instantiator;
23
use ReflectionProperty;
24
use function method_exists;
25
26
/**
27
 * Acts as a proxy to a nested Property structure, making it look like
28
 * just a single scalar property.
29
 *
30
 * This way value objects "just work" without UnitOfWork, Persisters or Hydrators
31
 * needing any changes.
32
 *
33
 * TODO: Move this class into Common\Reflection
34
 */
35
class ReflectionEmbeddedProperty extends ReflectionProperty
36
{
37
    /**
38
     * @var ReflectionProperty reflection property of the class where the embedded object has to be put
39
     */
40
    private $parentProperty;
41
42
    /**
43
     * @var ReflectionProperty reflection property of the embedded object
44
     */
45
    private $childProperty;
46
47
    /**
48
     * @var string name of the embedded class to be eventually instantiated
49
     */
50
    private $embeddedClass;
51
52
    /**
53
     * @var Instantiator|null
54
     */
55
    private $instantiator;
56
57
    /**
58
     * @param ReflectionProperty $parentProperty
59
     * @param ReflectionProperty $childProperty
60
     * @param string             $embeddedClass
61
     */
62 34
    public function __construct(ReflectionProperty $parentProperty, ReflectionProperty $childProperty, $embeddedClass)
63
    {
64 34
        $this->parentProperty  = $parentProperty;
65 34
        $this->childProperty   = $childProperty;
66 34
        $this->embeddedClass   = (string) $embeddedClass;
67
68 34
        parent::__construct($childProperty->getDeclaringClass()->getName(), $childProperty->getName());
69 34
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74 21
    public function getValue($object = null)
75
    {
76 21
        $embeddedObject = $this->getParentPropertyValue($object);
77
78 21
        if (null === $embeddedObject) {
79 8
            return null;
80
        }
81
82 15
        return $this->childProperty->getValue($embeddedObject);
83
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88 14
    public function setValue($object, $value = null)
89
    {
90 14
        $embeddedObject = $this->getParentPropertyValue($object);
91
92 14
        if (null === $embeddedObject) {
93 13
            $this->instantiator = $this->instantiator ?: new Instantiator();
94
95 13
            $embeddedObject = $this->instantiator->instantiate($this->embeddedClass);
96
97 13
            $this->parentProperty->setValue($object, $embeddedObject);
98
        }
99
100 14
        $this->childProperty->setValue($embeddedObject, $value);
101 14
    }
102
103
    /**
104
     * @param object $object
105
     *
106
     * @return mixed
107
     */
108 21
    private function getParentPropertyValue($object)
109
    {
110 21
        if (method_exists($this->parentProperty, 'isInitialized') && $this->parentProperty->getDeclaringClass()->isInstance($object)) {
111
            return $this->parentProperty->isInitialized($object) ? $this->parentProperty->getValue($object) : null;
112
        }
113
114 21
        return $this->parentProperty->getValue($object);
115
    }
116
}
117