Passed
Push — main ( d5e741...79ba9c )
by Sugeng
04:42
created

GoogleDriveComponent::init()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
c 2
b 0
f 0
dl 0
loc 23
rs 9.2222
cc 6
nc 7
nop 0
1
<?php
2
3
namespace diecoding\flysystem;
4
5
use Google\Client;
6
use Google\Service\Drive;
7
use League\Flysystem\PathPrefixing\PathPrefixedAdapter;
8
use Masbug\Flysystem\GoogleDriveAdapter;
9
use yii\base\InvalidConfigException;
10
11
/**
12
 * Interacting with Google Drive filesystem
13
 * @see https://github.com/masbug/flysystem-google-drive-ext
14
 * 
15
 * ```php
16
 * 'components' => [
17
 *     'fs' => [
18
 *         'class' => \diecoding\flysystem\GoogleDriveComponent::class,
19
 *         'applicationName' => 'My Google Drive App',
20
 *         'clientId' => '',
21
 *         'clientSecret' => '',
22
 *         'refreshToken' => '',
23
 *         'folderId' => '',
24
 *         // 'teamDriveId' => '',
25
 *         // 'sharedFolderId' => '',
26
 *         // 'options' => [],
27
 *         // 'debug' => false,
28
 *         // 'prefix' => '',
29
 *     ],
30
 * ],
31
 * ```
32
 * 
33
 * @link      https://sugengsulistiyawan.my.id/
34
 * @author    Sugeng Sulistiyawan <[email protected]>
35
 * @copyright Copyright (c) 2024
36
 */
37
class GoogleDriveComponent extends AbstractComponent
38
{
39
    /**
40
     * @var string
41
     */
42
    public $applicationName;
43
44
    /**
45
     * @var string
46
     */
47
    public $clientId;
48
49
    /**
50
     * @var string
51
     */
52
    public $clientSecret;
53
54
    /**
55
     * @var string
56
     */
57
    public $refreshToken;
58
59
    /**
60
     * @var string
61
     */
62
    public $folderId;
63
64
    /**
65
     * @var string
66
     */
67
    public $teamDriveId;
68
69
    /**
70
     * @var string
71
     */
72
    public $sharedFolderId;
73
74
    /**
75
     * @var array
76
     */
77
    public $options = [];
78
79
    /**
80
     * @var Client
81
     */
82
    protected $client;
83
84
    /**
85
     * @inheritdoc
86
     */
87
    public function init()
88
    {
89
        if (empty($this->clientId)) {
90
            throw new InvalidConfigException('The "clientId" property must be set.');
91
        }
92
93
        if (empty($this->clientSecret)) {
94
            throw new InvalidConfigException('The "clientSecret" property must be set.');
95
        }
96
97
        if (empty($this->refreshToken)) {
98
            throw new InvalidConfigException('The "refreshToken" property must be set.');
99
        }
100
101
        if (!empty($this->teamDriveId)) {
102
            $this->options['teamDriveId'] = $this->teamDriveId;
103
        }
104
105
        if (!empty($this->sharedFolderId)) {
106
            $this->options['sharedFolderId'] = $this->sharedFolderId;
107
        }
108
109
        parent::init();
110
    }
111
112
    /**
113
     * @return GoogleDriveAdapter
114
     */
115
    protected function initAdapter()
116
    {
117
        $client = new Client();
118
        $client->setClientId($this->clientId);
119
        $client->setClientSecret($this->clientSecret);
120
        $client->refreshToken(refreshToken: $this->refreshToken);
121
122
        if (!empty($this->applicationName)) {
123
            $client->setApplicationName($this->applicationName);
124
        }
125
126
        $this->client = $client;
127
        $service = new Drive($this->client);
128
        $adapter = new GoogleDriveAdapter($service, $this->folderId, $this->options);
129
130
        if ($this->prefix) {
131
            $adapter = new PathPrefixedAdapter($adapter, $this->prefix);
132
        }
133
134
        return $adapter;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $adapter also could return the type League\Flysystem\PathPrefixing\PathPrefixedAdapter which is incompatible with the documented return type Masbug\Flysystem\GoogleDriveAdapter.
Loading history...
135
    }
136
}
137