StorageAdapter::getVisibility()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Talboterie\FlysystemGCPStorage;
6
7
use Google\Cloud\Core\Exception\NotFoundException;
8
use Google\Cloud\Storage\Bucket;
9
use Google\Cloud\Storage\StorageObject;
10
use League\Flysystem\Adapter\AbstractAdapter;
11
use League\Flysystem\Config;
12
use LogicException;
13
14
class StorageAdapter extends AbstractAdapter
15
{
16
    /** @var Bucket */
17
    protected $bucket;
18
19
    public function __construct(Bucket $bucket, string $prefix = '')
20
    {
21
        $this->bucket = $bucket;
22
        $this->setPathPrefix($prefix);
23
    }
24
25
    public function copy($path, $newPath): StorageObject
26
    {
27
        return $this->move($path, $newPath, false);
28
    }
29
30
    public function createDir($dirname, Config $config): void
31
    {
32
        throw new LogicException(get_class($this) . ' create directory as needed when writing file. Path: ' . $dirname);
33
    }
34
35
    public function delete($path): bool
36
    {
37
        try {
38
            $this->bucket->object($this->applyPathPrefix($path))->delete();
39
40
            return true;
41
        } catch (NotFoundException $exception) {
42
            return false;
43
        }
44
    }
45
46
    public function deleteDir($dirname): bool
47
    {
48
        /** @var StorageObject[] $objects */
49
        $objects = $this->bucket->objects(['prefix' => $dirname . '/']);
50
51
        foreach ($objects as $object) {
52
            $object->delete();
53
        }
54
55
        return true;
56
    }
57
58
    public function getMetadata($path): array
59
    {
60
        return $this->bucket->object($this->applyPathPrefix($path))->info();
61
    }
62
63
    public function getMimetype($path): array
64
    {
65
        $meta = $this->getMetadata($path);
66
67
        return ['mimetype' => $meta['contentType']];
68
    }
69
70
    public function getSize($path): array
71
    {
72
        $meta = $this->getMetadata($path);
73
74
        return ['size' => $meta['size']];
75
    }
76
77
    public function getTimestamp($path): array
78
    {
79
        $meta = $this->getMetadata($path);
80
81
        return ['timestamp' => strtotime($meta['updated'])];
82
    }
83
84
    public function getVisibility($path): array
85
    {
86
        $visibility = $this->bucket->object($this->applyPathPrefix($path))->acl()->get();
87
88
        return compact('path', 'visibility');
89
    }
90
91
    public function has($path): bool
92
    {
93
        return $this->bucket->object($this->applyPathPrefix($path))->exists();
94
    }
95
96
    public function listContents($directory = '', $recursive = false): array
97
    {
98
        $options = [];
99
        if (!empty($directory)) {
100
            $options['prefix'] = $directory . '/';
101
        }
102
103
        /** @var StorageObject[] $objects */
104
        $objects = $this->bucket->objects($options);
105
106
        $items = [];
107
        foreach ($objects as $object) {
108
            $items[] = ['path' => $object->name()];
109
        }
110
111
        return $items;
112
    }
113
114
    public function read($path): array
115
    {
116
        $contents = $this->bucket->object($this->applyPathPrefix($path))->downloadAsString();
117
118
        return compact('contents');
119
    }
120
121
    public function readStream($path): array
122
    {
123
        $stream = $this->bucket->object($this->applyPathPrefix($path))->downloadAsStream();
124
125
        return compact('stream');
126
    }
127
128
    public function rename($path, $newPath): StorageObject
129
    {
130
        return $this->move($path, $newPath);
131
    }
132
133
    public function setVisibility($path, $visibility): array
134
    {
135
        $object = $this->bucket->object($this->applyPathPrefix($path));
136
        $object->update(['acl' => []], ['predefinedAcl' => $visibility]);
137
138
        return compact('path', 'visibility');
139
    }
140
141
    public function update($path, $contents, Config $config): StorageObject
142
    {
143
        return $this->upload($path, $contents, $config);
144
    }
145
146
    public function updateStream($path, $resource, Config $config): StorageObject
147
    {
148
        return $this->upload($path, $resource, $config);
149
    }
150
151
    public function write($path, $contents, Config $config): StorageObject
152
    {
153
        return $this->upload($path, $contents, $config);
154
    }
155
156
    public function writeStream($path, $resource, Config $config): StorageObject
157
    {
158
        return $this->upload($path, $resource, $config);
159
    }
160
161
    protected function move(string $path, string $newPath, bool $delete = true): StorageObject
162
    {
163
        $object = $this->bucket->object($this->applyPathPrefix($path));
164
        $newObject = $object->copy($this->bucket->name(), ['name' => $this->applyPathPrefix($newPath)]);
165
166
        if ($delete) {
167
            $object->delete();
168
        }
169
170
        return $newObject;
171
    }
172
173
    protected function upload(string $path, $content, Config $config): StorageObject
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

173
    protected function upload(string $path, $content, /** @scrutinizer ignore-unused */ Config $config): StorageObject

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
174
    {
175
        return $this->bucket->upload($content, ['name' => $this->applyPathPrefix($path)]);
176
    }
177
}
178