Completed
Pull Request — master (#427)
by
unknown
02:37
created

GoogleCloudClientStorage::getResources()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
namespace Gaufrette\Adapter;
3
4
use Gaufrette\Adapter;
5
use Gaufrette\Adapter\MetadataSupporter;
6
use Gaufrette\Adapter\ListKeysAware;
7
8
/**
9
 * Google Cloud Storage adapter using the Google Cloud Client Library for PHP
10
 * http://googlecloudplatform.github.io/google-cloud-php/
11
 *
12
 * @package Gaufrette
13
 * @author  Lech Buszczynski <[email protected]>
14
 */
15
class GoogleCloudClientStorage implements Adapter, MetadataSupporter, ListKeysAware
16
{
17
    protected $storageClient;
18
    protected $bucket;
19
    protected $bucketValidated;
20
    protected $options      = array();
21
    protected $metadata     = array();
22
    protected $resources    = array();
23
24
    /**
25
     * @param Google\Cloud\Storage\StorageClient    $service    Authenticated storage client class
0 ignored issues
show
Bug introduced by
There is no parameter named $service. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
26
     * @param string                                $bucketName Name of the bucket
27
     * @param array                                 $options    Options are: "directory" and "acl" (see https://cloud.google.com/storage/docs/access-control/lists)
28
     */
29
    public function __construct(\Google\Cloud\Storage\StorageClient $storageClient, $bucketName, $options = array())
30
    {
31
        $this->storageClient = $storageClient;
32
        $this->setBucket($bucketName);
33
        $this->options = array_replace_recursive(
34
            array(
35
                'directory' => '',
36
                'acl'       => array()
37
            ),
38
            $options
39
        );
40
        $this->options['directory'] = rtrim($this->options['directory'], '/');
41
    }
42
    
43
    /**
44
     * Get adapter options
45
     * 
46
     * @return  array
47
     */
48
    public function getOptions()
49
    {
50
        return $this->options;
51
    }
52
53
    /**
54
     * Set adapter options
55
     * 
56
     * @param   array   $options
57
     */
58
    public function setOptions($options)
59
    {
60
        $this->options = array_replace($this->options, $options);
61
    }
62
    
63
    protected function computePath($key = null)
64
    {
65
        if (strlen($this->options['directory']))
66
        {
67
            return $this->options['directory'].'/'.$key;
68
        }
69
        return $key;
70
    }
71
    
72
    protected function isBucket()
73
    {
74
        if ($this->bucketValidated === true)
75
        {
76
            return true;
77
        } elseif (!$this->bucket->exists()) {
78
            throw new \RuntimeException(sprintf('Bucket %s does not exist.', $this->bucket->name()));
79
        }
80
        $this->bucketValidated = true;
81
        return true;
82
    }
83
    
84
    public function setBucket($name)
85
    {
86
        $this->bucketValidated = null;
87
        $this->bucket = $this->storageClient->bucket($name);       
88
        $this->isBucket();
89
    }
90
    
91
    public function getBucket()
92
    {
93
        return $this->bucket;
94
    }
95
    
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function read($key)
100
    {   
101
        $this->isBucket();     
102
        $object = $this->bucket->object($this->computePath($key));
103
        try {
104
            $info = $object->info();
105
            $this->setResources($key, $info);
106
            return $object->downloadAsString();
107
        } catch (\Exception $e) {
108
            return false;
109
        }
110
    }
111
    
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function write($key, $content)
116
    {
117
        $this->isBucket();
118
        
119
        $options = array(
120
            'resumable'     => true,
121
            'name'          => $this->computePath($key),
122
            'metadata'      => $this->getMetadata($key),
123
        );
124
125
        $this->bucket->upload(
126
            $content,
127
            $options
128
        );
129
        
130
        $this->updateKeyProperties($key,
131
            array(
132
                'acl'       => $this->options['acl'],
133
                'metadata'  => $this->getMetadata($key)
134
            )
135
        );
136
137
        $size = $this->getResourceByName($key, 'size');
138
        return $size === null ? false : $size;
139
    }
140
    
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function exists($key)
145
    {
146
        $this->isBucket();
147
        $object = $this->bucket->object($this->computePath($key));
148
        return $object->exists();
149
    }
150
    
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function isDirectory($key)
155
    {
156
        return $this->exists($this->computePath(rtrim($key, '/')).'/');
157
    }
158
    
159
    /**
160
     * {@inheritdoc}
161
     */
162
    public function listKeys($prefix = null)
163
    {
164
        $this->isBucket();        
165
        $keys = array();
166
        
167
        foreach ($this->bucket->objects(array('prefix' => $this->computePath($prefix))) as $e)
168
        {
169
            $keys[] = $e->name();
170
        }
171
        sort($keys);
172
        return $keys;
173
    }
174
    
175
    /**
176
     * {@inheritdoc}
177
     */
178
    public function keys()
179
    {
180
        return $this->listKeys();
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    public function mtime($key)
187
    {
188
        $this->isBucket();
189
        $object = $object = $this->bucket->object($this->computePath($key));
190
        $info = $object->info();
191
        $this->setResources($key, $info);
192
        return strtotime($info['updated']);
193
    }
194
    
195
    /**
196
     * {@inheritdoc}
197
     */
198
    public function delete($key)
199
    {
200
        $this->isBucket();
201
        try {
202
            $object = $this->bucket->object($this->computePath($key));
203
            $object->delete();
204
            $this->setMetadata($key, null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
205
        } catch (\Exception $e) {
206
            return false;
207
        }
208
        return true;
209
    }
210
    
211
    /**
212
     * {@inheritdoc}
213
     */
214
    public function rename($sourceKey, $targetKey)
215
    {
216
        $this->isBucket();
217
        
218
        $pathedSourceKey = $this->computePath($sourceKey);
219
        $pathedTargetKey = $this->computePath($targetKey);
220
                
221
        $object = $this->bucket->object($pathedSourceKey);
222
        
223
        $copiedObject = $object->copy($this->bucket,
224
            array(
225
                'name' => $pathedTargetKey
226
            )
227
        );
228
        
229
        if ($copiedObject->exists())
230
        {
231
            $this->updateKeyProperties($targetKey,
232
                array(
233
                    'acl'       => $this->options['acl'],
234
                    'metadata'  => $this->getMetadata($sourceKey)
235
                )
236
            );
237
            $this->setMetadata($targetKey, $this->getMetadata($sourceKey));
238
            $this->setMetadata($sourceKey, null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
239
            $object->delete();
240
            return true;
241
        }
242
        return false;
243
    }
244
    
245
    /**
246
     * {@inheritdoc}
247
     */
248
    public function setMetadata($key, $metadata)
249
    {
250
        $this->metadata[$this->computePath($key)] = $metadata;
251
    }
252
253
    /**
254
     * {@inheritdoc}
255
     */
256
    public function getMetadata($key)
257
    {
258
        $pathedKey = $this->computePath($key);
259
        if (!isset($this->metadata[$pathedKey]) && $this->exists($pathedKey))
260
        {
261
            $data = $this->bucket->object($pathedKey)->info();
262
            if (isset($data['metadata']))
263
            {
264
                $this->metadata[$pathedKey] = $data['metadata'];
265
            }
266
        }
267
        return isset($this->metadata[$pathedKey]) ? $this->metadata[$pathedKey] : array();
268
    }
269
    
270
    /**
271
     * {@inheritdoc}
272
     */
273
    public function setResources($key, $data)
274
    {
275
        $this->resources[$this->computePath($key)] = $data;
276
    }
277
    
278
    /**
279
     * {@inheritdoc}
280
     */
281
    public function getResources($key)
282
    {
283
        $pathedKey = $this->computePath($key);
284
        return isset($this->resources[$pathedKey]) ? $this->resources[$pathedKey] : array();
285
    }
286
    
287
    /**
288
     * {@inheritdoc}
289
     */
290
    public function getResourceByName($key, $resourceName)
291
    {
292
        $pathedKey = $this->computePath($key);
293
        return isset($this->resources[$pathedKey][$resourceName]) ? $this->resources[$pathedKey][$resourceName] : null;
294
    }
295
    
296
    /**
297
     * Sets ACL and metadata information.
298
     * 
299
     * @param   string  $key
300
     * @param   array   $options    Can contain "acl" and/or "metadata" arrays.
301
     * @return  boolean
302
     */
303
    protected function updateKeyProperties($key, $options = array())
304
    {
305
        if ($this->exists($key) === false)
306
        {
307
            return false;
308
        }
309
      
310
        $object = $this->bucket->object($this->computePath($key));
311
        
312
        $properties = array_replace_recursive(
313
            array(
314
                'acl'       => array(),
315
                'metadata'  => array()
316
            ), $options
317
        );
318
319
        $acl = $object->acl();
320
        foreach ($properties['acl'] as $k => $v)
321
        {
322
            $acl->add($k, $v);
323
        }
324
        $object->update(array('metadata' => $properties['metadata']));
325
326
        $info = $object->info();
327
328
        $this->setResources($key, $info);
329
        return true;
330
    }
331
}
332