ChunksFilesystem::deleteChunk()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 13
rs 10
1
<?php
2
3
namespace Jobtech\LaravelChunky\Support;
4
5
use Illuminate\Contracts\Filesystem\FileNotFoundException;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Collection;
8
use Illuminate\Support\Str;
9
use Jobtech\LaravelChunky\Chunk;
10
use Jobtech\LaravelChunky\Events\ChunkAdded;
11
use Jobtech\LaravelChunky\Events\ChunkDeleted;
12
use Jobtech\LaravelChunky\Exceptions\ChunkyException;
13
use Keven\Flysystem\Concatenate\Concatenate;
14
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...
15
16
class ChunksFilesystem extends Filesystem
17
{
18
    /**
19
     * @param string $folder
20
     * @return Collection
21
     */
22
    public function listChunks(string $folder): Collection
23
    {
24
        $folder = $this->path($folder);
25
        $files = $this->list($folder);
26
27
        return collect($files)
28
            ->map(function ($path) use ($folder, $files) {
29
                $filename = str_replace($folder.DIRECTORY_SEPARATOR, '', $path);
30
                $exploded_name = explode('_', $filename);
31
32
                $index = array_shift($exploded_name);
33
                $last = count($files) - 1 == $index;
34
35
                return new Chunk(intval($index), $path, $this->disk(), $last);
36
            })->sortBy(function (Chunk $chunk) {
37
                return $chunk->getIndex();
38
            })->values();
39
    }
40
41
    /**
42
     * @return array
43
     */
44
    public function chunkFolders(): array
45
    {
46
        return $this->filesystem()->disk(
47
            $this->disk()
48
        )->directories(
49
            $this->folder()
50
        );
51
    }
52
53
    /**
54
     * @param string $folder
55
     * @return int
56
     */
57
    public function chunksCount(string $folder): int
58
    {
59
        return count($this->list($this->path($folder)));
60
    }
61
62
    /**
63
     * @param string $path
64
     * @return int
65
     */
66
    public function chunkSize(string $path): int
67
    {
68
        return $this->filesystem()->disk($this->disk())->size($this->path($path));
69
    }
70
71
    /**
72
     * @param string $path
73
     * @return resource|null
74
     * @throws FileNotFoundException
75
     */
76
    public function readChunk(string $path)
77
    {
78
        return $this->filesystem()->disk($this->disk())->readStream($this->path($path));
79
    }
80
81
    /**
82
     * @param Chunk $chunk
83
     * @param string $folder
84
     * @param array $options
85
     *
86
     * @return Chunk
87
     */
88
    public function store(Chunk $chunk, string $folder, $options = []): Chunk
89
    {
90
        if (! $chunk->getOriginalPath() instanceof File) {
91
            throw new ChunkyException('Path must be a file');
92
        }
93
94
        // Build destination
95
        $suffix = Str::endsWith($folder, DIRECTORY_SEPARATOR) ? '' : DIRECTORY_SEPARATOR;
96
        $destination = $this->path($folder.$suffix.$chunk->getSlug());
97
98
        // Copy file
99
        $file = fopen($chunk->getPath(), 'r');
100
101
        Arr::pull($options, 'disk');
102
        $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

102
        $this->filesystem()->disk($this->disk())->put($destination, /** @scrutinizer ignore-type */ $file, $options);
Loading history...
103
        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

103
        fclose(/** @scrutinizer ignore-type */ $file);
Loading history...
104
105
        // Return chunk
106
        $chunk->setPath($destination);
107
        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

107
        /** @scrutinizer ignore-call */ 
108
        event(new ChunkAdded($chunk));
Loading history...
108
109
        return $chunk;
110
    }
111
112
    /**
113
     * Delete all chunks and, once empty, delete the folder.
114
     *
115
     * @param Chunk $chunk
116
     * @return bool
117
     */
118
    public function deleteChunk(Chunk $chunk): bool
119
    {
120
        if (! $this->filesystem()->disk($this->disk())->exists($chunk->getPath())) {
121
            return true;
122
        }
123
124
        $deleted = $this->filesystem()->disk($this->disk())->delete($chunk->getPath());
125
126
        if ($deleted) {
127
            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

127
            /** @scrutinizer ignore-call */ 
128
            event(new ChunkDeleted($chunk));
Loading history...
128
        }
129
130
        return $deleted;
131
    }
132
133
    /**
134
     * Delete all chunks and, once empty, delete the folder.
135
     *
136
     * @param string $folder
137
     *
138
     * @return bool
139
     */
140
    public function delete(string $folder): bool
141
    {
142
        $folder = $this->path($folder);
143
144
        if (! $this->filesystem()->disk($this->disk())->exists($folder)) {
145
            return true;
146
        }
147
148
        foreach ($this->listChunks($folder) as $chunk) {
149
            $this->deleteChunk($chunk);
150
        }
151
152
        return $this->filesystem()->disk($this->disk())
153
            ->deleteDirectory($folder);
154
    }
155
156
    /**
157
     * Concatenate all chunks into final merge.
158
     *
159
     * @param string $chunk
160
     * @param array $chunks
161
     * @return bool
162
     */
163
    public function concatenate(string $chunk, array $chunks): bool
164
    {
165
        $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

165
        $this->filesystem()->disk($this->disk())->/** @scrutinizer ignore-call */ addPlugin(new Concatenate);
Loading history...
166
167
        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

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