Completed
Push — master ( d5f261...ed671f )
by Joseph
01:44
created

Adapter   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 9
dl 0
loc 127
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A mapUserDetails() 0 7 1
A getQuota() 0 6 1
A upload() 0 14 3
B handleUploadException() 0 28 6
A checkUploadStatus() 0 15 3
A makeService() 0 11 1
1
<?php
2
3
namespace STS\StorageConnect\Drivers\Dropbox;
4
5
use Kunnu\Dropbox\Dropbox;
6
use Kunnu\Dropbox\DropboxApp;
7
use Kunnu\Dropbox\Exceptions\DropboxClientException;
8
use Kunnu\Dropbox\Models\FileMetadata;
9
use STS\StorageConnect\Drivers\AbstractAdapter;
10
use STS\StorageConnect\Exceptions\UploadException;
11
use STS\StorageConnect\Models\CloudStorage;
12
use STS\StorageConnect\Models\Quota;
13
use STS\StorageConnect\UploadRequest;
14
use STS\StorageConnect\UploadResponse;
15
16
class Adapter extends AbstractAdapter
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $driver = "dropbox";
22
23
    /**
24
     * @var string
25
     */
26
    protected $providerClass = Provider::class;
27
28
    /**
29
     * @param $user
30
     *
31
     * @return array
32
     */
33
    protected function mapUserDetails( $user )
34
    {
35
        return [
36
            'name'  => $user->user['name']['display_name'],
37
            'email' => $user->user['email']
38
        ];
39
    }
40
41
    /**
42
     * @return \STS\StorageConnect\Models\Quota
43
     */
44
    public function getQuota()
45
    {
46
        $usage = $this->service()->getSpaceUsage();
47
48
        return new Quota(array_get($usage, "allocation.allocated", 0), array_get($usage, "used", 0));
49
    }
50
51
    /**
52
     * @param UploadRequest $request
53
     *
54
     * @return UploadResponse
55
     *
56
     */
57
    public function upload( UploadRequest $request )
58
    {
59
        try {
60
            if (starts_with($request->getSourcePath(), "http")) {
61
                return new UploadResponse($request, $this->service()->saveUrl($request->getDestinationPath(), $request->getSourcePath()), true);
62
            }
63
64
            return new UploadResponse($request, $this->service()->upload(new File($request->getSourcePath()), $request->getDestinationPath(), [
65
                'mode' => 'overwrite'
66
            ]));
67
        } catch (DropboxClientException $e) {
68
            throw $this->handleUploadException($e, new UploadException($request, $e));
69
        }
70
    }
71
72
    /**
73
     * @param DropboxClientException $dropbox
74
     * @param UploadException        $upload
75
     *
76
     * @return UploadException
77
     */
78
    protected function handleUploadException( DropboxClientException $dropbox, UploadException $upload )
79
    {
80
        // First check for connection failure
81
        if (str_contains($dropbox->getMessage(), "Connection timed out")) {
82
            return $upload->retry("Connection timeout");
83
        }
84
85
        // See if we have a parseable error
86
        $error = json_decode($dropbox->getMessage(), true);
87
88
        if (!is_array($error)) {
89
            return $upload->retry("Unknown error uploading file to Dropbox: " . $dropbox->getMessage());
90
        }
91
92
        if (str_contains(array_get($error, 'error_summary'), "insufficient_space")) {
93
            return $upload->disable("Dropbox account is full", CloudStorage::SPACE_FULL);
94
        }
95
96
        if (str_contains(array_get($error, 'error_summary'), "invalid_access_token")) {
97
            return $upload->disable("Dropbox integration is invalid", CloudStorage::INVALID_TOKEN);
98
        }
99
100
        if (str_contains(array_get($error, 'error_summary'), 'too_many_write_operations')) {
101
            return $upload->retry("Hit rate limit");
102
        }
103
104
        return $upload->retry("Unknown Dropbox exception: " . $dropbox->getMessage());
105
    }
106
107
    /**
108
     * @param UploadResponse $response
109
     *
110
     * @return UploadResponse
111
     */
112
    public function checkUploadStatus( UploadResponse $response )
113
    {
114
        $result = $this->service()->checkJobStatus($response->getOriginal());
115
116
        if($response instanceof FileMetadata) {
117
            return new UploadResponse( $response->getRequest(), $result );
118
        }
119
120
        if($response == "in_progress") {
121
            $response->incrementStatusCheck();
122
            return $response;
123
        }
124
125
126
    }
127
128
    /**
129
     * @return Dropbox
130
     */
131
    protected function makeService()
132
    {
133
        $service = new Dropbox(
134
            new DropboxApp($this->config['client_id'], $this->config['client_secret']),
135
            ['random_string_generator' => 'openssl']
136
        );
137
138
        $service->setAccessToken(array_get($this->token, "access_token"));
139
140
        return $service;
141
    }
142
}