Passed
Push — master ( 6248da...8ce640 )
by László
02:40
created

DropzoneHandler::__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
cc 1
nc 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\Range\DropzoneRange;
8
use CodingSocks\UploadHandler\Response\PercentageJsonResponse;
9
use CodingSocks\UploadHandler\StorageConfig;
10
use Illuminate\Http\Request;
11
use Illuminate\Http\UploadedFile;
12
use InvalidArgumentException;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
15
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
16
17
class DropzoneHandler extends BaseHandler
18
{
19 1
    use ChunkHelpers;
20
21
    /**
22
     * @var string
23
     */
24
    private $fileParam;
25
26
    /**
27
     * DropzoneDriver constructor.
28
     *
29
     * @param array $config
30
     */
31 16
    public function __construct($config)
32
    {
33 16
        $this->fileParam = $config['param'];
34 16
    }
35
36
    /**
37
     * {@inheritDoc}
38
     */
39 14 View Code Duplication
    public function handle(Request $request, StorageConfig $config, Closure $fileUploaded = null): Response
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41 14
        if ($this->isRequestMethodIn($request, [Request::METHOD_POST])) {
42 14
            return $this->save($request, $config, $fileUploaded);
43
        }
44
45
        throw new MethodNotAllowedHttpException([
46
            Request::METHOD_POST,
47
        ]);
48
    }
49
50
    /**
51
     * @param \Illuminate\Http\Request $request
52
     * @param \CodingSocks\UploadHandler\StorageConfig $config
53
     * @param \Closure|null $fileUploaded
54
     *
55
     * @return \Symfony\Component\HttpFoundation\Response
56
     */
57 14 View Code Duplication
    public function save(Request $request, StorageConfig $config, Closure $fileUploaded = null): Response
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59 14
        $file = $request->file($this->fileParam);
60
61 14
        $this->validateUploadedFile($file);
0 ignored issues
show
Bug introduced by
It seems like $file defined by $request->file($this->fileParam) on line 59 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...
62
63 12
        if ($this->isMonolithRequest($request)) {
64 2
            return $this->saveMonolith($file, $config, $fileUploaded);
0 ignored issues
show
Bug introduced by
It seems like $file defined by $request->file($this->fileParam) on line 59 can also be of type array or null; however, CodingSocks\UploadHandle...Handler::saveMonolith() 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...
65
        }
66
67 10
        $this->validateChunkRequest($request);
68
69 4
        return $this->saveChunk($file, $request, $config, $fileUploaded);
0 ignored issues
show
Bug introduced by
It seems like $file defined by $request->file($this->fileParam) on line 59 can also be of type array or null; however, CodingSocks\UploadHandle...oneHandler::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...
70
    }
71
72
    /**
73
     * @param \Illuminate\Http\Request $request
74
     *
75
     * @return bool
76
     */
77 12
    private function isMonolithRequest(Request $request): bool
78
    {
79 12
        return $request->post('dzuuid') === null
80 12
            && $request->post('dzchunkindex') === null
81 12
            && $request->post('dztotalfilesize') === null
82 12
            && $request->post('dzchunksize') === null
83 12
            && $request->post('dztotalchunkcount') === null
84 12
            && $request->post('dzchunkbyteoffset') === null;
85
    }
86
87
    /**
88
     * @param \Illuminate\Http\Request $request
89
     */
90 10
    private function validateChunkRequest(Request $request): void
91
    {
92 10
        $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...
93 10
            'dzuuid' => 'required',
94
            'dzchunkindex' => 'required',
95
            'dztotalfilesize' => 'required',
96
            'dzchunksize' => 'required',
97
            'dztotalchunkcount' => 'required',
98
            'dzchunkbyteoffset' => 'required',
99
        ]);
100 4
    }
101
102
    /**
103
     * @param \Illuminate\Http\UploadedFile $file
104
     * @param \CodingSocks\UploadHandler\StorageConfig $config
105
     * @param \Closure|null $fileUploaded
106
     *
107
     * @return \Symfony\Component\HttpFoundation\Response
108
     */
109 2 View Code Duplication
    private function saveMonolith(UploadedFile $file, StorageConfig $config, Closure $fileUploaded = null): Response
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111 2
        $path = $file->store($config->getMergedDirectory(), [
112 2
            'disk' => $config->getDisk(),
113
        ]);
114
115 2
        $this->triggerFileUploadedEvent($config->getDisk(), $path, $fileUploaded);
116
117 2
        return new PercentageJsonResponse(100);
118
    }
119
120
    /**
121
     * @param \Illuminate\Http\UploadedFile $file
122
     * @param \Illuminate\Http\Request $request
123
     * @param \CodingSocks\UploadHandler\StorageConfig $config
124
     * @param \Closure|null $fileUploaded
125
     *
126
     * @return \Symfony\Component\HttpFoundation\Response
127
     */
128 4
    private function saveChunk(UploadedFile $file, Request $request, StorageConfig $config, Closure $fileUploaded = null): Response
129
    {
130
        try {
131 4
            $range = new DropzoneRange(
132 4
                $request,
133 4
                'dzchunkindex',
134 4
                'dztotalchunkcount',
135 4
                'dzchunksize',
136 4
                'dztotalfilesize'
137
            );
138
        } catch (InvalidArgumentException $e) {
139
            throw new BadRequestHttpException($e->getMessage(), $e);
140
        }
141
142 4
        $uid = $request->post('dzuuid');
143
144 4
        $chunks = $this->storeChunk($config, $range, $file, $uid);
0 ignored issues
show
Bug introduced by
It seems like $uid defined by $request->post('dzuuid') on line 142 can also be of type array or null; however, CodingSocks\UploadHandle...nkHelpers::storeChunk() does only seem to accept string, 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...
145
146 4
        if (!$range->isFinished($chunks)) {
147 2
            return new PercentageJsonResponse($range->getPercentage($chunks));
148
        }
149
150 2
        $targetFilename = $file->hashName();
151
152 2
        $path = $this->mergeChunks($config, $chunks, $targetFilename);
153
154 2
        if ($config->sweep()) {
155
            $this->deleteChunkDirectory($config, $uid);
0 ignored issues
show
Bug introduced by
It seems like $uid defined by $request->post('dzuuid') on line 142 can also be of type array or null; however, CodingSocks\UploadHandle...:deleteChunkDirectory() does only seem to accept string, 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...
156
        }
157
158 2
        $this->triggerFileUploadedEvent($config->getDisk(), $path, $fileUploaded);
159
160 2
        return new PercentageJsonResponse(100);
161
    }
162
}
163