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

Lookup   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 91
ccs 0
cts 47
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A from() 0 19 3
B fromReference() 0 35 6
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\Common\Persistence\Mapping\MappingException as BaseMappingException;
23
use Doctrine\MongoDB\Aggregation\Stage as BaseStage;
24
use Doctrine\ODM\MongoDB\Aggregation\Builder;
25
use Doctrine\ODM\MongoDB\DocumentManager;
26
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
27
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
28
use Doctrine\ODM\MongoDB\Mapping\MappingException;
29
30
/**
31
 * Fluent interface for building aggregation pipelines.
32
 */
33
class Lookup extends BaseStage\Lookup
34
{
35
    /**
36
     * @var DocumentManager
37
     */
38
    private $dm;
39
40
    /**
41
     * @var ClassMetadata
42
     */
43
    private $class;
44
45
    /**
46
     * @param Builder $builder
47
     * @param string $from
48
     * @param DocumentManager $documentManager
49
     * @param ClassMetadata $class
50
     */
51
    public function __construct(Builder $builder, $from, DocumentManager $documentManager, ClassMetadata $class)
52
    {
53
        $this->dm = $documentManager;
54
        $this->class = $class;
55
56
        parent::__construct($builder, $from);
57
    }
58
59
    /**
60
     * @param string $from
61
     * @return $this
62
     */
63
    public function from($from)
64
    {
65
        // $from can either be
66
        // a) a field name indicating a reference to a different document. Currently, only REFERENCE_STORE_AS_ID is supported
67
        // b) a Class name
68
        // c) a collection name
69
        // In cases b) and c) the local and foreign fields need to be filled
70
        if ($this->class->hasReference($from)) {
71
            return $this->fromReference($from);
72
        }
73
74
        // Check if mapped class with given name exists
75
        try {
76
            $targetMapping = $this->dm->getClassMetadata($from);
77
            return parent::from($targetMapping->getCollection());
78
        } catch (BaseMappingException $e) {
79
            return parent::from($from);
80
        }
81
    }
82
83
    /**
84
     * @param string $fieldName
85
     * @return $this
86
     * @throws MappingException
87
     */
88
    private function fromReference($fieldName)
89
    {
90
        if (! $this->class->hasReference($fieldName)) {
91
            MappingException::referenceMappingNotFound($this->class->name, $fieldName);
92
        }
93
94
        $referenceMapping = $this->class->getFieldMapping($fieldName);
95
        $targetMapping = $this->dm->getClassMetadata($referenceMapping['targetDocument']);
96
        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...
97
98
        if ($referenceMapping['isOwningSide']) {
99
            if ($referenceMapping['storeAs'] !== ClassMetadataInfo::REFERENCE_STORE_AS_ID) {
100
                throw MappingException::cannotLookupNonIdReference($this->class->name, $fieldName);
101
            }
102
103
            $this
104
                ->foreignField('_id')
105
                ->localField($referenceMapping['name']);
106
        } else {
107
            if (isset($referenceMapping['repositoryMethod'])) {
108
                throw MappingException::repositoryMethodLookupNotAllowed($this->class->name, $fieldName);
109
            }
110
111
            $mappedByMapping = $targetMapping->getFieldMapping($referenceMapping['mappedBy']);
112
            if ($mappedByMapping['storeAs'] !== ClassMetadataInfo::REFERENCE_STORE_AS_ID) {
113
                throw MappingException::cannotLookupNonIdReference($this->class->name, $fieldName);
114
            }
115
116
            $this
117
                ->localField('_id')
118
                ->foreignField($mappedByMapping['name']);
119
        }
120
121
        return $this;
122
    }
123
}
124