RemoveFileHandler::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
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