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
Push — master ( b0347f...7545ff )
by
unknown
04:35
created

DropboxClient::getFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
3
namespace Spatie\FlysystemDropbox;
4
5
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Psr7\StreamWrapper;
8
use Psr\Http\Message\StreamInterface;
9
10
class DropboxClient
11
{
12
    protected $accessToken;
13
14
    protected $client;
15
16
    public function __construct(string $accessToken)
17
    {
18
        $this->accessToken = $accessToken;
19
20
        $this->client = new Client([
21
            'base_uri' => 'https://api.dropboxapi.com/2/',
22
            'headers' => [
23
                'Authorization' => "Bearer {$this->accessToken}",
24
            ],
25
        ]);
26
    }
27
28 View Code Duplication
    public function move(string $path, string $newPath): array
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...
29
    {
30
        $path = $this->normalizePath($path);
31
        $newPath = $this->normalizePath($newPath);
32
33
        $response = $this->client->post('files/move', [
34
            'json' => [
35
                'from_path' => $path,
36
                'to_path' => $newPath,
37
            ]
38
        ]);
39
40
        return json_decode($response->getBody(), true);
41
    }
42
43 View Code Duplication
    public function copy(string $path, string $newPath): array
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...
44
    {
45
        $path = $this->normalizePath($path);
46
        $newPath = $this->normalizePath($newPath);
47
48
        $response = $this->client->post('files/copy', [
49
            'json' => [
50
                'from_path' => $path,
51
                'to_path' => $newPath,
52
            ]
53
        ]);
54
55
        return json_decode($response->getBody(), true);
56
    }
57
58 View Code Duplication
    public function delete(string $path): array
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...
59
    {
60
        $path = $this->normalizePath($path);
61
62
        $response = $this->client->post('files/delete', [
63
            'json' => [
64
                'path' => $path,
65
            ]
66
        ]);
67
68
        return json_decode($response->getBody(), true);
69
    }
70
71
    /**
72
     * @param string $path
73
     *
74
     * @return mixed
75
     */
76
    public function createFolder(string $path)
77
    {
78
        $path = $this->normalizePath($path);
79
80
        $response = $this->client->post('files/create_folder', [
81
            'json' => [
82
                'path' => $path,
83
            ]
84
        ]);
85
86
        $result = json_decode($response->getBody(), true);
87
88
        $result['.tag'] = 'folder';
89
90
        return $result;
91
    }
92
93 View Code Duplication
    public function getMetadata(string $path)
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...
94
    {
95
        $path = $this->normalizePath($path);
96
97
        $response = $this->client->post('files/get_metadata', [
98
            'json' => [
99
                'path' => $path,
100
            ]
101
        ]);
102
103
        return json_decode($response->getBody(), true);
104
    }
105
106
    public function getFile(string $path)
107
    {
108
        $dropboxApiArguments = [
109
            'path' => $this->normalizePath($path),
110
        ];
111
112
        $response = $this->client->post('https://content.dropboxapi.com/2/files/download', [
113
            'headers' => [
114
                'Dropbox-API-Arg' => json_encode($dropboxApiArguments),
115
            ],
116
        ]);
117
118
        return StreamWrapper::getResource($response->getBody());
119
    }
120
121 View Code Duplication
    public function listContents($path = '', $recursive = false): array
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...
122
    {
123
        $path = $this->normalizePath($path);
124
125
        $response = $this->client->post('files/list_folder', [
126
            'json' => [
127
                'path' => $path,
128
                'recursive' => $recursive,
129
            ]
130
        ]);
131
132
        return json_decode($response->getBody(), true);
133
    }
134
135 View Code Duplication
    public function getTemporaryLink(string $path): string
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...
136
    {
137
        $path = $this->normalizePath($path);
138
139
        $response = $this->client->post('files/get_temporary_link', [
140
            'json' => [
141
                'path' => $path,
142
            ],
143
        ]);
144
145
        $body = json_decode($response->getBody(), true);
146
147
        return $body['link'];
148
    }
149
150
    public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64'): string
151
    {
152
        $dropboxApiArguments = [
153
            'path' => $this->normalizePath($path),
154
            'format' => $format,
155
            'size' => $size
156
        ];
157
158
        $response = $this->client->post('https://content.dropboxapi.com/2/files/get_thumbnail', [
159
            'headers' => [
160
                'Dropbox-API-Arg' => json_encode($dropboxApiArguments),
161
            ],
162
        ]);
163
164
        return (string) $response->getBody();
165
    }
166
167
    public function uploadFromString(string $path, string $mode, string $contents)
168
    {
169
        $dropboxApiArguments = [
170
            'path' => $this->normalizePath($path),
171
            'mode' => $mode,
172
            'autorename' => true,
173
        ];
174
175
        $response = $this->client->post('https://content.dropboxapi.com/2/files/upload', [
176
            'headers' => [
177
                'Dropbox-API-Arg' => json_encode($dropboxApiArguments),
178
                'Content-Type' => 'application/octet-stream',
179
            ],
180
            'body' => $contents,
181
        ]);
182
183
        $metadata = json_decode($response->getBody(), true);
184
185
        $metadata['.tag'] = 'file';
186
187
        return $metadata;
188
    }
189
190
    public function normalizePath(string $path): string
191
    {
192
        $path = trim($path,'/');
193
194
        if ($path === '') {
195
            return '';
196
        }
197
198
        return '/'.$path;
199
    }
200
}
201