Completed
Push — master ( 2a52ee...a45a67 )
by Andreas
11:17
created

Doctrine/ODM/MongoDB/Aggregation/Stage/Lookup.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 7
    public function __construct(Builder $builder, $from, DocumentManager $documentManager, ClassMetadata $class)
52
    {
53 7
        $this->dm = $documentManager;
54 7
        $this->class = $class;
55
56 7
        parent::__construct($builder, $from);
57 7
    }
58
59
    /**
60
     * @param string $from
61
     * @return $this
62
     */
63 7
    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 7
        if ($this->class->hasReference($from)) {
71 5
            return $this->fromReference($from);
72
        }
73
74
        // Check if mapped class with given name exists
75
        try {
76 2
            $targetMapping = $this->dm->getClassMetadata($from);
77 1
            return parent::from($targetMapping->getCollection());
78 1
        } catch (BaseMappingException $e) {
79 1
            return parent::from($from);
80
        }
81
    }
82
83
    /**
84
     * @param string $fieldName
85
     * @return $this
86
     * @throws MappingException
87
     */
88 5
    private function fromReference($fieldName)
89
    {
90 5
        if (! $this->class->hasReference($fieldName)) {
91
            MappingException::referenceMappingNotFound($this->class->name, $fieldName);
92
        }
93
94 5
        $referenceMapping = $this->class->getFieldMapping($fieldName);
95 5
        $targetMapping = $this->dm->getClassMetadata($referenceMapping['targetDocument']);
96 5
        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 5
        if ($referenceMapping['isOwningSide']) {
99 3
            if ($referenceMapping['storeAs'] !== ClassMetadataInfo::REFERENCE_STORE_AS_ID) {
100
                throw MappingException::cannotLookupNonIdReference($this->class->name, $fieldName);
101
            }
102
103
            $this
104 3
                ->foreignField('_id')
105 3
                ->localField($referenceMapping['name']);
106
        } else {
107 2
            if (isset($referenceMapping['repositoryMethod'])) {
108
                throw MappingException::repositoryMethodLookupNotAllowed($this->class->name, $fieldName);
109
            }
110
111 2
            $mappedByMapping = $targetMapping->getFieldMapping($referenceMapping['mappedBy']);
112 2
            if ($mappedByMapping['storeAs'] !== ClassMetadataInfo::REFERENCE_STORE_AS_ID) {
113
                throw MappingException::cannotLookupNonIdReference($this->class->name, $fieldName);
114
            }
115
116
            $this
117 2
                ->localField('_id')
118 2
                ->foreignField($mappedByMapping['name']);
119
        }
120
121 5
        return $this;
122
    }
123
}
124