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.

EmbeddedRelation::isEmbeddedRequested()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Halapi\Relation;
4
5
use Doctrine\Common\Annotations\Reader;
6
use JMS\Serializer\SerializerBuilder;
7
use Symfony\Component\HttpFoundation\RequestStack;
8
9
/**
10
 * Class EmbeddedRelation.
11
 *
12
 * @author Romain Richard
13
 */
14
class EmbeddedRelation extends AbstractRelation implements RelationInterface
15
{
16
    /**
17
     * @var RequestStack
18
     */
19
    private $requestStack;
20
21
    /**
22
     * @var \JMS\Serializer\Serializer
23
     */
24
    private $serializer;
25
26
    /**
27
     * EmbeddedRelation constructor.
28
     *
29
     * @param Reader       $annotationReader
30
     * @param RequestStack $requestStack
31
     */
32
    public function __construct(
33
        Reader $annotationReader,
34
        RequestStack $requestStack
35
    ) {
36
        $this->annotationReader = $annotationReader;
37
        $this->requestStack = $requestStack;
38
        $this->serializer = SerializerBuilder::create()->build();
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getName()
45
    {
46
        return '_embedded';
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getRelation($resource)
53
    {
54
        $reflectionClass = new \ReflectionClass($resource);
55
        $embedded = [];
56
        $requestedEmbedded = $this->getEmbeddedParams();
57
58
        foreach ($reflectionClass->getProperties() as $property) {
59
            $propertyName = $property->getName();
60
61
            if ($this->isEmbeddable($property) && $this->isEmbeddedRequested($propertyName, $requestedEmbedded)) {
62
                $embedded[$property->getName()] = $this->getEmbeddedContent($resource, $property);
63
            }
64
        }
65
66
        return $embedded;
67
    }
68
69
    /**
70
     * @param $propertyName
71
     * @param $requestedEmbedded
72
     *
73
     * @return bool
74
     */
75
    private function isEmbeddedRequested($propertyName, $requestedEmbedded)
76
    {
77
        return in_array($propertyName, $requestedEmbedded);
78
    }
79
80
    /**
81
     * @param $resource
82
     * @param $property
83
     *
84
     * @return array
85
     */
86
    private function getEmbeddedContent($resource, $property)
87
    {
88
        $value = $resource->{'get'.ucfirst($property->getName())}();
89
90
        return $this->serializer->toArray($value);
91
    }
92
93
    /**
94
     * Get the embed query param.
95
     *
96
     * @return array
97
     */
98
    private function getEmbeddedParams()
99
    {
100
        $request = $this->requestStack->getMasterRequest();
101
102
        $embed = $request->get('embed');
103
104
        if (!is_array($embed)) {
105
            return [];
106
        }
107
108
        return $embed;
109
    }
110
}
111