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.

LinksRelation::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
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) {
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