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 ( 15ba7e...2cb5e8 )
by Romain
01:58
created

src/Halapi/Relation/LinksRelation.php (1 issue)

Labels
Severity

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
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
     */
93
    private function getRelationLink(\ReflectionProperty $property, $relationContent)
94
    {
95
        if ($this->classMetadata->hasAssociation($property->getName())) {
96
            return $this->urlGenerator->generate(
97
                $this->getAssociationRouteName($property),
98
                [$this->objectManager->getIdentifierName($relationContent) => $this->objectManager->getIdentifier($relationContent)]
99
            );
100
        }
101
    }
102
103
    /**
104
     * Get the url of an entity.
105
     *
106
     * @param $resource
107
     *
108
     * @return array|null
109
     */
110
    private function getSelfLink($resource)
111
    {
112
        if ($resource instanceof \Traversable) {
113
            return;
114
        }
115
116
        return [
117
            'self' => $this->urlGenerator->generate(
118
                $this->getResourceRouteName($this->reflectionClass),
119
                [$this->objectManager->getIdentifierName($resource) => $this->objectManager->getIdentifier($resource)]
120
            ),
121
        ];
122
    }
123
124
    /**
125
     * Get the links of a collection.
126
     *
127
     * @param \ReflectionProperty $property
128
     * @param $relationContent
129
     *
130
     * @return array|void
131
     */
132
    private function getRelationLinks(\ReflectionProperty $property, $relationContent)
133
    {
134
        if ($relationContent instanceof Collection) {
0 ignored issues
show
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...
135
            $links = [];
136
            foreach ($relationContent as $relation) {
137
                $links[] = $this->getRelationLink($property, $relation);
138
            }
139
140
            return $links;
141
        }
142
143
        return $this->getRelationLink($property, $relationContent);
144
    }
145
146
    /**
147
     * Return the configured route name for an embeddable relation.
148
     *
149
     * @param \ReflectionProperty $property
150
     *
151
     * @return string
152
     */
153
    private function getAssociationRouteName(\ReflectionProperty $property)
154
    {
155
        if ($routeName = $this->annotationReader->getPropertyAnnotation($property, Embeddable::class)->getRouteName()) {
156
            return $routeName;
157
        }
158
159
        return $this->getResourceRouteName(new \ReflectionClass(
160
            $this->classMetadata->getAssociationTargetClass($property->getName())
161
        ));
162
    }
163
164
    /**
165
     * Return the configured route name for a resource, or get_*entityShortName* by default.
166
     *
167
     * @param \ReflectionClass $resource
168
     *
169
     * @return string
170
     */
171
    private function getResourceRouteName(\ReflectionClass $resource)
172
    {
173
        if ($routeName = $this->annotationReader->getClassAnnotation($resource, Embeddable::class)->getRouteName()) {
174
            return $routeName;
175
        }
176
177
        return 'get_'.strtolower($resource->getShortName());
178
    }
179
}
180