Completed
Push — master ( 43594b...173d09 )
by Gaetano
07:30
created

RelationTraversingEntity::getRelations()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 13
nc 4
nop 1
1
<?php
2
3
namespace Kaliop\eZObjectWrapperBundle\Core\Traits;
4
5
use Kaliop\eZObjectWrapperBundle\Core\EntityInterface;
6
7
use eZ\Publish\Core\FieldType\RelationList;
8
use eZ\Publish\Core\FieldType\Relation;
9
10
/**
11
 * Adds the capability to fetch related objects as Entity.
12
 * ** Requires the EntityManagerAwareEntity trait as well **
13
 */
14
trait RelationTraversingEntity
15
{
16
    /**
17
     * @param string $fieldName
18
     * @return EntityInterface[]
19
     */
20
    protected function getRelations($fieldName)
21
    {
22
        $relatedEntities = array();
0 ignored issues
show
Unused Code introduced by
$relatedEntities is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
23
24
        $fieldValue = $this->content()->getFieldValue($fieldName);
0 ignored issues
show
Bug introduced by
It seems like content() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
25
        if (! $fieldValue instanceof RelationList\Value) {
26
            throw new \RuntimeException("Field '$fieldName' is not of type RelationList");
27
        }
28
29
        $relatedContentItems = array();
30
        foreach($fieldValue->destinationContentIds as $contentId) {
31
            // Just in case the object has been pu tin the trash or hidden and they have not updated the related fields to remove from view.
32
            try {
33
                $relatedContentItems[] = $this->getContentService()->loadContent($contentId);
0 ignored issues
show
Bug introduced by
It seems like getContentService() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
34
            } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
35
            }
36
        }
37
38
        /** @var \Kaliop\eZObjectWrapperBundle\Core\EntityManager $em */
39
        $em = $this->getEntityManager();
0 ignored issues
show
Bug introduced by
It seems like getEntityManager() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
40
        $relatedEntities = $em->loadMany($relatedContentItems);
41
42
        return $relatedEntities;
43
    }
44
45
    /**
46
     * @param string $fieldName
47
     * @return EntityInterface
48
     */
49
    protected function getRelation($fieldName)
50
    {
51
        $fieldValue = $this->content()->getFieldValue($fieldName);
0 ignored issues
show
Bug introduced by
It seems like content() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
52
        if (! $fieldValue instanceof Relation\Value) {
53
            throw new \RuntimeException("Field '$fieldName' is not of type Relation");
54
        }
55
56
        $relatedContentItem = $this->getContentService()->loadContent($fieldValue->destinationContentIds);
0 ignored issues
show
Bug introduced by
The property destinationContentIds does not seem to exist. Did you mean destinationContentId?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
It seems like getContentService() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
57
58
        /** @var \Kaliop\eZObjectWrapperBundle\Core\EntityManager $em */
59
        $em = $this->getEntityManager();
0 ignored issues
show
Bug introduced by
It seems like getEntityManager() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
60
        $relatedEntity = $em->load($relatedContentItem);
61
62
        return $relatedEntity;
63
    }
64
}
65