RemoveFileHandler   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 6
dl 0
loc 48
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 12 2
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\Remove;
14
15
use BenGorFile\File\Domain\Model\FileDoesNotExistException;
16
use BenGorFile\File\Domain\Model\FileId;
17
use BenGorFile\File\Domain\Model\FileRepository;
18
use BenGorFile\File\Domain\Model\Filesystem;
19
20
/**
21
 * Remove file handler class.
22
 *
23
 * @author Beñat Espiña <[email protected]>
24
 * @author Gorka Laucirica <[email protected]>
25
 */
26
class RemoveFileHandler
27
{
28
    /**
29
     * The filesystem.
30
     *
31
     * @var Filesystem
32
     */
33
    private $filesystem;
34
35
    /**
36
     * The file repository.
37
     *
38
     * @var FileRepository
39
     */
40
    private $repository;
41
42
    /**
43
     * Constructor.
44
     *
45
     * @param Filesystem     $filesystem  The filesystem
46
     * @param FileRepository $aRepository The file repository
47
     */
48
    public function __construct(Filesystem $filesystem, FileRepository $aRepository)
49
    {
50
        $this->filesystem = $filesystem;
51
        $this->repository = $aRepository;
52
    }
53
54
    /**
55
     * Handles the given command.
56
     *
57
     * @param RemoveFileCommand $aCommand The command
58
     *
59
     * @throws FileDoesNotExistException when file is already exists
60
     */
61
    public function __invoke(RemoveFileCommand $aCommand)
62
    {
63
        $id = new FileId($aCommand->id());
64
65
        $file = $this->repository->fileOfId($id);
66
        if (null === $file) {
67
            throw new FileDoesNotExistException();
68
        }
69
        $this->filesystem->delete($file->name());
70
71
        $this->repository->remove($file);
72
    }
73
}
74