Completed
Pull Request — master (#1448)
by Andreas
18:02
created

Lookup::fromReference()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 46
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7.7999

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 46
ccs 12
cts 19
cp 0.6316
rs 8.4751
cc 6
eloc 22
nc 10
nop 1
crap 7.7999
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 3
    public function __construct(Builder $builder, $from, DocumentManager $documentManager, ClassMetadata $class)
59
    {
60 3
        $this->dm = $documentManager;
61 3
        $this->class = $class;
62
        
63 3
        parent::__construct($builder, $from);
64 3
    }
65
66
    /**
67
     * @param string $from
68
     * @return $this
69
     */
70 3
    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 3
        if ($this->class->hasReference($from)) {
78 3
            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 3
    private function fromReference($fieldName)
90
    {
91 3
        if (! $this->class->hasReference($fieldName)) {
92
            MappingException::referenceMappingNotFound($this->class->name, $fieldName);
93
        }
94
95 3
        $referenceMapping = $this->class->getFieldMapping($fieldName);
96 3
        if ($referenceMapping['storeAs'] !== ClassMetadataInfo::REFERENCE_STORE_AS_ID) {
97
            // TODO: Change exception
98
            throw new \InvalidArgumentException('Can\'t use non-ID references in lookup stages yet.');
99
        }
100
101 3
        if (isset($referenceMapping['repositoryMethod'])) {
102
//             TODO: Use proper exception
103
            throw new \InvalidArgumentException('Can\'t use inverse references with repositoryMethod in lookup stages.');
104
        }
105
106
        // Get target mapping
107 3
        if (empty($referenceMapping['targetDocument'])) {
108
            // TODO: Use proper exception
109
            throw new \InvalidArgumentException('References must have a target document to be used in lookup stages');
110
        }
111
112 3
        $targetMapping = $this->dm->getClassMetadata($referenceMapping['targetDocument']);
113
//        if ($targetMapping->isInheritanceTypeCollectionPerClass()) {
114
            // TODO: Use proper exception
115
//            throw new \InvalidArgumentException('Cannot use references with per-class inheritance in lookup stages');
116
//        }
117
118
        // TODO: Check discriminator map?
119
120 3
        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...
121
122 3
        if ($referenceMapping['isOwningSide']) {
123
            $this
124 3
                ->foreignField('_id')
125 3
                ->localField($referenceMapping['name']);
126
        } else {
127
            $mappedByMapping = $targetMapping->getFieldMapping($referenceMapping['mappedBy']);
128
            $this
129
                ->localField('_id')
130
                ->foreignField($mappedByMapping['name']);
131
        }
132
133 3
        return $this;
134
    }
135
}
136