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 ( 80cafa...d6a2b9 )
by
unknown
03:01
created

DropboxClient::createFolder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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