OverwriteFileHandler   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 8
dl 0
loc 51
rs 10
c 0
b 0
f 0

2 Methods

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