Skip to content

Commit

Permalink
Fix review issues
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Oct 22, 2017
1 parent 0ee7e8a commit bc70b7a
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 73 deletions.
17 changes: 17 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Bucket.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\ODM\MongoDB\Aggregation\Stage;

Expand Down
17 changes: 17 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Stage/BucketAuto.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\ODM\MongoDB\Aggregation\Stage;

Expand Down
102 changes: 32 additions & 70 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Stage/GraphLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ public function from($from)
public function connectFromField($connectFromField)
{
// No targetClass mapping - simply use field name as is
if (!$this->targetClass) {
if ( ! $this->targetClass) {
return parent::connectFromField($connectFromField);
}

// connectFromField doesn't have to be a reference - in this case, just convert the field name
if (!$this->targetClass->hasReference($connectFromField)) {
if ( ! $this->targetClass->hasReference($connectFromField)) {
return parent::connectFromField($this->convertTargetFieldName($connectFromField));
}

Expand All @@ -107,38 +107,7 @@ public function connectFromField($connectFromField)
throw MappingException::connectFromFieldMustReferenceSameDocument($connectFromField);
}

if ($referenceMapping['isOwningSide']) {
switch ($referenceMapping['storeAs']) {
case ClassMetadataInfo::REFERENCE_STORE_AS_ID:
case ClassMetadataInfo::REFERENCE_STORE_AS_REF:
$referencedFieldName = ClassMetadataInfo::getReferenceFieldName($referenceMapping['storeAs'], $referenceMapping['name']);
break;

default:
throw MappingException::cannotLookupDbRefReference($this->class->name, $connectFromField);
}

parent::connectFromField($referencedFieldName);
} else {
if (isset($referenceMapping['repositoryMethod'])) {
throw MappingException::repositoryMethodLookupNotAllowed($this->class->name, $connectFromField);
}

$mappedByMapping = $this->targetClass->getFieldMapping($referenceMapping['mappedBy']);
switch ($mappedByMapping['storeAs']) {
case ClassMetadataInfo::REFERENCE_STORE_AS_ID:
case ClassMetadataInfo::REFERENCE_STORE_AS_REF:
$referencedFieldName = ClassMetadataInfo::getReferenceFieldName($mappedByMapping['storeAs'], $mappedByMapping['name']);
break;

default:
throw MappingException::cannotLookupDbRefReference($this->class->name, $connectFromField);
}

parent::connectFromField($referencedFieldName);
}

return $this;
return parent::connectFromField($this->getReferencedFieldName($connectFromField, $referenceMapping));
}

public function connectToField($connectToField)
Expand All @@ -153,7 +122,7 @@ public function connectToField($connectToField)
*/
private function fromReference($fieldName)
{
if (! $this->class->hasReference($fieldName)) {
if ( ! $this->class->hasReference($fieldName)) {
MappingException::referenceMappingNotFound($this->class->name, $fieldName);
}

Expand All @@ -165,49 +134,21 @@ private function fromReference($fieldName)

parent::from($this->targetClass->getCollection());

if ($referenceMapping['isOwningSide']) {
switch ($referenceMapping['storeAs']) {
case ClassMetadataInfo::REFERENCE_STORE_AS_ID:
case ClassMetadataInfo::REFERENCE_STORE_AS_REF:
$referencedFieldName = ClassMetadataInfo::getReferenceFieldName($referenceMapping['storeAs'], $referenceMapping['name']);
break;

default:
throw MappingException::cannotLookupDbRefReference($this->class->name, $fieldName);
}
$referencedFieldName = $this->getReferencedFieldName($fieldName, $referenceMapping);

if ($referenceMapping['isOwningSide']) {
$this
->startWith('$' . $referencedFieldName)
->connectToField('_id');

// A self-reference indicates that we can also fill the "connectFromField" accordingly
if ($this->targetClass->name === $this->class->name) {
$this->connectFromField($referencedFieldName);
}
} else {
if (isset($referenceMapping['repositoryMethod'])) {
throw MappingException::repositoryMethodLookupNotAllowed($this->class->name, $fieldName);
}

$mappedByMapping = $this->targetClass->getFieldMapping($referenceMapping['mappedBy']);
switch ($mappedByMapping['storeAs']) {
case ClassMetadataInfo::REFERENCE_STORE_AS_ID:
case ClassMetadataInfo::REFERENCE_STORE_AS_REF:
$referencedFieldName = ClassMetadataInfo::getReferenceFieldName($mappedByMapping['storeAs'], $mappedByMapping['name']);
break;

default:
throw MappingException::cannotLookupDbRefReference($this->class->name, $fieldName);
}

$this
->startWith('$' . $referencedFieldName)
->connectToField('_id');
}

// A self-reference indicates that we can also fill the "connectFromField" accordingly
if ($this->targetClass->name === $this->class->name) {
$this->connectFromField($referencedFieldName);
}
// A self-reference indicates that we can also fill the "connectFromField" accordingly
if ($this->targetClass->name === $this->class->name) {
$this->connectFromField($referencedFieldName);
}

return $this;
Expand All @@ -230,7 +171,7 @@ protected function convertTargetFieldName($fieldName)
return array_map([$this, 'convertTargetFieldName'], $fieldName);
}

if (!$this->targetClass) {
if ( ! $this->targetClass) {
return $fieldName;
}

Expand All @@ -245,4 +186,25 @@ private function getDocumentPersister(ClassMetadata $class)
{
return $this->dm->getUnitOfWork()->getDocumentPersister($class->name);
}

private function getReferencedFieldName($fieldName, array $mapping)
{
if ( ! $mapping['isOwningSide']) {
if (isset($mapping['repositoryMethod']) || ! isset($mapping['mappedBy'])) {
throw MappingException::repositoryMethodLookupNotAllowed($this->class->name, $fieldName);
}

$mapping = $this->targetClass->getFieldMapping($mapping['mappedBy']);
}

switch ($mapping['storeAs']) {
case ClassMetadataInfo::REFERENCE_STORE_AS_ID:
case ClassMetadataInfo::REFERENCE_STORE_AS_REF:
return ClassMetadataInfo::getReferenceFieldName($mapping['storeAs'], $mapping['name']);
break;

default:
throw MappingException::cannotLookupDbRefReference($this->class->name, $fieldName);
}
}
}
4 changes: 2 additions & 2 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private function fromReference($fieldName)
->foreignField('_id')
->localField($referencedFieldName);
} else {
if (isset($referenceMapping['repositoryMethod'])) {
if (isset($referenceMapping['repositoryMethod']) || ! isset($referenceMapping['mappedBy'])) {
throw MappingException::repositoryMethodLookupNotAllowed($this->class->name, $fieldName);
}

Expand Down Expand Up @@ -165,7 +165,7 @@ public function foreignField($foreignField)

protected function prepareFieldName($fieldName, ClassMetadata $class = null)
{
if (!$class) {
if ( ! $class) {
return $fieldName;
}

Expand Down
17 changes: 17 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Stage/ReplaceRoot.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\ODM\MongoDB\Aggregation\Stage;

Expand Down
17 changes: 17 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Stage/SortByCount.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\ODM\MongoDB\Aggregation\Stage;

Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Mapping/MappingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,6 @@ public static function canNotCombineReadPreferenceAndSlaveOkay($className)

public static function connectFromFieldMustReferenceSameDocument($fieldName)
{
return new self("Cannot use field '$fieldName' as connectFromField in \$graphLookup stage. Reference must target the document itself.");
return new self("Cannot use field '$fieldName' as connectFromField in a \$graphLookup stage. Reference must target the document itself.");
}
}

0 comments on commit bc70b7a

Please sign in to comment.