ChunkyRequestHelpers::lastIndexFrom()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 2
eloc 5
c 2
b 1
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
namespace Jobtech\LaravelChunky\Concerns;
4
5
use Illuminate\Support\Str;
6
use Jobtech\LaravelChunky\Http\Requests\AddChunkRequest;
7
8
trait ChunkyRequestHelpers
9
{
10
    /**
11
     * Check if folder is a valid string, otherwise guess the folder from the
12
     * request file input.
13
     *
14
     * @param \Jobtech\LaravelChunky\Http\Requests\AddChunkRequest $request
15
     * @param string|null                                          $folder
16
     *
17
     * @return string
18
     */
19
    protected function checkFolder(AddChunkRequest $request, ?string $folder)
20
    {
21
        $file = $request->fileInput();
22
23
        if ($folder !== null) {
24
            return Str::slug($folder);
25
        }
26
27
        return $this->chunkFolderNameFor(
0 ignored issues
show
Bug introduced by
It seems like chunkFolderNameFor() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

27
        return $this->/** @scrutinizer ignore-call */ chunkFolderNameFor(
Loading history...
28
            str_replace(
29
                $file->guessExtension(),
30
                '',
31
                $file->getClientOriginalName()
32
            )
33
        );
34
    }
35
36
    /**
37
     * Retrieve last chunk index from chunk request by calculating the ceil value of
38
     * the total size of the file divided by the size of the single chunk.
39
     *
40
     * @param \Jobtech\LaravelChunky\Http\Requests\AddChunkRequest $request
41
     *
42
     * @return int
43
     */
44
    public function lastIndexFrom(AddChunkRequest $request): int
45
    {
46
        $total_size = $request->totalSizeInput();
47
        $chunk_size = $request->chunkSizeInput();
48
49
        if ($total_size < $chunk_size) {
50
            // In this case usually it means that there's only a chunk
51
            return 1;
52
        }
53
54
        return ceil($total_size / $chunk_size);
0 ignored issues
show
Bug Best Practice introduced by
The expression return ceil($total_size / $chunk_size) returns the type double which is incompatible with the type-hinted return integer.
Loading history...
55
    }
56
57
    /**
58
     * Check if current index refers to the last chunk.
59
     *
60
     * @param \Jobtech\LaravelChunky\Http\Requests\AddChunkRequest $request
61
     *
62
     * @return bool
63
     */
64
    public function isLastIndex(AddChunkRequest $request): bool
65
    {
66
        $last_index = $this->lastIndexFrom($request)
67
            + ($this->settings->defaultIndex() - 1);
68
69
        return $request->indexInput() == $last_index;
70
    }
71
}
72