Passed
Push — master ( 7ab13e...d32116 )
by Jasper
10:00
created

EncryptedFilesystemAdapter::directoryExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Swis\Flysystem\Encrypted;
6
7
use Illuminate\Contracts\Encryption\DecryptException;
8
use Illuminate\Contracts\Encryption\Encrypter;
9
use Illuminate\Contracts\Encryption\EncryptException;
10
use Illuminate\Support\Traits\ForwardsCalls;
11
use League\Flysystem\FileAttributes;
12
use League\Flysystem\FilesystemAdapter;
13
use League\Flysystem\Config;
14
use League\Flysystem\UnableToReadFile;
15
use League\Flysystem\UnableToRetrieveMetadata;
16
use League\Flysystem\UnableToWriteFile;
17
use League\MimeTypeDetection\ExtensionMimeTypeDetector;
18
use League\MimeTypeDetection\MimeTypeDetector;
19
20
class EncryptedFilesystemAdapter implements FilesystemAdapter
21
{
22
    use ForwardsCalls;
23
24
    /**
25
     * @var \League\Flysystem\FilesystemAdapter
26
     */
27
    private $adapter;
28
29
    /**
30
     * @var \Illuminate\Contracts\Encryption\Encrypter
31
     */
32
    private $encrypter;
33
34
    /**
35
     * @var \League\MimeTypeDetection\MimeTypeDetector
36
     */
37
    private $mimeTypeDetector;
38
39
    /**
40
     * @param \League\Flysystem\FilesystemAdapter $adapter
41
     * @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
42
     * @param \League\MimeTypeDetection\MimeTypeDetector|null $mimeTypeDetector
43
     */
44
    public function __construct(
45
        FilesystemAdapter $adapter,
46
        Encrypter $encrypter,
47
        MimeTypeDetector $mimeTypeDetector = null
48
    ) {
49
        $this->adapter = $adapter;
50
        $this->encrypter = $encrypter;
51
        $this->mimeTypeDetector = $mimeTypeDetector ?: new ExtensionMimeTypeDetector();
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    public function fileExists(string $path): bool
58
    {
59
        return $this->adapter->fileExists($path);
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function directoryExists(string $path): bool
66
    {
67
        return $this->adapter->directoryExists($path);
0 ignored issues
show
Bug introduced by
The method directoryExists() does not exist on League\Flysystem\FilesystemAdapter. It seems like you code against a sub-type of League\Flysystem\FilesystemAdapter such as Swis\Flysystem\Encrypted...ryptedFilesystemAdapter. ( Ignorable by Annotation )

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

67
        return $this->adapter->/** @scrutinizer ignore-call */ directoryExists($path);
Loading history...
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73
    public function write(string $path, string $contents, Config $config): void
74
    {
75
        try {
76
            $this->adapter->write($path, $this->encrypter->encrypt($contents), $config);
77
        } catch (EncryptException $exception) {
78
            throw UnableToWriteFile::atLocation($path, $exception->getMessage(), $exception);
79
        }
80
    }
81
82
    /**
83
     * @inheritDoc
84
     */
85
    public function writeStream(string $path, $contents, Config $config): void
86
    {
87
        $this->write($path, stream_get_contents($contents), $config);
88
    }
89
90
    /**
91
     * @inheritDoc
92
     */
93
    public function read(string $path): string
94
    {
95
        try {
96
            return $this->encrypter->decrypt($this->adapter->read($path));
97
        } catch (DecryptException $exception) {
98
            throw UnableToReadFile::fromLocation($path, $exception->getMessage(), $exception);
99
        }
100
    }
101
102
    /**
103
     * @inheritDoc
104
     */
105
    public function readStream(string $path)
106
    {
107
        $stream = tmpfile();
108
109
        if ($stream === false) {
110
            throw UnableToReadFile::fromLocation($path, 'Unable to create temporary stream.');
111
        }
112
113
        fwrite($stream, $this->read($path));
114
        fseek($stream, 0);
115
116
        return $stream;
117
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122
    public function delete(string $path): void
123
    {
124
        $this->adapter->delete($path);
125
    }
126
127
    /**
128
     * @inheritDoc
129
     */
130
    public function deleteDirectory(string $path): void
131
    {
132
        $this->adapter->deleteDirectory($path);
133
    }
134
135
    /**
136
     * @inheritDoc
137
     */
138
    public function createDirectory(string $path, Config $config): void
139
    {
140
        $this->adapter->createDirectory($path, $config);
141
    }
142
143
    /**
144
     * @inheritDoc
145
     */
146
    public function setVisibility(string $path, string $visibility): void
147
    {
148
        $this->adapter->setVisibility($path, $visibility);
149
    }
150
151
    /**
152
     * @inheritDoc
153
     */
154
    public function visibility(string $path): FileAttributes
155
    {
156
        return $this->adapter->visibility($path);
157
    }
158
159
    /**
160
     * @inheritDoc
161
     */
162
    public function mimeType(string $path): FileAttributes
163
    {
164
        try {
165
            $mimeType = $this->mimeTypeDetector->detectMimeType($path, $this->read($path));
166
        } catch (UnableToReadFile $exception) {
167
            throw UnableToRetrieveMetadata::mimeType($path, $exception->getMessage(), $exception);
168
        }
169
170
        if ($mimeType === null) {
171
            throw UnableToRetrieveMetadata::mimeType($path);
172
        }
173
174
        return new FileAttributes($path, null, null, null, $mimeType);
175
    }
176
177
    /**
178
     * @inheritDoc
179
     */
180
    public function lastModified(string $path): FileAttributes
181
    {
182
        return $this->adapter->lastModified($path);
183
    }
184
185
    /**
186
     * @inheritDoc
187
     */
188
    public function fileSize(string $path): FileAttributes
189
    {
190
        try {
191
            return new FileAttributes($path, \strlen($this->read($path)));
192
        } catch (UnableToReadFile $exception) {
193
            throw UnableToRetrieveMetadata::fileSize($path, $exception->getMessage(), $exception);
194
        }
195
    }
196
197
    /**
198
     * @inheritDoc
199
     */
200
    public function listContents(string $path, bool $deep): iterable
201
    {
202
        return $this->adapter->listContents($path, $deep);
203
    }
204
205
    /**
206
     * @inheritDoc
207
     */
208
    public function move(string $source, string $destination, Config $config): void
209
    {
210
        $this->adapter->move($source, $destination, $config);
211
    }
212
213
    /**
214
     * @inheritDoc
215
     */
216
    public function copy(string $source, string $destination, Config $config): void
217
    {
218
        $this->adapter->copy($source, $destination, $config);
219
    }
220
221
    /**
222
     * Dynamically pass missing methods to the decorated adapter.
223
     *
224
     * @param string $method
225
     * @param array $parameters
226
     *
227
     * @return mixed
228
     */
229
    public function __call(string $method, array $parameters)
230
    {
231
        return $this->forwardCallTo($this->adapter, $method, $parameters);
232
    }
233
}
234