Passed
Pull Request — master (#71)
by
unknown
03:51
created

PluploadBaseHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
nc 1
cc 1
nop 1
crap 1
1
<?php
2
3
namespace CodingSocks\UploadHandler\Driver;
4
5
use Closure;
6
use CodingSocks\UploadHandler\Helper\ChunkHelpers;
7
use CodingSocks\UploadHandler\Identifier\Identifier;
8
use CodingSocks\UploadHandler\Range\PluploadRange;
9
use CodingSocks\UploadHandler\Response\PercentageJsonResponse;
10
use CodingSocks\UploadHandler\StorageConfig;
11
use Illuminate\Http\Request;
12
use Illuminate\Http\UploadedFile;
13
use InvalidArgumentException;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
16
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
17
18
class PluploadBaseHandler extends BaseHandler
19
{
20 1
    use ChunkHelpers;
21
22
    /**
23
     * @var \CodingSocks\UploadHandler\Identifier\Identifier
24
     */
25
    private $identifier;
26
27
    /**
28
     * PluploadDriver constructor.
29
     *
30
     * @param \CodingSocks\UploadHandler\Identifier\Identifier $identifier
31
     */
32 19
    public function __construct(Identifier $identifier)
33
    {
34 19
        $this->identifier = $identifier;
35 19
    }
36
37
38
    /**
39
     * @inheritDoc
40
     */
41 18
    public function handle(Request $request, StorageConfig $config, Closure $fileUploaded = null): Response
42
    {
43 18
        if ($this->isRequestMethodIn($request, [Request::METHOD_POST])) {
44 9
            return $this->save($request, $config, $fileUploaded);
45
        }
46
47 9
        throw new MethodNotAllowedHttpException([
48 9
            Request::METHOD_POST,
49
        ]);
50
    }
51
52
    /**
53
     * @param \Illuminate\Http\Request $request
54
     * @param \CodingSocks\UploadHandler\StorageConfig $config
55
     * @param \Closure|null $fileUploaded
56
     *
57
     * @return mixed
58
     */
59 9
    private function save(Request $request, StorageConfig $config, ?Closure $fileUploaded)
60
    {
61 9
        $file = $request->file('file');
62
63 9
        $this->validateUploadedFile($file);
0 ignored issues
show
Bug introduced by
It seems like $file defined by $request->file('file') on line 61 can also be of type array; however, CodingSocks\UploadHandle...:validateUploadedFile() does only seem to accept null|object<Illuminate\Http\UploadedFile>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
64
65 7
        $this->validateChunkRequest($request);
66
67 4
        return $this->saveChunk($file, $request, $config, $fileUploaded);
0 ignored issues
show
Bug introduced by
It seems like $file defined by $request->file('file') on line 61 can also be of type array or null; however, CodingSocks\UploadHandle...aseHandler::saveChunk() does only seem to accept object<Illuminate\Http\UploadedFile>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
68
    }
69
70
    /**
71
     * @param \Illuminate\Http\Request $request
72
     */
73 7
    private function validateChunkRequest(Request $request): void
74
    {
75 7
        $request->validate([
0 ignored issues
show
Bug introduced by
The call to validate() misses a required argument $...$params.

This check looks for function calls that miss required arguments.

Loading history...
76 7
            'name' => 'required',
77
            'chunk' => 'required|integer',
78
            'chunks' => 'required|integer',
79
        ]);
80 4
    }
81
82
    /**
83
     * @param \Illuminate\Http\UploadedFile $file
84
     * @param \Illuminate\Http\Request $request
85
     * @param \CodingSocks\UploadHandler\StorageConfig $config
86
     * @param \Closure|null $fileUploaded
87
     *
88
     * @return \Symfony\Component\HttpFoundation\Response
89
     */
90 4
    private function saveChunk(UploadedFile $file, Request $request, StorageConfig $config, Closure $fileUploaded = null): Response
91
    {
92
        try {
93 4
            $range = new PluploadRange($request);
94
        } catch (InvalidArgumentException $e) {
95
            throw new BadRequestHttpException($e->getMessage(), $e);
96
        }
97
98 4
        $uid = $this->identifier->generateFileIdentifier($range->getTotal(), $file->getClientOriginalName());
99
100 4
        $chunks = $this->storeChunk($config, $range, $file, $uid);
101
102 4
        if (!$range->isLast()) {
103 2
            return new PercentageJsonResponse($range->getPercentage());
104
        }
105
106 2
        $targetFilename = $file->hashName();
107
108 2
        $path = $this->mergeChunks($config, $chunks, $targetFilename);
109
110 2
        if ($config->sweep()) {
111
            $this->deleteChunkDirectory($config, $uid);
112
        }
113
114 2
        $this->triggerFileUploadedEvent($config->getDisk(), $path, $fileUploaded);
115
116 2
        return new PercentageJsonResponse($range->getPercentage());
117
    }
118
}
119