Passed
Pull Request — master (#132)
by Arman
02:54
created

DropboxFileSystemAdapter::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
1
<?php
2
/**
3
 * Quantum PHP Framework
4
 *
5
 * An open source software development framework for PHP
6
 *
7
 * @package Quantum
8
 * @author Arman Ag. <[email protected]>
9
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
10
 * @link http://quantum.softberg.org/
11
 * @since 2.9.0
12
 */
13
14
namespace Quantum\Libraries\Storage\Adapters\Dropbox;
15
16
use Quantum\Libraries\Storage\FilesystemAdapterInterface;
17
use Exception;
18
19
/**
20
 * Class DropboxFileSystemAdapter
21
 * @package Quantum\Libraries\Storage
22
 */
23
class DropboxFileSystemAdapter implements FilesystemAdapterInterface
24
{
25
26
    /**
27
     * @var DropboxFileSystemAdapter|null
28
     */
29
    private static $instance = null;
30
31
    /**
32
     * @var DropboxApp
33
     */
34
    private $dropboxApp;
35
36
    /**
37
     * DropboxFileSystemAdapter constructor
38
     * @param DropboxApp $dropboxApp
39
     */
40
    private function __construct(DropboxApp $dropboxApp)
41
    {
42
        $this->dropboxApp = $dropboxApp;
43
    }
44
45
    /**
46
     * Get Instance
47
     * @param DropboxApp $dropboxApp
48
     * @return DropboxFileSystemAdapter
49
     */
50
    public static function getInstance(DropboxApp $dropboxApp): DropboxFileSystemAdapter
51
    {
52
        if (self::$instance === null) {
53
            self::$instance = new self($dropboxApp);
54
        }
55
56
        return self::$instance;
57
    }
58
59
    /**
60
     * @inheritDoc
61
     */
62
    public function makeDirectory(string $dirname): bool
63
    {
64
        try {
65
            $this->dropboxApp->rpcRequest(DropboxApp::ENDPOINT_CREATE_FOLDER, $this->dropboxApp->path($dirname));
66
            return true;
67
        } catch (Exception $e) {
68
            return false;
69
        }
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75
    public function removeDirectory(string $dirname): bool
76
    {
77
        try {
78
            $this->dropboxApp->rpcRequest(DropboxApp::ENDPOINT_DELETE_FILE, $this->dropboxApp->path($dirname));
79
            return true;
80
        } catch (Exception $e) {
81
            return false;
82
        }
83
    }
84
85
    /**
86
     * @inheritDoc
87
     */
88
    public function get(string $filename)
89
    {
90
        try {
91
            return (string)$this->dropboxApp->contentRequest(DropboxApp::ENDPOINT_DOWNLOAD_FILE, $this->dropboxApp->path($filename));
92
        } catch (Exception $e) {
93
            return false;
94
        }
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100
    public function put(string $filename, string $content)
101
    {
102
        try {
103
            $response = $this->dropboxApp->contentRequest(DropboxApp::ENDPOINT_UPLOAD_FILE,
104
                ['path' => '/' . $filename, 'mode' => 'overwrite'], $content);
105
106
            return $response->size;
107
108
        } catch (Exception $e) {
109
            return false;
110
        }
111
    }
112
113
    /**
114
     * @inheritDoc
115
     */
116
    public function append(string $filename, string $content)
117
    {
118
        $fileContent = $this->get($filename);
119
120
        return $this->put($filename, $fileContent . $content);
0 ignored issues
show
Bug introduced by
Are you sure $fileContent of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

120
        return $this->put($filename, /** @scrutinizer ignore-type */ $fileContent . $content);
Loading history...
121
    }
122
123
    /**
124
     * @inheritDoc
125
     */
126
    public function rename(string $oldName, string $newName): bool
127
    {
128
        try {
129
            $this->dropboxApp->rpcRequest(DropboxApp::ENDPOINT_MOVE_FILE, [
130
                'from_path' => $this->dropboxApp->path($oldName),
131
                'to_path' => $this->dropboxApp->path($newName)
132
            ]);
133
134
            return true;
135
        } catch (Exception $e) {
136
            return false;
137
        }
138
    }
139
140
    /**
141
     * @inheritDoc
142
     */
143
    public function copy(string $source, string $dest): bool
144
    {
145
        try {
146
            $this->dropboxApp->rpcRequest(DropboxApp::ENDPOINT_COPY_FILE, [
147
                'from_path' => $this->dropboxApp->path($source),
148
                'to_path' => $this->dropboxApp->path($dest),
149
            ]);
150
151
            return true;
152
        } catch (Exception $e) {
153
            return false;
154
        }
155
    }
156
157
    /**
158
     * @inheritDoc
159
     */
160
    public function exists(string $filename): bool
161
    {
162
        return $this->isFile($filename);
163
    }
164
165
    /**
166
     * @inheritDoc
167
     */
168
    public function size(string $filename)
169
    {
170
        try {
171
            $meta = (array)$this->dropboxApp->rpcRequest(DropboxApp::ENDPOINT_FILE_METADATA, $this->dropboxApp->path($filename));
172
            return $meta['size'];
173
        } catch (Exception $e) {
174
            return false;
175
        }
176
    }
177
178
    /**
179
     * @inheritDoc
180
     */
181
    public function lastModified(string $filename)
182
    {
183
        try {
184
            $meta = (array)$this->dropboxApp->rpcRequest(DropboxApp::ENDPOINT_FILE_METADATA, $this->dropboxApp->path($filename));
185
            return isset($meta['server_modified']) ? strtotime($meta['server_modified']) : false;
186
        } catch (Exception $e) {
187
            return false;
188
        }
189
190
    }
191
192
    /**
193
     * @inheritDoc
194
     */
195
    public function remove(string $filename): bool
196
    {
197
        try {
198
            $this->dropboxApp->rpcRequest(DropboxApp::ENDPOINT_DELETE_FILE, $this->dropboxApp->path($filename));
199
            return true;
200
        } catch (Exception $e) {
201
            return false;
202
        }
203
204
    }
205
206
    /**
207
     * @inheritDoc
208
     */
209
    public function isFile(string $filename): bool
210
    {
211
        try {
212
            $meta = (array)$this->dropboxApp->rpcRequest(DropboxApp::ENDPOINT_FILE_METADATA, $this->dropboxApp->path($filename));
213
            return $meta['.tag'] == 'file';
214
        } catch (Exception $e) {
215
            return false;
216
        }
217
218
    }
219
220
    /**
221
     * @inheritDoc
222
     */
223
    public function isDirectory(string $dirname): bool
224
    {
225
        try {
226
            $meta = (array)$this->dropboxApp->rpcRequest(DropboxApp::ENDPOINT_FILE_METADATA, $this->dropboxApp->path($dirname));
227
            return $meta['.tag'] == 'folder';
228
        } catch (Exception $e) {
229
            return false;
230
        }
231
    }
232
233
    /**
234
     * @inheritDoc
235
     */
236
    public function listDirectory(string $dirname)
237
    {
238
        try {
239
            $response = (array)$this->dropboxApp->rpcRequest(DropboxApp::ENDPOINT_LIST_FOLDER, $this->dropboxApp->path($dirname));
240
            return $response['entries'];
241
        } catch (Exception $e) {
242
            return false;
243
        }
244
    }
245
246
}