Passed
Push — main ( c98abe...4f1522 )
by Sugeng
04:42
created

GoogleDriveComponent   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 30
c 1
b 0
f 0
dl 0
loc 89
rs 10

2 Methods

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