Passed
Push — master ( 335552...de70eb )
by László
03:52
created

MonolithHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 18.87 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 10
loc 53
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A save() 0 14 1
A handle() 10 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 16
    public function __construct($config)
25
    {
26 16
        $this->fileParam = $config['param'];
27 16
    }
28
29
    /**
30
     * {@inheritDoc}
31
     */
32 13 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 13
        if ($request->isMethod(Request::METHOD_POST)) {
35 4
            return $this->save($request, $config, $fileUploaded);
36
        }
37
38 9
        throw new MethodNotAllowedHttpException([
39 9
            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);
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