Code Duplication    Length = 36-46 lines in 2 locations

src/BenGorFile/FileBundle/Controller/OverwriteAction.php 1 location

@@ 24-59 (lines=36) @@
21
 *
22
 * @author Beñat Espiña <[email protected]>
23
 */
24
trait OverwriteAction
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

src/BenGorFile/FileBundle/Controller/UploadAction.php 1 location

@@ 24-69 (lines=46) @@
21
 *
22
 * @author Beñat Espiña <[email protected]>
23
 */
24
trait UploadAction
25
{
26
    /**
27
     * Upload action.
28
     *
29
     * @param Request        $aRequest    The request
30
     * @param FileCommandBus $aCommandBus The command bus
31
     * @param string         $aProperty   The file property that want to get from request
32
     *
33
     * @return array
34
     */
35
    public function upload(Request $aRequest, FileCommandBus $aCommandBus, $aProperty)
36
    {
37
        if (false === $aRequest->files->has($aProperty)) {
38
            throw new \InvalidArgumentException(sprintf('Given %s property is not in the request', $aProperty));
39
        }
40
41
        $command = $this->command();
42
43
        $uploadedFile = $aRequest->files->get($aProperty);
44
        $command = new $command(
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
    /**
61
     * Gets the FQCN command name.
62
     *
63
     * @return string
64
     */
65
    private function command()
66
    {
67
        return UploadFileCommand::class;
68
    }
69
}
70