GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 0c01fe...beedbe )
by Romain
01:58
created

LinksRelation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 4
1
<?php
2
3
namespace Halapi\Relation;
4
5
use Doctrine\Common\Collections\Collection;
6
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
7
use Doctrine\Common\Persistence\ObjectManager;
8
use Doctrine\Common\Annotations\Reader;
9
use Symfony\Component\HttpFoundation\RequestStack;
10
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
11
12
/**
13
 * Class LinksRelation.
14
 *
15
 * @author Romain Richard
16
 */
17
class LinksRelation extends AbstractRelation implements RelationInterface
18
{
19
    /**
20
     * @var UrlGeneratorInterface
21
     */
22
    private $urlGenerator;
23
24
    /**
25
     * @var ObjectManager
26
     */
27
    private $objectManager;
28
29
    /**
30
     * @var RequestStack
31
     */
32
    private $requestStack;
33
34
    /**
35
     * @var ClassMetadata
36
     */
37
    private $classMetadata;
38
39
    /**
40
     * @var \ReflectionClass
41
     */
42
    private $reflectionClass;
43
44
    /**
45
     * AbstractRelation constructor.
46
     *
47
     * @param Reader                $annotationReader
48
     * @param UrlGeneratorInterface $urlGenerator
49
     * @param ObjectManager         $objectManager
50
     * @param RequestStack          $requestStack
51
     */
52
    public function __construct(
53
        Reader $annotationReader,
54
        UrlGeneratorInterface $urlGenerator,
55
        ObjectManager $objectManager,
56
        RequestStack $requestStack
57
    ) {
58
        $this->urlGenerator = $urlGenerator;
59
        $this->annotationReader = $annotationReader;
60
        $this->objectManager = $objectManager;
61
        $this->requestStack = $requestStack;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getName()
68
    {
69
        return '_links';
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getRelation($resource)
76
    {
77
        $this->classMetadata = $this->objectManager->getClassMetadata(get_class($resource));
78
        $this->reflectionClass = new \ReflectionClass($resource);
79
        $links = $this->getSelfLink($resource);
80
81
        foreach ($this->reflectionClass->getProperties() as $property) {
82
            if ($this->isEmbeddable($property) && $property->getName()) {
83
                $propertyName = $property->getName();
84
                $relationContent = $resource->{'get'.ucfirst($propertyName)}();
85
                if ($relationContent) {
86
                    $links[$propertyName] = $this->getRelationLinks($property, $relationContent);
87
                }
88
            }
89
        }
90
91
        return $links;
92
    }
93
94
    /**
95
     * @param \ReflectionProperty $property
96
     * @param object              $relationContent
97
     *
98
     * @return string|null
99
     *
100
     * @throws \Doctrine\ORM\Mapping\MappingException
101
     */
102
    private function getRelationLink(\ReflectionProperty $property, $relationContent)
103
    {
104
        if ($this->classMetadata->hasAssociation($property->getName())) {
105
            $targetClass = $this->classMetadata->getAssociationTargetClass($property->getName());
106
            $shortName = strtolower((new \ReflectionClass($targetClass))->getShortName());
107
108
            return $this->urlGenerator->generate(
109
                'get_'.$shortName,
110
                [$shortName => $this->getEntityId($relationContent)]
111
            );
112
        }
113
    }
114
115
    /**
116
     * Get the url of an entity based on the 'get_entity' route pattern.
117
     *
118
     * @param $resource
119
     * @param \ReflectionClass $reflectionClass
0 ignored issues
show
Bug introduced by
There is no parameter named $reflectionClass. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
120
     *
121
     * @return array|null
122
     */
123
    private function getSelfLink($resource)
124
    {
125
        if ($resource instanceof \Traversable) {
126
            return;
127
        }
128
129
        return [
130
            'self' => $this->urlGenerator->generate(
131
                'get_'.strtolower($this->reflectionClass->getShortName()),
132
                [strtolower($this->reflectionClass->getShortName()) => $this->getEntityId($resource)]
133
            ),
134
        ];
135
    }
136
137
    /**
138
     * Get the links of a collection.
139
     *
140
     * @param \ReflectionProperty $property
141
     * @param $relationContent
142
     *
143
     * @return array|void
144
     */
145
    private function getRelationLinks(\ReflectionProperty $property, $relationContent)
146
    {
147
        if ($relationContent instanceof Collection) {
148
            $links = [];
149
            foreach ($relationContent as $relation) {
150
                $links[] = $this->getRelationLink($property, $relation);
151
            }
152
153
            return $links;
154
        }
155
156
        return $this->getRelationLink($property, $relationContent);
157
    }
158
159
    /**
160
     * Returns entity single identifier.
161
     * This is a compatibility-limiting feature as it will not be able to get the identity
162
     * of an entity which has multiple identifiers.
163
     *
164
     * @param $entity
165
     */
166
    private function getEntityId($entity)
167
    {
168
        $identifier = $this->classMetadata->getIdentifier()[0];
169
        $getter = 'get'.ucfirst($identifier);
170
171
        return $entity->$getter();
172
    }
173
}
174