Completed
Pull Request — master (#105)
by Kristof
13:40 queued 05:31
created

ImageUploaderService::getUploadDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace CultuurNet\UDB3\Media;
4
5
use Broadway\CommandHandling\CommandBusInterface;
6
use Broadway\UuidGenerator\UuidGeneratorInterface;
7
use CultuurNet\UDB3\Media\Commands\UploadImage;
8
use CultuurNet\UDB3\Media\Properties\MIMEType;
9
use League\Flysystem\FilesystemInterface;
10
use Symfony\Component\HttpFoundation\File\UploadedFile;
11
use ValueObjects\Identity\UUID;
12
use ValueObjects\String\String;
13
14
class ImageUploaderService implements ImageUploaderInterface
15
{
16
    /**
17
     * @var UuidGeneratorInterface
18
     */
19
    protected $uuidGenerator;
20
21
    /**
22
     * @var CommandBusInterface
23
     */
24
    protected $commandBus;
25
26
    /**
27
     * @var string
28
     */
29
    protected $uploadDirectory;
30
31
    /**
32
     * @var FilesystemInterface
33
     */
34
    protected $filesystem;
35
36
    /**
37
     * @param UuidGeneratorInterface $uuidGenerator
38
     * @param CommandBusInterface $commandBus
39
     * @param FilesystemInterface $filesystem
40
     * @param $uploadDirectory
41
     */
42
    public function __construct(
43
        UuidGeneratorInterface $uuidGenerator,
44
        CommandBusInterface $commandBus,
45
        FilesystemInterface $filesystem,
46
        $uploadDirectory
47
    ) {
48
        $this->uuidGenerator = $uuidGenerator;
49
        $this->commandBus = $commandBus;
50
        $this->filesystem = $filesystem;
51
        $this->uploadDirectory = $uploadDirectory;
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function upload(UploadedFile $file, String $description, String $copyrightHolder)
58
    {
59
        if (!$file->isValid()) {
60
            throw new \InvalidArgumentException('The file did not upload correctly.');
61
        }
62
63
        $fileType = $file->getMimeType();
64
65
        if (!$fileType) {
66
            throw new \InvalidArgumentException('The type of the uploaded file can not be guessed.');
67
        }
68
69
        $mimeType = MIMEType::fromNative($fileType);
70
71
        $fileId = new UUID($this->uuidGenerator->generate());
72
        $fileName = $fileId . '.' . $file->guessExtension();
73
        $destination = $this->getUploadDirectory() . '/' . $fileName;
74
        $stream = fopen($file->getRealPath(), 'r+');
75
        $this->filesystem->writeStream($destination, $stream);
76
        fclose($stream);
77
78
        return $this->commandBus->dispatch(
79
            new UploadImage($fileId, $mimeType, $description, $copyrightHolder)
80
        );
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86
    public function getUploadDirectory()
87
    {
88
        return $this->uploadDirectory;
89
    }
90
}
91