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
Pull Request — master (#159)
by Roman
02:02
created

AbstractResource::isNotNativeType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
2
3
namespace OpenStack\Common\Resource;
4
5
use OpenStack\Common\Transport\Serializable;
6
use OpenStack\Common\Transport\Utils;
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * Represents a top-level abstraction of a remote API resource. Usually a resource represents a discrete
11
 * entity such as a Server, Container, Load Balancer. Apart from a representation of state, a resource can
12
 * also execute RESTFul operations on itself (updating, deleting, listing) or on other models.
13
 *
14
 * @package OpenStack\Common\Resource
15
 */
16
abstract class AbstractResource implements ResourceInterface, Serializable
17
{
18
    /**
19
     * The JSON key that indicates how the API nests singular resources. For example, when
20
     * performing a GET, it could respond with ``{"server": {"id": "12345"}}``. In this case,
21
     * "server" is the resource key, since the essential state of the server is nested inside.
22
     *
23
     * @var string
24
     */
25
    protected $resourceKey;
26
27
    /**
28
     * An array of aliases that will be checked when the resource is being populated. For example,
29
     *
30
     * 'FOO_BAR' => 'fooBar'
31
     *
32
     * will extract FOO_BAR from the response, and save it as 'fooBar' in the resource.
33
     *
34
     * @var array
35
     */
36
    protected $aliases = [];
37
38
    /**
39
     * Populates the current resource from a response object.
40
     *
41
     * @param ResponseInterface $response
42
     *
43
     * @return AbstractResource
44
     */
45
    public function populateFromResponse(ResponseInterface $response): self
46
    {
47
        if (strpos($response->getHeaderLine('Content-Type'), 'application/json') === 0) {
48
            $json = Utils::jsonDecode($response);
49
            if (!empty($json)) {
50
                $this->populateFromArray(Utils::flattenJson($json, $this->resourceKey));
51
            }
52
        }
53
54
        return $this;
55
    }
56
57
    /**
58
     * Populates the current resource from a data array.
59
     *
60
     * @param array $array
61
     *
62
     * @return mixed|void
63 74
     */
64
    public function populateFromArray(array $array): self
65 74
    {
66 61
        $aliases = $this->getAliases();
67 61
68 61
        foreach ($array as $key => $val) {
69 61
            $alias = $aliases[$key] ?? false;
70 61
71
            if ($alias instanceof Alias) {
72 74
                $alias = $aliases[$key];
73
74
                $key = $alias->getName();
75
                $val = $alias->getValue($this, $val);
76
            }
77
78
            if (property_exists($this, $key)) {
79
                $this->{$key} = $val;
80
            }
81
        }
82 133
83
        return $this;
84 133
    }
85
86 133
    /**
87 133
     * Constructs alias objects
88
     *
89 133
     * @return Alias[]
90 131
     */
91 128
    protected function getAliases()
92 128
    {
93
        $aliases = [];
94 131
95 131
        foreach ((array)$this->aliases as $alias => $property) {
96 133
            $aliases[$alias] = new Alias($property);
97 133
        }
98
99 128
        return $aliases;
100 2
    }
101 128
102 6
    /**
103 6
     * Internal method which retrieves the values of provided keys.
104 6
     *
105 6
     * @param array $keys
106 6
     *
107 128
     * @return array
108 28
     */
109 127
    protected function getAttrs(array $keys)
110 11
    {
111 11
        $output = [];
112
113 128
        foreach ($keys as $key) {
114
            if (property_exists($this, $key) && $this->$key !== null) {
115
                $output[$key] = $this->$key;
116 124
            }
117
        }
118 124
119 124
        return $output;
120 124
    }
121
122 View Code Duplication
    public function model(string $class, $data = null): ResourceInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123 12
    {
124
        $model = new $class();
125 12
126 12
        // @codeCoverageIgnoreStart
127 12
        if (!$model instanceof ResourceInterface) {
128 12
            throw new \RuntimeException(sprintf('%s does not implement %s', $class, ResourceInterface::class));
129
        }
130 12
        // @codeCoverageIgnoreEnd
131
132
        if ($data instanceof ResponseInterface) {
133 131
            $model->populateFromResponse($data);
134
        } elseif (is_array($data)) {
135 131
            $model->populateFromArray($data);
136
        }
137 131
138 3
        return $model;
139
    }
140
141 128
    public function serialize(): \stdClass
142 128
    {
143 128
        $output = new \stdClass();
144
145
        foreach ((new \ReflectionClass($this))->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
146
            $name = $property->getName();
147
            $val = $this->{$name};
148
149
            $fn = function ($val) {
150
                return ($val instanceof Serializable) ? $val->serialize() : $val;
151
            };
152
153 65
            if (is_array($val)) {
154
                foreach ($val as $sk => $sv) {
155 65
                    $val[$sk] = $fn($sv);
156
                }
157 65
            }
158 65
159 65
            $output->{$name} = $fn($val);
160 65
        }
161 65
162
        return $output;
163 65
    }
164
}
165