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

MonolithHandler::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 3
cts 5
cp 0.6
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2.2559
1
<?php
2
3
namespace CodingSocks\UploadHandler\Driver;
4
5
use Closure;
6
use CodingSocks\UploadHandler\Response\PercentageJsonResponse;
7
use CodingSocks\UploadHandler\StorageConfig;
8
use Illuminate\Http\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
11
12
class MonolithHandler extends BaseHandler
13
{
14
    /**
15
     * @var string
16
     */
17
    private $fileParam;
18
19
    /**
20
     * MonolithDriver constructor.
21
     *
22
     * @param array $config
23
     */
24 7
    public function __construct($config)
25
    {
26 7
        $this->fileParam = $config['param'];
27 7
    }
28
29
    /**
30
     * {@inheritDoc}
31
     */
32 4 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...
33
    {
34 4
        if ($request->isMethod(Request::METHOD_POST)) {
35 4
            return $this->save($request, $config, $fileUploaded);
36
        }
37
38
        throw new MethodNotAllowedHttpException([
39
            Request::METHOD_POST,
40
        ]);
41
    }
42
43
    /**
44
     * @param \Illuminate\Http\Request $request
45
     * @param \CodingSocks\UploadHandler\StorageConfig $config
46
     * @param \Closure|null $fileUploaded
47
     *
48
     * @return \Symfony\Component\HttpFoundation\Response
49
     */
50 4
    public function save(Request $request, StorageConfig $config, Closure $fileUploaded = null): Response
51
    {
52 4
        $file = $request->file($this->fileParam);
53
54 4
        $this->validateUploadedFile($file);
0 ignored issues
show
Bug introduced by
It seems like $file defined by $request->file($this->fileParam) on line 52 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...
55
56 2
        $path = $file->store($config->getMergedDirectory(), [
57 2
            'disk' => $config->getDisk(),
58
        ]);
59
60 2
        $this->triggerFileUploadedEvent($config->getDisk(), $path, $fileUploaded);
61
62 2
        return new PercentageJsonResponse(100);
63
    }
64
}
65