Test Failed
Push — master ( d08b1d...c7da01 )
by Filippo
14:13 queued 09:16
created

ChunksFilesystem::store()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 3
eloc 11
c 2
b 1
f 0
nc 3
nop 3
dl 0
loc 22
rs 9.9
1
<?php
2
3
namespace Jobtech\LaravelChunky\Support;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Str;
8
use Jobtech\LaravelChunky\Chunk;
9
use Jobtech\LaravelChunky\Events\ChunkAdded;
10
use Jobtech\LaravelChunky\Events\ChunkDeleted;
11
use Jobtech\LaravelChunky\Exceptions\ChunkyException;
12
use Keven\Flysystem\Concatenate\Concatenate;
13
use Symfony\Component\HttpFoundation\File\File;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\File\File was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
class ChunksFilesystem extends Filesystem
16
{
17
    /**
18
     * @param string $folder
19
     * @return \Illuminate\Support\Collection
20
     */
21
    public function listChunks(string $folder): Collection
22
    {
23
        $folder = $this->path($folder);
24
        $files = $this->list($folder);
25
26
        return collect($files)
27
            ->map(function ($path, $key) use ($folder, $files) {
28
                $filename = str_replace($folder.DIRECTORY_SEPARATOR, '', $path);
29
                $exploded_name = explode('_', $filename);
30
31
                $index = array_shift($exploded_name);
32
                $last = count($files) - 1 == $key;
33
34
                return new Chunk(intval($index), $path, $this->disk(), $last);
35
            })->sortBy(function (Chunk $chunk) {
36
                return $chunk->getIndex();
37
            });
38
    }
39
40
    /**
41
     * @return array
42
     */
43
    public function chunkFolders(): array
44
    {
45
        return $this->filesystem()->disk(
46
            $this->disk()
47
        )->directories(
48
            $this->folder()
49
        );
50
    }
51
52
    /**
53
     * @param string $folder
54
     * @return int
55
     */
56
    public function chunksCount(string $folder): int
57
    {
58
        return count($this->list($this->path($folder)));
59
    }
60
61
    /**
62
     * @param string $path
63
     * @return int
64
     */
65
    public function chunkSize(string $path): int
66
    {
67
        return $this->filesystem()->disk($this->disk())->size($this->path($path));
68
    }
69
70
    /**
71
     * @param string $path
72
     * @return resource|null
73
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
74
     */
75
    public function readChunk(string $path)
76
    {
77
        return $this->filesystem()->disk($this->disk())->readStream($this->path($path));
78
    }
79
80
    /**
81
     * @param \Jobtech\LaravelChunky\Chunk $chunk
82
     * @param string $folder
83
     * @param array $options
84
     *
85
     * @return Chunk
86
     */
87
    public function store(Chunk $chunk, string $folder, $options = []): Chunk
88
    {
89
        if (! $chunk->getOriginalPath() instanceof File) {
90
            throw new ChunkyException('Path must be a file');
91
        }
92
93
        // Build destination
94
        $suffix = Str::endsWith($folder, DIRECTORY_SEPARATOR) ? '' : DIRECTORY_SEPARATOR;
95
        $destination = $this->path($folder.$suffix.$chunk->getSlug());
96
97
        // Copy file
98
        $file = fopen($chunk->getPath(), 'r');
99
100
        Arr::pull($options, 'disk');
101
        $this->filesystem()->disk($this->disk())->put($destination, $file, $options);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $contents of Illuminate\Filesystem\FilesystemAdapter::put() does only seem to accept resource|string, maybe add an additional type check? ( Ignorable by Annotation )

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

101
        $this->filesystem()->disk($this->disk())->put($destination, /** @scrutinizer ignore-type */ $file, $options);
Loading history...
102
        fclose($file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

102
        fclose(/** @scrutinizer ignore-type */ $file);
Loading history...
103
104
        // Return chunk
105
        $chunk->setPath($destination);
106
        event(new ChunkAdded($chunk));
0 ignored issues
show
Bug introduced by
The function event was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

106
        /** @scrutinizer ignore-call */ 
107
        event(new ChunkAdded($chunk));
Loading history...
107
108
        return $chunk;
109
    }
110
111
    /**
112
     * Delete all chunks and, once empty, delete the folder.
113
     *
114
     * @param \Jobtech\LaravelChunky\Chunk $chunk
115
     * @return bool
116
     */
117
    public function deleteChunk(Chunk $chunk): bool
118
    {
119
        if (! $this->filesystem()->disk($this->disk())->exists($chunk->getPath())) {
120
            return true;
121
        }
122
123
        $deleted = $this->filesystem()->disk($this->disk())->delete($chunk->getPath());
124
125
        if ($deleted) {
126
            event(new ChunkDeleted($chunk));
0 ignored issues
show
Bug introduced by
The function event was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

126
            /** @scrutinizer ignore-call */ 
127
            event(new ChunkDeleted($chunk));
Loading history...
127
        }
128
129
        return $deleted;
130
    }
131
132
    /**
133
     * Delete all chunks and, once empty, delete the folder.
134
     *
135
     * @param string $folder
136
     *
137
     * @return bool
138
     */
139
    public function delete(string $folder): bool
140
    {
141
        $folder = $this->path($folder);
142
143
        if (! $this->filesystem()->disk($this->disk())->exists($folder)) {
144
            return true;
145
        }
146
147
        foreach ($this->listChunks($folder) as $chunk) {
148
            $this->deleteChunk($chunk);
149
        }
150
151
        return $this->filesystem()->disk($this->disk())
152
            ->deleteDirectory($folder);
153
    }
154
155
    /**
156
     * Concatenate all chunks into final merge.
157
     *
158
     * @param string $chunk
159
     * @param array $chunks
160
     * @return bool
161
     */
162
    public function concatenate(string $chunk, array $chunks): bool
163
    {
164
        $this->filesystem()->disk($this->disk())->addPlugin(new Concatenate);
0 ignored issues
show
Bug introduced by
The method addPlugin() does not exist on Illuminate\Contracts\Filesystem\Filesystem. It seems like you code against a sub-type of Illuminate\Contracts\Filesystem\Filesystem such as Illuminate\Filesystem\FilesystemAdapter. ( Ignorable by Annotation )

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

164
        $this->filesystem()->disk($this->disk())->/** @scrutinizer ignore-call */ addPlugin(new Concatenate);
Loading history...
165
166
        return $this->filesystem()->disk($this->disk())->concatenate($chunk, ...$chunks);
0 ignored issues
show
Bug introduced by
The method concatenate() does not exist on Illuminate\Contracts\Filesystem\Filesystem. It seems like you code against a sub-type of Illuminate\Contracts\Filesystem\Filesystem such as Illuminate\Filesystem\FilesystemAdapter. ( Ignorable by Annotation )

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

166
        return $this->filesystem()->disk($this->disk())->/** @scrutinizer ignore-call */ concatenate($chunk, ...$chunks);
Loading history...
167
    }
168
}
169