OverwriteAction::overwriteAction()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 15

Duplication

Lines 23
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 23
loc 23
rs 9.0856
cc 2
eloc 15
nc 2
nop 4
1
<?php
2
3
/*
4
 * This file is part of the BenGorFile package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace BenGorFile\FileBundle\Controller;
14
15
use BenGorFile\File\Application\Command\Overwrite\OverwriteFileCommand;
16
use BenGorFile\File\Infrastructure\Application\FileCommandBus;
17
use Symfony\Component\HttpFoundation\Request;
18
19
/**
20
 * BenGor file's overwrite action trait.
21
 *
22
 * @author Beñat Espiña <[email protected]>
23
 */
24 View Code Duplication
trait OverwriteAction
0 ignored issues
show
Duplication introduced by
This class 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...
25
{
26
    /**
27
     * Overwrite action.
28
     *
29
     * @param string         $anId        The file id
30
     * @param Request        $aRequest    The request
31
     * @param FileCommandBus $aCommandBus The command bus
32
     * @param string         $aProperty   The file property that want to get from request
33
     *
34
     * @return array
35
     */
36
    public function overwriteAction($anId, Request $aRequest, FileCommandBus $aCommandBus, $aProperty)
37
    {
38
        if (false === $aRequest->files->has($aProperty)) {
39
            throw new \InvalidArgumentException(sprintf('Given %s property is not in the request', $aProperty));
40
        }
41
42
        $uploadedFile = $aRequest->files->get($aProperty);
43
        $command = new OverwriteFileCommand(
44
            $anId,
45
            $uploadedFile->getClientOriginalName(),
46
            file_get_contents($uploadedFile->getPathname()),
47
            $uploadedFile->getMimeType()
48
        );
49
        $aCommandBus->handle($command);
50
51
        $aRequest->files->remove($aProperty);
52
53
        return [
54
            'id'        => $command->id(),
55
            'filename'  => $uploadedFile->getClientOriginalName(),
56
            'mime_type' => $uploadedFile->getMimeType(),
57
        ];
58
    }
59
}
60