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:51
created

Image   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 138
Duplicated Lines 10.87 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 15
loc 138
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A retrieve() 0 5 1
A delete() 0 4 1
A getMetadata() 0 5 1
A resetMetadata() 0 5 1
A mergeMetadata() 0 5 1
A getMetadataItem() 7 7 1
A deleteMetadataItem() 8 8 2
A parseMetadata() 0 4 1
A getAliases() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php declare(strict_types=1);
2
3
namespace OpenStack\Compute\v2\Models;
4
5
use OpenStack\Common\Resource\Alias;
6
use OpenStack\Common\Resource\OperatorResource;
7
use OpenStack\Common\Resource\Deletable;
8
use OpenStack\Common\Resource\HasMetadata;
9
use OpenStack\Common\Resource\Listable;
10
use OpenStack\Common\Resource\Retrievable;
11
use OpenStack\Common\Transport\Utils;
12
use Psr\Http\Message\ResponseInterface;
13
14
/**
15
 * Represents a Compute v2 Image
16
 *
17
 * @property \OpenStack\Compute\v2\Api $api
18
 */
19
class Image extends OperatorResource implements Listable, Retrievable, Deletable, HasMetadata
20
{
21
    /** @var string */
22
    public $id;
23
24
    /** @var array */
25
    public $links;
26
27
    /** @var array */
28
    public $metadata;
29
30
    /** @var int */
31
    public $minDisk;
32
33
    /** @var int */
34
    public $minRam;
35
36
    /** @var string */
37
    public $name;
38
39
    /** @var string */
40
    public $progress;
41
42
    /** @var string */
43
    public $status;
44
45
    /** @var \DateTimeImmutable */
46
    public $created;
47
48
    /** @var \DateTimeImmutable */
49
    public $updated;
50
51
    /**
52
     * @var array
53
     */
54 1
    protected $aliases = [];
55
56 1
    protected $resourceKey = 'image';
57 1
    protected $resourcesKey = 'images';
58 1
59
    /**
60
     * @inheritdoc
61
     */
62
    protected function getAliases(): array
63 1
    {
64
        $aliases = parent::getAliases();
65 1
        $aliases['created'] = new Alias('created', \DateTimeImmutable::class);
66 1
        $aliases['updated'] = new Alias('updated', \DateTimeImmutable::class);
67
        return $aliases;
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73 1
    public function retrieve()
74
    {
75 1
        $response = $this->execute($this->api->getImage(), ['id' => (string)$this->id]);
76 1
        $this->populateFromResponse($response);
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     */
82
    public function delete()
83
    {
84
        $this->execute($this->api->deleteImage(), ['id' => (string)$this->id]);
85
    }
86
87 1
    /**
88
     * Retrieves metadata from the API.
89 1
     *
90 1
     * @return array
91
     */
92
    public function getMetadata(): array
93
    {
94
        $response = $this->execute($this->api->getImageMetadata(), ['id' => $this->id]);
95
        return $this->parseMetadata($response);
96
    }
97
98
    /**
99
     * Resets all the metadata for this image with the values provided. All existing metadata keys
100
     * will either be replaced or removed.
101
     *
102 1
     * @param array $metadata {@see \OpenStack\Compute\v2\Api::putImageMetadata}
103
     */
104 1
    public function resetMetadata(array $metadata)
105 1
    {
106
        $response = $this->execute($this->api->putImageMetadata(), ['id' => $this->id, 'metadata' => $metadata]);
107
        $this->metadata = $this->parseMetadata($response);
108
    }
109
110
    /**
111
     * Merges the existing metadata for the image with the values provided. Any existing keys
112
     * referenced in the user options will be replaced with the user's new values. All other
113
     * existing keys will remain unaffected.
114
     *
115 1
     * @param array $metadata {@see \OpenStack\Compute\v2\Api::postImageMetadata}
116
     */
117 1
    public function mergeMetadata(array $metadata)
118 1
    {
119
        $response = $this->execute($this->api->postImageMetadata(), ['id' => $this->id, 'metadata' => $metadata]);
120
        $this->metadata = $this->parseMetadata($response);
121
    }
122
123
    /**
124
     * Retrieve the value for a specific metadata key.
125
     *
126 1
     * @param string $key {@see \OpenStack\Compute\v2\Api::getImageMetadataKey}
127
     *
128 1
     * @return mixed
129 1
     */
130 View Code Duplication
    public function getMetadataItem(string $key)
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...
131
    {
132
        $response = $this->execute($this->api->getImageMetadataKey(), ['id' => $this->id, 'key' => $key]);
133
        $value = $this->parseMetadata($response)[$key];
134
        $this->metadata[$key] = $value;
135
        return $value;
136
    }
137
138
    /**
139
     * Remove a specific metadata key.
140
     *
141
     * @param string $key {@see \OpenStack\Compute\v2\Api::deleteImageMetadataKey}
142
     */
143 View Code Duplication
    public function deleteMetadataItem(string $key)
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...
144
    {
145
        if (isset($this->metadata[$key])) {
146
            unset($this->metadata[$key]);
147
        }
148
149
        $this->execute($this->api->deleteImageMetadataKey(), ['id' => $this->id, 'key' => $key]);
150
    }
151
152
    public function parseMetadata(ResponseInterface $response): array
153
    {
154
        return Utils::jsonDecode($response)['metadata'];
155
    }
156
}
157