GoogleDriveFileSystemAdapter::lastModified()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 4
nop 1
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 3.0.0
13
 */
14
15
namespace Quantum\Libraries\Storage\Adapters\GoogleDrive;
16
17
use Quantum\Libraries\Storage\Contracts\FilesystemAdapterInterface;
18
use Exception;
19
20
/**
21
 * Class GoogleDriveFileSystemAdapter
22
 * @package Quantum\Libraries\Storage
23
 */
24
class GoogleDriveFileSystemAdapter implements FilesystemAdapterInterface
25
{
26
    /**
27
     * @var GoogleDriveApp
28
     */
29
    private $googleDriveApp;
30
31
    /**
32
     * @param GoogleDriveApp $googleDriveApp
33
     */
34
    public function __construct(GoogleDriveApp $googleDriveApp)
35
    {
36
        $this->googleDriveApp = $googleDriveApp;
37
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42
    public function makeDirectory(string $dirname, ?string $parentId = null): bool
43
    {
44
        try {
45
            $data = [
46
                'name' => $dirname,
47
                'mimeType' => GoogleDriveApp::FOLDER_MIMETYPE,
48
                'parents' => $parentId ? [$parentId] : ['root'],
49
            ];
50
51
            $this->googleDriveApp->rpcRequest(GoogleDriveApp::FILE_METADATA_URL, $data);
52
            return true;
53
        } catch (Exception $e) {
54
            return false;
55
        }
56
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61
    public function removeDirectory(string $dirname): bool
62
    {
63
        return $this->remove($dirname);
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69
    public function get(string $filename)
70
    {
71
        try {
72
            return (string)$this->googleDriveApp->getFileInfo($filename, true);
73
        } catch (Exception $e) {
74
            return false;
75
        }
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81
    public function put(string $filename, string $content, ?string $parentId = null)
82
    {
83
        try {
84
            if ($this->isFile($filename)) {
85
                $fileId = $filename;
86
            } else {
87
                $data = [
88
                    'name' => $filename,
89
                    'parents' => $parentId ? [$parentId] : ['root'],
90
                ];
91
92
                $newFile = $this->googleDriveApp->rpcRequest(
93
                    GoogleDriveApp::FILE_METADATA_URL,
94
                    $data
95
                );
96
97
                if (!isset($newFile->id)) {
98
                    throw new Exception('Google Drive file creation failed');
99
                }
100
101
                $fileId = $newFile->id;
102
            }
103
104
            return $this->googleDriveApp->rpcRequest(
105
                GoogleDriveApp::FILE_MEDIA_URL . '/' . $fileId . '?uploadType=media',
106
                $content,
107
                'PUT',
108
                'application/octet-stream'
109
            );
110
111
        } catch (Exception $e) {
112
            return false;
113
        }
114
    }
115
116
    /**
117
     * @inheritDoc
118
     */
119
    public function append(string $filename, string $content)
120
    {
121
        $fileContent = $this->get($filename);
122
123
        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

123
        return $this->put($filename, /** @scrutinizer ignore-type */ $fileContent . $content);
Loading history...
124
    }
125
126
    /**
127
     * @inheritDoc
128
     */
129
    public function rename(string $oldName, string $newName): bool
130
    {
131
        try {
132
            $data = [
133
                'name' => $newName,
134
            ];
135
136
            $this->googleDriveApp->rpcRequest(GoogleDriveApp::FILE_METADATA_URL . '/' . $oldName, $data, 'PATCH');
137
            return true;
138
        } catch (Exception $e) {
139
            return false;
140
        }
141
    }
142
143
    /**
144
     * @inheritDoc
145
     */
146
    public function copy(string $source, string $dest = 'root'): bool
147
    {
148
        try {
149
            $data = [
150
                'parents' => [$dest],
151
            ];
152
153
            $this->googleDriveApp->rpcRequest(GoogleDriveApp::FILE_METADATA_URL . '/' . $source . '/copy', $data);
154
            return true;
155
        } catch (Exception $e) {
156
            return false;
157
        }
158
    }
159
160
    /**
161
     * @inheritDoc
162
     */
163
    public function exists(string $filename): bool
164
    {
165
        return $this->isFile($filename);
166
    }
167
168
    /**
169
     * @inheritDoc
170
     */
171
    public function size(string $filename)
172
    {
173
        try {
174
            $meta = (array)$this->googleDriveApp->getFileInfo($filename);
175
            return $meta['size'];
176
        } catch (Exception $e) {
177
            return false;
178
        }
179
    }
180
181
    /**
182
     * @inheritDoc
183
     */
184
    public function lastModified(string $filename)
185
    {
186
        try {
187
            $meta = (array)$this->googleDriveApp->getFileInfo($filename);
188
            return empty($meta['modifiedTime']) ? false : strtotime($meta['modifiedTime']);
189
        } catch (Exception $e) {
190
            return false;
191
        }
192
193
    }
194
195
    /**
196
     * @inheritDoc
197
     */
198
    public function remove(string $filename): bool
199
    {
200
        try {
201
            $this->googleDriveApp->rpcRequest(GoogleDriveApp::FILE_METADATA_URL . '/' . $filename, [], 'DELETE');
202
            return true;
203
        } catch (Exception $e) {
204
            return false;
205
        }
206
207
    }
208
209
    /**
210
     * @inheritDoc
211
     */
212
    public function isFile(string $filename): bool
213
    {
214
        try {
215
            $meta = (array)$this->googleDriveApp->getFileInfo($filename);
216
217
            return $meta['kind'] === GoogleDriveApp::DRIVE_FILE_KIND && $meta['mimeType'] != GoogleDriveApp::FOLDER_MIMETYPE;
218
        } catch (Exception $e) {
219
            return false;
220
        }
221
    }
222
223
    /**
224
     * @inheritDoc
225
     */
226
    public function isDirectory(string $dirname): bool
227
    {
228
        try {
229
            $meta = (array)$this->googleDriveApp->getFileInfo($dirname);
230
231
            return $meta['kind'] === GoogleDriveApp::DRIVE_FILE_KIND && $meta['mimeType'] === GoogleDriveApp::FOLDER_MIMETYPE;
232
        } catch (Exception $e) {
233
            return false;
234
        }
235
    }
236
237
    /**
238
     * @inheritDoc
239
     */
240
    public function listDirectory(string $dirname)
241
    {
242
        try {
243
            $params = [
244
                'q' => "'$dirname' in parents and trashed = false",
245
                'fields' => '*',
246
            ];
247
            $response = (array)$this->googleDriveApp->rpcRequest(GoogleDriveApp::FILE_METADATA_URL . '?' . http_build_query($params), [], 'GET');
248
            return $response['files'];
249
        } catch (Exception $e) {
250
            return false;
251
        }
252
    }
253
}
254