Test Failed
Push — master ( 87b409...65f6df )
by Filippo
21:44 queued 16:15
created

Chunk::getSlug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Jobtech\LaravelChunky;
4
5
use Illuminate\Contracts\Support\Arrayable;
6
use Illuminate\Contracts\Support\Jsonable;
7
use Illuminate\Contracts\Support\Responsable;
8
use Illuminate\Http\Response;
9
use Illuminate\Http\UploadedFile;
10
use Illuminate\Support\Arr;
11
use Illuminate\Support\Str;
12
use Illuminate\Support\Traits\ForwardsCalls;
13
use Jobtech\LaravelChunky\Http\Resources\ChunkResource;
14
use Symfony\Component\HttpFoundation\File\File;
15
16
class Chunk implements Arrayable, Jsonable, Responsable
17
{
18
    use ForwardsCalls;
19
20
    /** @var bool */
21
    private $show_file_info = true;
22
23
    /** @var int */
24
    private $index;
25
26
    /** @var \Symfony\Component\HttpFoundation\File\File|string */
27
    private $path;
28
29
    /** @var string|null */
30
    private $disk;
31
32
    /** @var bool */
33
    private $last;
34
35
    public function __construct(int $index, $path, $disk = null, $last = false)
36
    {
37
        $this->index = $index;
38
        $this->path = $path;
39
        $this->disk = $disk;
40
        $this->last = $last;
41
    }
42
43
    /**
44
     * @return int
45
     */
46
    public function getIndex(): int
47
    {
48
        return $this->index;
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function getPath(): string
55
    {
56
        if ($this->path instanceof File) {
57
            return $this->path->getRealPath();
58
        }
59
60
        return $this->path;
61
    }
62
63
    /**
64
     * @return mixed
65
     */
66
    public function getOriginalPath()
67
    {
68
        return $this->path;
69
    }
70
71
    /**
72
     * @param $path
73
     */
74
    public function setPath($path)
75
    {
76
        $this->path = $path;
77
    }
78
79
    /**
80
     * @param string|null $suffix
81
     * @return string
82
     */
83
    public function getFilename($suffix = null): string
84
    {
85
        if ($this->path instanceof UploadedFile) {
86
            return basename($this->path->getClientOriginalName(), $suffix);
87
        } elseif ($this->path instanceof File) {
88
            return $this->path->getBasename($suffix);
89
        }
90
91
        return basename($this->path, $suffix);
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getName(): string
98
    {
99
        return pathinfo(
100
            $this->getFilename($this->getExtension()),
0 ignored issues
show
Bug introduced by
It seems like $this->getExtension() can also be of type string[]; however, parameter $suffix of Jobtech\LaravelChunky\Chunk::getFilename() does only seem to accept null|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

100
            $this->getFilename(/** @scrutinizer ignore-type */ $this->getExtension()),
Loading history...
101
            PATHINFO_FILENAME
102
        );
103
    }
104
105
    /**
106
     * @return string|string[]
107
     */
108
    public function getExtension()
109
    {
110
        return pathinfo($this->getFilename(), PATHINFO_EXTENSION);
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function getSlug(): string
117
    {
118
        return $this->index.'_'.Str::slug($this->getName()).'.'.$this->getExtension();
0 ignored issues
show
Bug introduced by
Are you sure $this->getExtension() of type string|string[] can be used in concatenation? ( Ignorable by Annotation )

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

118
        return $this->index.'_'.Str::slug($this->getName()).'.'./** @scrutinizer ignore-type */ $this->getExtension();
Loading history...
119
    }
120
121
    /**
122
     * Retrieve the chunk file disk.
123
     *
124
     * @return string|null
125
     */
126
    public function getDisk(): ?string
127
    {
128
        return $this->disk;
129
    }
130
131
    /**
132
     * Set the chunk file disk.
133
     *
134
     * @param string|null $disk
135
     */
136
    public function setDisk($disk = null)
137
    {
138
        $this->disk = $disk;
139
    }
140
141
    /**
142
     * @return bool
143
     */
144
    public function isLast(): bool
145
    {
146
        return $this->last;
147
    }
148
149
    /**
150
     * @param bool $last
151
     */
152
    public function setLast(bool $last): void
153
    {
154
        $this->last = $last;
155
    }
156
157
    /**
158
     * If this method is called, when a chunk is turned to array, the file path and real path
159
     * will be omitted.
160
     *
161
     * @return $this
162
     */
163
    public function hideFileInfo()
164
    {
165
        $this->show_file_info = false;
166
167
        return $this;
168
    }
169
170
    /**
171
     * If this method is called, when a chunk is turned to array, the file path and real path
172
     * will be included.
173
     *
174
     * @return $this
175
     */
176
    public function showFileInfo()
177
    {
178
        $this->show_file_info = true;
179
180
        return $this;
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    public function toArray(): array
187
    {
188
        $extension = $this->getExtension();
189
190
        $data = [
191
            'name'      => $this->getName(),
192
            'extension' => $extension,
193
            'index'     => $this->getIndex(),
194
            'last'      => $this->isLast(),
195
        ];
196
197
        if ($this->show_file_info) {
198
            $data['file'] = $this->getFilename();
199
            $data['path'] = $this->getPath();
200
        }
201
202
        return $data;
203
    }
204
205
    /**
206
     * {@inheritdoc}
207
     */
208
    public function toJson($options = 0): string
209
    {
210
        return json_encode($this->toArray(), $options);
211
    }
212
213
    /**
214
     * {@inheritdoc}
215
     */
216
    public function toResponse($request)
217
    {
218
        if ($request->wantsJson()) {
219
            return $this->toResource();
220
        }
221
222
        return new Response(
223
            $this->toJson(),
224
            Response::HTTP_CREATED
225
        );
226
    }
227
228
    /**
229
     * Transforms the current model into a json resource.
230
     */
231
    public function toResource()
232
    {
233
        /** @var \Illuminate\Http\Resources\Json\JsonResource $resource */
234
        $resource = config('chunky.resource', ChunkResource::class);
235
236
        return new $resource($this);
237
    }
238
239
    /**
240
     * @param \Symfony\Component\HttpFoundation\File\File|string $file
241
     * @param int $index
242
     * @param array $options
243
     *
244
     * @return \Jobtech\LaravelChunky\Chunk
245
     */
246
    public static function create($file, int $index, $options = [])
247
    {
248
        return new static($index, $file, Arr::pull($options, 'disk'));
249
    }
250
}
251