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 ( d6a2b9...e5eea9 )
by
unknown
03:12
created

DropboxClient   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 176
Duplicated Lines 44.89 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 79
loc 176
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A move() 14 14 1
A copy() 14 14 1
A delete() 12 12 1
A createFolder() 0 16 1
A getMetadata() 12 12 1
A listContents() 13 13 1
A getTemporaryLink() 14 14 1
A getThumbnail() 0 16 1
A uploadFromString() 0 22 1
A normalizePath() 0 10 2

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
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 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...
92
    {
93
        $path = $this->normalizePath($path);
94
95
        $response = $this->client->post('files/get_metadata', [
96
            'json' => [
97
                'path' => $path,
98
            ]
99
        ]);
100
101
        return json_decode($response->getBody(), true);
102
    }
103
104 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...
105
    {
106
        $path = $this->normalizePath($path);
107
108
        $response = $this->client->post('files/list_folder', [
109
            'json' => [
110
                'path' => $path,
111
                'recursive' => $recursive,
112
            ]
113
        ]);
114
115
        return json_decode($response->getBody(), true);
116
    }
117
118 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...
119
    {
120
        $path = $this->normalizePath($path);
121
122
        $response = $this->client->post('files/get_temporary_link', [
123
            'json' => [
124
                'path' => $path,
125
            ],
126
        ]);
127
128
        $body = json_decode($response->getBody(), true);
129
130
        return $body['link'];
131
    }
132
133
    public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64'): string
134
    {
135
        $dropboxApiArguments = [
136
            'path' => $this->normalizePath($path),
137
            'format' => $format,
138
            'size' => $size
139
        ];
140
141
        $response = $this->client->post('https://content.dropboxapi.com/2/files/get_thumbnail', [
142
            'headers' => [
143
                'Dropbox-API-Arg' => json_encode($dropboxApiArguments),
144
            ],
145
        ]);
146
147
        return (string) $response->getBody();
148
    }
149
150
    public function uploadFromString(string $path, string $mode, string $contents)
151
    {
152
        $dropboxApiArguments = [
153
            'path' => $this->normalizePath($path),
154
            'mode' => $mode,
155
            'autorename' => true,
156
        ];
157
158
        $response = $this->client->post('https://content.dropboxapi.com/2/files/upload', [
159
            'headers' => [
160
                'Dropbox-API-Arg' => json_encode($dropboxApiArguments),
161
                'Content-Type' => 'application/octet-stream',
162
            ],
163
            'body' => $contents,
164
        ]);
165
166
        $metadata = json_decode($response->getBody(), true);
167
168
        $metadata['.tag'] = 'file';
169
170
        return $metadata;
171
    }
172
173
    public function normalizePath(string $path): string
174
    {
175
        $path = trim($path,'/');
176
177
        if ($path === '') {
178
            return '';
179
        }
180
181
        return '/'.$path;
182
    }
183
}
184