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 (#134)
by Ha
01:47
created

Object::populateHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
2
3
namespace OpenStack\ObjectStore\v1\Models;
4
5
use GuzzleHttp\Psr7\Uri;
6
use OpenStack\Common\Transport\Utils;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\StreamInterface;
9
use OpenStack\Common\Resource\OperatorResource;
10
use OpenStack\Common\Resource\Creatable;
11
use OpenStack\Common\Resource\Deletable;
12
use OpenStack\Common\Resource\HasMetadata;
13
14
/**
15
 * @property \OpenStack\ObjectStore\v1\Api $api
16
 */
17
class Object extends OperatorResource implements Creatable, Deletable, HasMetadata
18
{
19
    use MetadataTrait;
20
21
    const METADATA_PREFIX = 'X-Object-Meta-';
22
23
    /** @var string */
24
    public $containerName;
25
26
    /** @var string */
27
    public $name;
28
29
    /** @var string */
30
    public $hash;
31
32
    /** @var string */
33
    public $contentType;
34
35
    /** @var int */
36
    public $contentLength;
37
38
    /** @var string */
39
    public $lastModified;
40
41
    /** @var array */
42
    public $metadata;
43
44
    protected $markerKey = 'name';
45
    protected $aliases = ['bytes' => 'contentLength'];
46
47
    /**
48
     * {@inheritdoc}
49 5
     */
50
    public function populateFromResponse(ResponseInterface $response): self
51 5
    {
52
        parent::populateFromResponse($response);
53 5
54 5
        $this->populateHeaders($response);
55 5
56 5
        return $this;
57 5
    }
58 5
59
    /**
60
     * @param ResponseInterface $response
61
     *
62
     * @return $this
63
     */
64 View Code Duplication
    private function populateHeaders(ResponseInterface $response): self
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...
65 1
    {
66
        $this->hash = $response->getHeaderLine('ETag');
67 1
        $this->contentLength = $response->getHeaderLine('Content-Length');
0 ignored issues
show
Documentation Bug introduced by
The property $contentLength was declared of type integer, but $response->getHeaderLine('Content-Length') is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
68
        $this->lastModified = $response->getHeaderLine('Last-Modified');
69
        $this->contentType = $response->getHeaderLine('Content-Type');
70
        $this->metadata = $this->parseMetadata($response);
71
72
        return $this;
73
    }
74
75 3
    /**
76
     * Retrieves the public URI for this resource.
77 3
     *
78 3
     * @return \GuzzleHttp\Psr7\Uri
79
     */
80
    public function getPublicUri(): Uri
81
    {
82
        return Utils::addPaths($this->getHttpBaseUrl(), $this->containerName, $this->name);
83
    }
84 4
85
    /**
86 4
     * @param array $data {@see \OpenStack\ObjectStore\v1\Api::putObject}
87 2
     *
88 2
     * @return $this
89
     */
90
    public function create(array $data): Creatable
91
    {
92
        $response = $this->execute($this->api->putObject(), $data + ['containerName' => $this->containerName]);
93
        return $this->populateFromResponse($response);
94
    }
95
96
    /**
97
     * {@inheritdoc}
98 1
     */
99
    public function retrieve()
100 1
    {
101 1
        $response = $this->executeWithState($this->api->headObject());
102
        $this->populateFromResponse($response);
103
    }
104
105
    /**
106
     * This call will perform a `GET` HTTP request for the given object and return back its content in the form of a
107 1
     * Guzzle Stream object. Downloading an object will transfer all of the content for an object, and is therefore
108
     * distinct from fetching its metadata (a `HEAD` request). The body of an object is not fetched by default to
109 1
     * improve performance when handling large objects.
110 1
     *
111
     * @return StreamInterface
112
     */
113
    public function download(): StreamInterface
114
    {
115 1
        /** @var ResponseInterface $response */
116
        $response = $this->executeWithState($this->api->getObject());
117 1
        $this->populateHeaders($response);
118 1
        return $response->getBody();
119 1
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 1
    public function delete()
125
    {
126
        $this->executeWithState($this->api->deleteObject());
127 1
    }
128 1
129 1
    /**
130 1
     * @param array $options {@see \OpenStack\ObjectStore\v1\Api::copyObject}
131
     */
132 1
    public function copy(array $options)
133 1
    {
134
        $options += ['name' => $this->name, 'containerName' => $this->containerName];
135
        $this->execute($this->api->copyObject(), $options);
136
    }
137
138
    /**
139 1
     * {@inheritdoc}
140
     */
141 View Code Duplication
    public function mergeMetadata(array $metadata)
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...
142 1
    {
143 1
        $options = [
144 1
            'containerName' => $this->containerName,
145 1
            'name'          => $this->name,
146
            'metadata'      => array_merge($metadata, $this->getMetadata()),
147 1
        ];
148 1
149
        $response = $this->execute($this->api->postObject(), $options);
150
        $this->metadata = $this->parseMetadata($response);
151
    }
152
153
    /**
154 2
     * {@inheritdoc}
155
     */
156 2 View Code Duplication
    public function resetMetadata(array $metadata)
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...
157 2
    {
158
        $options = [
159
            'containerName'  => $this->containerName,
160
            'name'           => $this->name,
161
            'metadata'       => $metadata,
162
        ];
163
164
        $response = $this->execute($this->api->postObject(), $options);
165
        $this->metadata = $this->parseMetadata($response);
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    public function getMetadata(): array
172
    {
173
        $response = $this->executeWithState($this->api->headObject());
174
        return $this->parseMetadata($response);
175
    }
176
}
177