Completed
Push — master ( 985338...fab147 )
by Beñat
03:54
created

ByHashUploadFileHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
lcom 0
cbo 8
dl 62
loc 62
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 6 1
A __invoke() 17 17 3

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
/*
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\File\Application\Command\Upload;
14
15
use BenGorFile\File\Domain\Model\FileAlreadyExistsException;
16
use BenGorFile\File\Domain\Model\FileFactory;
17
use BenGorFile\File\Domain\Model\FileId;
18
use BenGorFile\File\Domain\Model\FileMimeType;
19
use BenGorFile\File\Domain\Model\FileName;
20
use BenGorFile\File\Domain\Model\FileRepository;
21
use BenGorFile\File\Domain\Model\Filesystem;
22
23
/**
24
 * Upload file by hashing handler class.
25
 *
26
 * @author Beñat Espiña <[email protected]>
27
 * @author Gorka Laucirica <[email protected]>
28
 */
29 View Code Duplication
class ByHashUploadFileHandler
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...
30
{
31
    /**
32
     * The file factory.
33
     *
34
     * @var FileFactory
35
     */
36
    private $factory;
37
38
    /**
39
     * The filesystem.
40
     *
41
     * @var Filesystem
42
     */
43
    private $filesystem;
44
45
    /**
46
     * The file repository.
47
     *
48
     * @var FileRepository
49
     */
50
    private $repository;
51
52
    /**
53
     * Constructor.
54
     *
55
     * @param Filesystem     $filesystem  The filesystem
56
     * @param FileRepository $aRepository The file repository
57
     * @param FileFactory    $aFactory    The file factory
58
     */
59
    public function __construct(Filesystem $filesystem, FileRepository $aRepository, FileFactory $aFactory)
60
    {
61
        $this->factory = $aFactory;
62
        $this->filesystem = $filesystem;
63
        $this->repository = $aRepository;
64
    }
65
66
    /**
67
     * Handles the given command.
68
     *
69
     * @param UploadFileCommand $aCommand The command
70
     *
71
     * @throws FileAlreadyExistsException when file is already exists
72
     */
73
    public function __invoke(UploadFileCommand $aCommand)
74
    {
75
        $id = new FileId($aCommand->id());
76
        $file = $this->repository->fileOfId($id);
77
        if (null !== $file) {
78
            throw new FileAlreadyExistsException();
79
        }
80
        $name = FileName::fromHash($aCommand->name());
81
        if (true === $this->filesystem->has($name)) {
82
            throw new FileAlreadyExistsException();
83
        }
84
85
        $this->filesystem->write($name, $aCommand->uploadedFile());
86
        $file = $this->factory->build($id, $name, new FileMimeType($aCommand->mimeType()));
87
88
        $this->repository->persist($file);
89
    }
90
}
91