Passed
Push — master ( 8bf825...1fc14d )
by Alexey
03:10
created

Filesystem::createMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.125

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 4
cp 0.5
crap 1.125
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types = 1);
2
3
namespace Venta\Filesystem;
4
5
use League\Flysystem\FilesystemInterface;
6
use League\Flysystem\UnreadableFileException;
7
use Venta\Contracts\Filesystem\Filesystem as FilesystemContract;
8
use Venta\Contracts\Filesystem\Metadata as MetadataContract;
9
10
/**
11
 * Class Filesystem
12
 *
13
 * @package Venta\Filesystem
14
 */
15
final class Filesystem implements FilesystemContract
16
{
17
18
    /**
19
     * @var FilesystemInterface
20
     */
21
    private $flysystem;
22
23
    /**
24
     * Filesystem constructor.
25
     *
26
     * @param FilesystemInterface $flysystem
27
     */
28 6
    public function __construct(FilesystemInterface $flysystem)
29
    {
30 6
        $this->flysystem = $flysystem;
31 6
    }
32
33
    /**
34
     * @inheritDoc
35
     */
36 1
    public function append(string $path, string $contents, array $config = []): bool
37
    {
38 1
        if ($this->exists($path)) {
39 1
            $contents = $this->read($path) . $contents;
40
        }
41
42 1
        return $this->write($path, $contents);
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48 1
    public function copy(string $path, string $newPath): bool
49
    {
50 1
        return $this->flysystem->copy($path, $newPath);
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56 1
    public function createDir(string $dirname, array $config = []): bool
57
    {
58 1
        return $this->flysystem->createDir($dirname, $config);
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64 1
    public function delete(string $path): bool
65
    {
66 1
        return $this->flysystem->delete($path);
67
    }
68
69
    /**
70
     * @inheritDoc
71
     */
72 1
    public function deleteDir(string $dirname): bool
73
    {
74 1
        return $this->flysystem->deleteDir($dirname);
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80 2
    public function exists(string $path): bool
81
    {
82 2
        return $this->flysystem->has($path);
83
    }
84
85
    /**
86
     * @inheritDoc
87
     */
88 1
    public function listAll(string $path = '.', bool $recursive = false): array
89
    {
90 1
        return array_map([$this, 'createMetadata'], $this->flysystem->listContents($path, $recursive));
91
    }
92
93
    /**
94
     * @inheritDoc
95
     */
96 1 View Code Duplication
    public function listDirectories(string $path = '.', bool $recursive = false): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98 1
        return array_map(
99 1
            [$this, 'createMetadata'],
100
            array_filter(
101 1
                $this->flysystem->listContents($path, $recursive),
102
                function ($item) {
103 1
                    return $item['type'] === MetadataContract::TYPE_DIR;
104 1
                }
105
            )
106
        );
107
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112 1 View Code Duplication
    public function listFiles(string $path = '.', bool $recursive = false): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
    {
114 1
        return array_map(
115 1
            [$this, 'createMetadata'],
116
            array_filter(
117 1
                $this->flysystem->listContents($path, $recursive),
118 1
                function ($item) {
119 1
                    return $item['type'] === MetadataContract::TYPE_FILE;
120 1
                }
121
            )
122
        );
123
    }
124
125
    /**
126
     * @inheritDoc
127
     */
128 1
    public function metadata($path)
129
    {
130 1
        $metadata = $this->flysystem->getMetadata($path);
131 1
        if (is_array($metadata)) {
132 1
            return $this->createMetadata($metadata);
133
        }
134
135
        return null;
136
    }
137
138
    /**
139
     * @inheritDoc
140
     */
141 1
    public function move(string $path, string $newPath): bool
142
    {
143 1
        return $this->flysystem->rename($path, $newPath);
144
    }
145
146
    /**
147
     * @inheritDoc
148
     */
149 1
    public function prepend(string $path, string $contents, array $config = []): bool
150
    {
151 1
        if ($this->exists($path)) {
152 1
            $contents .= $this->read($path);
153
        }
154
155 1
        return $this->write($path, $contents);
156
    }
157
158
    /**
159
     * @inheritDoc
160
     */
161 2
    public function read(string $path): string
162
    {
163 2
        $content = $this->flysystem->read($path);
164 2
        if ($content === false) {
165
            throw new UnreadableFileException(sprintf('Unable to read file "%s".', $path));
166
        }
167
168 2
        return $content;
169
    }
170
171
    /**
172
     * @inheritDoc
173
     */
174 2
    public function write(string $path, string $contents, array $config = []): bool
175
    {
176 2
        return $this->flysystem->put($path, $contents, $config);
177
    }
178
179
    /**
180
     * @param array $metadata
181
     * @return MetadataContract
182
     */
183 2
    private function createMetadata(array $metadata): MetadataContract
184
    {
185 2
        return new Metadata($metadata);
186
    }
187
188
}