AddFileHandler::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 1
dl 0
loc 18
ccs 13
cts 13
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace PersonalGalaxy\Files\Handler;
5
6
use PersonalGalaxy\Files\{
7
    Command\AddFile,
8
    Entity\File,
9
    Entity\File\Name,
10
    Repository\FileRepository,
11
    Repository\FolderRepository,
12
    Exception\FolderNotFound,
13
};
14
use Innmind\Filesystem\{
15
    Adapter,
16
    File\File as RawFile,
17
};
18
19
final class AddFileHandler
20
{
21
    private $files;
22
    private $folders;
23
    private $filesystem;
24
25 2
    public function __construct(
26
        FileRepository $files,
27
        FolderRepository $folders,
28
        Adapter $filesystem
29
    ) {
30 2
        $this->files = $files;
31 2
        $this->folders = $folders;
32 2
        $this->filesystem = $filesystem;
33 2
    }
34
35 2
    public function __invoke(AddFile $wished): void
36
    {
37 2
        if (!$this->folders->has($wished->folder())) {
38 1
            throw new FolderNotFound($wished->folder());
39
        }
40
41 1
        $file = File::add(
42 1
            $wished->identity(),
43 1
            new Name((string) $wished->file()->name()),
44 1
            $wished->folder(),
45 1
            $wished->file()->mediaType()
46
        );
47 1
        $this->filesystem->add(new RawFile(
48 1
            (string) $wished->identity(),
49 1
            $wished->file()->content(),
50 1
            $wished->file()->mediaType()
51
        ));
52 1
        $this->files->add($file);
53 1
    }
54
}
55