-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGoogleDriveFilesystem.php
67 lines (57 loc) · 1.64 KB
/
GoogleDriveFilesystem.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/**
* @link http://www.lahautesociete.com
* @copyright Copyright (c) 2017 La Haute Société
*/
namespace lhs\Yii2FlysystemGoogleDrive;
use creocoder\flysystem\Filesystem;
use Google_Client;
use Google_Service_Drive;
use Hypweb\Flysystem\GoogleDrive\GoogleDriveAdapter;
use League\Flysystem\AdapterInterface;
use yii\base\InvalidConfigException;
/**
* GoogleDriveFilesystem class
*
* @author albanjubert
**/
class GoogleDriveFilesystem extends Filesystem
{
public $clientId;
public $clientSecret;
public $refreshToken;
public $rootFolderId;
public $driveId;
/**
* @inheritdoc
*/
public function init()
{
if ($this->clientId === null) {
throw new InvalidConfigException('The "clientId" property must be set.');
}
if ($this->clientSecret === null) {
throw new InvalidConfigException('The "clientSecret" property must be set.');
}
if ($this->refreshToken === null) {
throw new InvalidConfigException('The "refreshToken" property must be set.');
}
parent::init();
}
/**
* @return AdapterInterface
*/
protected function prepareAdapter()
{
$client = new Google_Client();
$client->setClientId($this->clientId);
$client->setClientSecret($this->clientSecret);
$client->refreshToken($this->refreshToken);
$service = new Google_Service_Drive($client);
$options = [];
if (isset($this->driveId)) {
$options['teamDriveId'] = $this->driveId;
}
return new GoogleDriveAdapter($service, $this->rootFolderId, $options);
}
}