GoogleCloudStorageComponent::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace diecoding\flysystem;
4
5
use Google\Auth\FetchAuthTokenInterface;
6
use Google\Cloud\Storage\StorageClient;
7
use League\Flysystem\GoogleCloudStorage\GoogleCloudStorageAdapter;
8
use Psr\Cache\CacheItemPoolInterface;
9
use yii\base\InvalidConfigException;
10
11
/**
12
 * Interacting with Google Cloud Storage filesystem
13
 * @see https://flysystem.thephpleague.com/docs/adapter/google-cloud-storage/
14
 * 
15
 * ```php
16
 * 'components' => [
17
 *     'fs' => [
18
 *         'class' => \diecoding\flysystem\GoogleCloudStorageComponent::class,
19
 *         'bucket' => 'your-bucket',
20
 *         // 'apiEndpoint' => '',
21
 *         // 'projectId' => '',
22
 *         // 'authCache' => null,
23
 *         // 'authCacheOptions' => [],
24
 *         // 'authHttpHandler' => function () {},
25
 *         // 'credentialsFetcher' => null,
26
 *         // 'httpHandler' => function () {},
27
 *         // 'keyFile' => '',
28
 *         'keyFilePath' => __DIR__ . '/gcs_credentials.json',
29
 *         // 'requestTimeout' => 0,
30
 *         // 'retries' => 0,
31
 *         // 'scopes' => [],
32
 *         // 'quotaProject' => '',
33
 *         // 'userProject' => false,
34
 *         // 'prefix' => '',
35
 *     ],
36
 * ],
37
 * ```
38
 * 
39
 * @link      https://sugengsulistiyawan.my.id/
40
 * @author    Sugeng Sulistiyawan <[email protected]>
41
 * @copyright Copyright (c) 2024
42
 */
43
class GoogleCloudStorageComponent extends AbstractComponent
44
{
45
    /**
46
     * @var string The name of the bucket to request.
47
     */
48
    public $bucket;
49
50
    /**
51
     * @var string The hostname with optional port to use in
52
     *             place of the default service endpoint. Example:
53
     *             `foobar.com` or `foobar.com:1234`.
54
     */
55
    public $apiEndpoint;
56
57
    /**
58
     * @var string The project ID from the Google Developer's
59
     *             Console.
60
     */
61
    public $projectId;
62
63
    /**
64
     * @var CacheItemPoolInterface A cache used storing access
65
     *                             tokens. **Defaults to** a simple in memory implementation.
66
     */
67
    public $authCache;
68
69
    /**
70
     * @var array Cache configuration options.
71
     */
72
    public $authCacheOptions;
73
74
    /**
75
     * @var callable A handler used to deliver Psr7
76
     *               requests specifically for authentication.
77
     */
78
    public $authHttpHandler;
79
80
    /**
81
     * @var FetchAuthTokenInterface A credentials fetcher instance.
82
     */
83
    public $credentialsFetcher;
84
85
    /**
86
     * @var callable A handler used to deliver Psr7 requests.
87
     *               Only valid for requests sent over REST.
88
     */
89
    public $httpHandler;
90
91
    /**
92
     * @var array The contents of the service account credentials
93
     *            .json file retrieved from the Google Developer's Console.
94
     *            Ex: `json_decode(file_get_contents($path), true)`.
95
     */
96
    public $keyFile;
97
98
    /**
99
     * @var string The full path to your service account
100
     *             credentials .json file retrieved from the Google Developers
101
     *             Console.
102
     */
103
    public $keyFilePath;
104
105
    /**
106
     * @var float Seconds to wait before timing out the
107
     *            request. **Defaults to** `0` with REST and `60` with gRPC.
108
     */
109
    public $requestTimeout;
110
111
    /**
112
     * @var int Number of retries for a failed request.
113
     *          **Defaults to** `3`.
114
     */
115
    public $retries;
116
117
    /**
118
     * @var array Scopes to be used for the request.
119
     */
120
    public $scopes;
121
122
    /**
123
     * @var string Specifies a user project to bill for
124
     *             access charges associated with the request.
125
     */
126
    public $quotaProject;
127
128
    /**
129
     * @var string|bool $userProject If true, the current Project ID
130
     *                  will be used. If a string, that string will be used as the
131
     *                  userProject argument, and that project will be billed for the
132
     *                  request. **Defaults to** `false`.
133
     */
134
    public $userProject = false;
135
136
    /**
137
     * @var StorageClient
138
     */
139
    protected $client;
140
141
    /**
142
     * @inheritdoc
143
     * @throws InvalidConfigException
144
     */
145
    public function init()
146
    {
147
        $this->validateProperties([
148
            'bucket',
149
        ]);
150
151
        parent::init();
152
    }
153
154
    /**
155
     * @return GoogleCloudStorageAdapter
156
     */
157
    protected function initAdapter()
158
    {
159
        $config = [
160
            'apiEndpoint' => $this->apiEndpoint,
161
            'projectId' => $this->projectId,
162
            'authCache' => $this->authCache,
163
            'authCacheOptions' => $this->authCacheOptions,
164
            'authHttpHandler' => $this->authHttpHandler,
165
            'credentialsFetcher' => $this->credentialsFetcher,
166
            'httpHandler' => $this->httpHandler,
167
            'keyFile' => $this->keyFile,
168
            'keyFilePath' => $this->keyFilePath,
169
            'requestTimeout' => $this->requestTimeout,
170
            'retries' => $this->retries,
171
            'scopes' => $this->scopes,
172
            'quotaProject' => $this->quotaProject,
173
        ];
174
175
        foreach ($config as $key => $value) {
176
            if (empty($value)) {
177
                unset($config[$key]);
178
            }
179
        }
180
181
        $this->client = new StorageClient($config);
182
        $bucket = $this->client->bucket($this->bucket, $this->userProject);
183
184
        return new GoogleCloudStorageAdapter($bucket, (string) $this->prefix);
185
    }
186
}
187