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 ( b6d48e...df54b5 )
by Romain
02:16
created

LinksRelation::getId()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 3
nop 2
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\Annotations\Reader;
8
use Halapi\Annotation\Embeddable;
9
use Halapi\ObjectManager\ObjectManagerInterface;
10
use Halapi\UrlGenerator\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 ObjectManagerInterface
26
     */
27
    private $objectManager;
28
29
    /**
30
     * @var ClassMetadata
31
     */
32
    private $classMetadata;
33
34
    /**
35
     * @var \ReflectionClass
36
     */
37
    private $reflectionClass;
38
39
    /**
40
     * AbstractRelation constructor.
41
     *
42
     * @param Reader                 $annotationReader
43
     * @param UrlGeneratorInterface  $urlGenerator
44
     * @param ObjectManagerInterface $objectManager
45
     */
46
    public function __construct(
47
        Reader $annotationReader,
48
        UrlGeneratorInterface $urlGenerator,
49
        ObjectManagerInterface $objectManager
50
    ) {
51
        $this->urlGenerator = $urlGenerator;
52
        $this->annotationReader = $annotationReader;
53
        $this->objectManager = $objectManager;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getName()
60
    {
61
        return '_links';
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getRelation($resource)
68
    {
69
        $this->classMetadata = $this->objectManager->getClassMetadata(get_class($resource));
70
        $this->reflectionClass = new \ReflectionClass($resource);
71
        $links = $this->getSelfLink($resource);
72
73
        foreach ($this->reflectionClass->getProperties() as $property) {
74
            if ($this->isEmbeddable($property) && $property->getName()) {
75
                $propertyName = $property->getName();
76
                $relationContent = $resource->{'get'.ucfirst($propertyName)}();
77
                if ($relationContent) {
78
                    $links[$propertyName] = $this->getRelationLinks($property, $relationContent);
79
                }
80
            }
81
        }
82
83
        return $links;
84
    }
85
86
    /**
87
     * @param \ReflectionProperty $property
88
     * @param object              $relationContent
89
     *
90
     * @return string|null
91
     *
92
     * @throws \Doctrine\ORM\Mapping\MappingException
93
     */
94
    private function getRelationLink(\ReflectionProperty $property, $relationContent)
95
    {
96
        if ($this->classMetadata->hasAssociation($property->getName())) {
97
            return $this->urlGenerator->generate(
98
                $this->getAssociationRouteName($property),
99
                [$this->objectManager->getIdentifierName($relationContent) => $this->objectManager->getIdentifier($relationContent)]
100
            );
101
        }
102
    }
103
104
    /**
105
     * Get the url of an entity.
106
     *
107
     * @param $resource
108
     *
109
     * @return array|null
110
     */
111
    private function getSelfLink($resource)
112
    {
113
        if ($resource instanceof \Traversable) {
114
            return;
115
        }
116
117
        return [
118
            'self' => $this->urlGenerator->generate(
119
                $this->getResourceRouteName($this->reflectionClass),
120
                [$this->objectManager->getIdentifierName($resource) => $this->objectManager->getIdentifier($resource)]
121
            ),
122
        ];
123
    }
124
125
    /**
126
     * Get the links of a collection.
127
     *
128
     * @param \ReflectionProperty $property
129
     * @param $relationContent
130
     *
131
     * @return array|void
132
     */
133
    private function getRelationLinks(\ReflectionProperty $property, $relationContent)
134
    {
135
        if ($relationContent instanceof Collection) {
0 ignored issues
show
Bug introduced by
The class Doctrine\Common\Collections\Collection does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
136
            $links = [];
137
            foreach ($relationContent as $relation) {
138
                $links[] = $this->getRelationLink($property, $relation);
139
            }
140
141
            return $links;
142
        }
143
144
        return $this->getRelationLink($property, $relationContent);
145
    }
146
147
    /**
148
     * Return the configured route name for an embeddable relation.
149
     *
150
     * @param \ReflectionProperty $property
151
     *
152
     * @return string
153
     */
154
    private function getAssociationRouteName(\ReflectionProperty $property)
155
    {
156
        if ($routeName = $this->annotationReader->getPropertyAnnotation($property, Embeddable::class)->getRouteName()) {
157
            return $routeName;
158
        }
159
160
        return $this->getResourceRouteName(new \ReflectionClass(
161
            $this->classMetadata->getAssociationTargetClass($property->getName())
162
        ));
163
    }
164
165
    /**
166
     * Return the configured route name for a resource, or get_*entityShortName* by default.
167
     *
168
     * @param \ReflectionClass $resource
169
     *
170
     * @return string
171
     */
172
    private function getResourceRouteName(\ReflectionClass $resource)
173
    {
174
        if ($routeName = $this->annotationReader->getClassAnnotation($resource, Embeddable::class)->getRouteName()) {
175
            return $routeName;
176
        }
177
178
        return 'get_'.strtolower($resource->getShortName());
179
    }
180
}
181