Completed
Pull Request — master (#1448)
by Andreas
10:20
created

Lookup::fromReference()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 0
cts 29
cp 0
rs 8.439
c 0
b 0
f 0
cc 6
eloc 22
nc 10
nop 1
crap 42
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\Aggregation\Stage;
21
22
use Doctrine\MongoDB\Aggregation\Stage as BaseStage;
23
use Doctrine\ODM\MongoDB\Aggregation\Builder;
24
use Doctrine\ODM\MongoDB\DocumentManager;
25
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
26
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
27
use Doctrine\ODM\MongoDB\Mapping\MappingException;
28
use Doctrine\ODM\MongoDB\Query\Expr;
29
30
/**
31
 * Fluent interface for building aggregation pipelines.
32
 *
33
 * @author alcaeus <[email protected]>
34
 */
35
class Lookup extends BaseStage\Lookup
36
{
37
    /**
38
     * @var DocumentManager
39
     */
40
    private $dm;
41
42
    /**
43
     * @var ClassMetadata
44
     */
45
    private $class;
46
47
    /**
48
     * @var ClassMetadata
49
     */
50
    private $targetClass;
51
52
    /**
53
     * @param Builder $builder
54
     * @param string $from
55
     * @param DocumentManager $documentManager
56
     * @param ClassMetadata $class
57
     */
58
    public function __construct(Builder $builder, $from, DocumentManager $documentManager, ClassMetadata $class)
59
    {
60
        $this->dm = $documentManager;
61
        $this->class = $class;
62
63
        parent::__construct($builder, $from);
64
    }
65
66
    /**
67
     * @param string $from
68
     * @return $this
69
     */
70
    public function from($from)
71
    {
72
        // $from can either be
73
        // a) a field name indicating a reference to a different document. Currently, only REFERENCE_STORE_AS_ID is supported
74
        // b) a Class name
75
        // c) a collection name
76
        // In cases b) and c) the local and foreign fields need to be filled
77
        if ($this->class->hasReference($from)) {
78
            return $this->fromReference($from);
79
        } else {
80
            return parent::from($from);
81
        }
82
    }
83
84
    /**
85
     * @param string $fieldName
86
     * @return $this
87
     * @throws MappingException
88
     */
89
    private function fromReference($fieldName)
90
    {
91
        if (! $this->class->hasReference($fieldName)) {
92
            MappingException::referenceMappingNotFound($this->class->name, $fieldName);
93
        }
94
95
        $referenceMapping = $this->class->getFieldMapping($fieldName);
96
        $targetMapping = $this->dm->getClassMetadata($referenceMapping['targetDocument']);
97
        parent::from($targetMapping->getCollection());
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (from() instead of fromReference()). Are you sure this is correct? If so, you might want to change this to $this->from().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
98
99
        if ($referenceMapping['isOwningSide']) {
100
            if ($referenceMapping['storeAs'] !== ClassMetadataInfo::REFERENCE_STORE_AS_ID) {
101
                throw MappingException::cannotLookupNonIdReference($this->class->name, $fieldName);
102
            }
103
104
            $this
105
                ->foreignField('_id')
106
                ->localField($referenceMapping['name']);
107
        } else {
108
            if (isset($referenceMapping['repositoryMethod'])) {
109
                throw MappingException::repositoryMethodLookupNotAllowed($this->class->name, $fieldName);
110
            }
111
112
            $mappedByMapping = $targetMapping->getFieldMapping($referenceMapping['mappedBy']);
113
            if ($mappedByMapping['storeAs'] !== ClassMetadataInfo::REFERENCE_STORE_AS_ID) {
114
                throw MappingException::cannotLookupNonIdReference($this->class->name, $fieldName);
115
            }
116
117
            $this
118
                ->localField('_id')
119
                ->foreignField($mappedByMapping['name']);
120
        }
121
122
        return $this;
123
    }
124
}
125