Passed
Pull Request — master (#167)
by
unknown
02:46
created

GoogleDriveFileSystemAdapter::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
nc 2
nop 1
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Quantum\Libraries\Storage\Adapters\GoogleDrive;
4
5
use Quantum\Libraries\Storage\FilesystemAdapterInterface;
6
use Exception;
7
8
class GoogleDriveFileSystemAdapter implements FilesystemAdapterInterface
9
{
10
11
    /**
12
     * @var GoogleDriveFileSystemAdapter|null
13
     */
14
    private static $instance = null;
15
16
    private $googleDriveApp;
17
18
    /**
19
     * GoogleDriveFileSystemAdapter constructor
20
     * @param GoogleDriveApp $googleDriveApp
21
     */
22
    private function __construct(GoogleDriveApp $googleDriveApp)
23
    {
24
        $this->googleDriveApp = $googleDriveApp;
25
    }
26
27
    /**
28
     * Get Instance
29
     * @param GoogleDriveApp $googleDriveApp
30
     * @return GoogleDriveFileSystemAdapter
31
     */
32
    public static function getInstance(GoogleDriveApp $googleDriveApp): GoogleDriveFileSystemAdapter
33
    {
34
        if (self::$instance === null) {
35
            self::$instance = new self($googleDriveApp);
36
        }
37
38
        return self::$instance;
39
    }
40
41
    public function makeDirectory(string $dirname, ?string $parentId = null): bool
42
    {
43
        try{
44
            $data = [
45
                'name' => $dirname,
46
                'mimeType' => GoogleDriveApp::FOLDER_MIMETYPE,
47
                'parents' => $parentId ? [$parentId] : ['root']
48
            ];
49
50
            $this->googleDriveApp->rpcRequest(GoogleDriveApp::FILE_METADATA_URL, $data);
51
            return true;
52
        } catch (Exception $e) {
53
            return false;
54
        }
55
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60
    public function removeDirectory(string $dirname): bool
61
    {
62
        return $this->remove($dirname);
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68
    public function get(string $filename)
69
    {
70
        try {
71
            return (string)$this->googleDriveApp->getFileInfo($filename, true);
72
        } catch (Exception $e) {
73
            return false;
74
        }
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80
    public function put(string $filename, string $content, ?string $parentId = null)
81
    {
82
        try {
83
            if($this->isFile($filename)){
84
                return $this->googleDriveApp->rpcRequest(GoogleDriveApp::FILE_MEDIA_URL . '/' . $filename . '?uploadType=media', $content, 'PATCH', 'application/octet-stream');
85
            }else{
86
                $data = [
87
                    'name' => $filename,
88
                    'parents' => $parentId ? [$parentId] : ['root']
89
                ];
90
91
                $newFile = $this->googleDriveApp->rpcRequest(GoogleDriveApp::FILE_METADATA_URL, $data);
92
93
                return $this->put($newFile->id, $content);
94
            }
95
        } catch (Exception $e) {
96
            return false;
97
        }
98
    }
99
100
    /**
101
     * @inheritDoc
102
     */
103
    public function append(string $filename, string $content)
104
    {
105
        $fileContent = $this->get($filename);
106
107
        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

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