Completed
Pull Request — master (#1448)
by Andreas
11:18
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 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 91
ccs 0
cts 47
cp 0
rs 10
c 1
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
 * @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
     * @param Builder $builder
49
     * @param string $from
50
     * @param DocumentManager $documentManager
51
     * @param ClassMetadata $class
52
     */
53
    public function __construct(Builder $builder, $from, DocumentManager $documentManager, ClassMetadata $class)
54
    {
55
        $this->dm = $documentManager;
56
        $this->class = $class;
57
58
        parent::__construct($builder, $from);
59
    }
60
61
    /**
62
     * @param string $from
63
     * @return $this
64
     */
65
    public function from($from)
66
    {
67
        // $from can either be
68
        // a) a field name indicating a reference to a different document. Currently, only REFERENCE_STORE_AS_ID is supported
69
        // b) a Class name
70
        // c) a collection name
71
        // In cases b) and c) the local and foreign fields need to be filled
72
        if ($this->class->hasReference($from)) {
73
            return $this->fromReference($from);
74
        }
75
76
        // Check if mapped class with given name exists
77
        try {
78
            $targetMapping = $this->dm->getClassMetadata($from);
79
            return parent::from($targetMapping->getCollection());
80
        } catch (BaseMappingException $e) {
81
            return parent::from($from);
82
        }
83
    }
84
85
    /**
86
     * @param string $fieldName
87
     * @return $this
88
     * @throws MappingException
89
     */
90
    private function fromReference($fieldName)
91
    {
92
        if (! $this->class->hasReference($fieldName)) {
93
            MappingException::referenceMappingNotFound($this->class->name, $fieldName);
94
        }
95
96
        $referenceMapping = $this->class->getFieldMapping($fieldName);
97
        $targetMapping = $this->dm->getClassMetadata($referenceMapping['targetDocument']);
98
        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...
99
100
        if ($referenceMapping['isOwningSide']) {
101
            if ($referenceMapping['storeAs'] !== ClassMetadataInfo::REFERENCE_STORE_AS_ID) {
102
                throw MappingException::cannotLookupNonIdReference($this->class->name, $fieldName);
103
            }
104
105
            $this
106
                ->foreignField('_id')
107
                ->localField($referenceMapping['name']);
108
        } else {
109
            if (isset($referenceMapping['repositoryMethod'])) {
110
                throw MappingException::repositoryMethodLookupNotAllowed($this->class->name, $fieldName);
111
            }
112
113
            $mappedByMapping = $targetMapping->getFieldMapping($referenceMapping['mappedBy']);
114
            if ($mappedByMapping['storeAs'] !== ClassMetadataInfo::REFERENCE_STORE_AS_ID) {
115
                throw MappingException::cannotLookupNonIdReference($this->class->name, $fieldName);
116
            }
117
118
            $this
119
                ->localField('_id')
120
                ->foreignField($mappedByMapping['name']);
121
        }
122
123
        return $this;
124
    }
125
}
126