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
01:49
created

AbstractResource::populateFromArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
ccs 5
cts 5
cp 1
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php declare(strict_types=1);
2
3
namespace OpenStack\Common\Resource;
4
5
use OpenStack\Common\Resource\Alias;
6
use OpenStack\Common\Transport\Serializable;
7
use OpenStack\Common\Transport\Utils;
8
use Psr\Http\Message\ResponseInterface;
9
10
/**
11
 * Represents a top-level abstraction of a remote API resource. Usually a resource represents a discrete
12
 * entity such as a Server, Container, Load Balancer. Apart from a representation of state, a resource can
13
 * also execute RESTFul operations on itself (updating, deleting, listing) or on other models.
14
 *
15
 * @package OpenStack\Common\Resource
16
 */
17
abstract class AbstractResource implements ResourceInterface, Serializable
18
{
19
    /**
20
     * The JSON key that indicates how the API nests singular resources. For example, when
21
     * performing a GET, it could respond with ``{"server": {"id": "12345"}}``. In this case,
22
     * "server" is the resource key, since the essential state of the server is nested inside.
23
     *
24
     * @var string
25
     */
26
    protected $resourceKey;
27
28
    /**
29
     * @var Alias
30
     */
31
    private static $_alias;
32
33
    /**
34
     * Populates the current resource from a response object.
35
     *
36
     * @param ResponseInterface $response
37
     *
38
     * @return AbstractResource
39
     */
40
    public function populateFromResponse(ResponseInterface $response): self
41
    {
42
        if (strpos($response->getHeaderLine('Content-Type'), 'application/json') === 0) {
43
            $json = Utils::jsonDecode($response);
44
            if (!empty($json)) {
45
                $this->populateFromArray(Utils::flattenJson($json, $this->resourceKey));
46
            }
47
        }
48
49
        return $this;
50
    }
51
52
    /**
53
     * Populates the current resource from a data array.
54
     *
55
     * @param array $array
56
     *
57
     * @return mixed|void
58
     */
59
    public function populateFromArray(array $array): self
60
    {
61
        $alias = static::getAlias();
62
63 74
        foreach ($array as $key => $val) {
64
            $alias->setObjectProperty($this, $key, $val);
65 74
        }
66 61
67 61
        return $this;
68 61
    }
69 61
70 61
    /**
71
     * Provides an object for aliases configuration
72 74
     *
73
     * @return Alias
74
     */
75
    protected static function getAlias(): Alias
76
    {
77
        if (self::$_alias === null) {
78
            self::$_alias = new Alias();
79
        }
80
81
        return self::$_alias;
82 133
    }
83
84 133
    /**
85
     * Internal method which retrieves the values of provided keys.
86 133
     *
87 133
     * @param array $keys
88
     *
89 133
     * @return array
90 131
     */
91 128
    protected function getAttrs(array $keys)
92 128
    {
93
        $output = [];
94 131
95 131
        foreach ($keys as $key) {
96 133
            if (property_exists($this, $key) && $this->$key !== null) {
97 133
                $output[$key] = $this->$key;
98
            }
99 128
        }
100 2
101 128
        return $output;
102 6
    }
103 6
104 6 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...
105 6
    {
106 6
        $model = new $class();
107 128
108 28
        // @codeCoverageIgnoreStart
109 127
        if (!$model instanceof ResourceInterface) {
110 11
            throw new \RuntimeException(sprintf('%s does not implement %s', $class, ResourceInterface::class));
111 11
        }
112
        // @codeCoverageIgnoreEnd
113 128
114
        if ($data instanceof ResponseInterface) {
115
            $model->populateFromResponse($data);
116 124
        } elseif (is_array($data)) {
117
            $model->populateFromArray($data);
118 124
        }
119 124
120 124
        return $model;
121
    }
122
123 12
    public function serialize(): \stdClass
124
    {
125 12
        $output = new \stdClass();
126 12
127 12
        foreach ((new \ReflectionClass($this))->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
128 12
            $name = $property->getName();
129
            $val = $this->{$name};
130 12
131
            $fn = function ($val) {
132
                return ($val instanceof Serializable) ? $val->serialize() : $val;
133 131
            };
134
135 131
            if (is_array($val)) {
136
                foreach ($val as $sk => $sv) {
137 131
                    $val[$sk] = $fn($sv);
138 3
                }
139
            }
140
141 128
            $output->{$name} = $fn($val);
142 128
        }
143 128
144
        return $output;
145
    }
146
}
147