Completed
Pull Request — master (#105)
by Kristof
05:46
created

ImageUploaderService::upload()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 25
rs 8.8571
c 1
b 0
f 0
cc 3
eloc 16
nc 3
nop 3
1
<?php
2
3
namespace CultuurNet\UDB3\Media;
4
5
use Broadway\CommandHandling\CommandBusInterface;
6
use Broadway\UuidGenerator\UuidGeneratorInterface;
7
use CultuurNet\UDB3\CommandHandling\Udb3CommandHandler;
8
use CultuurNet\UDB3\Media\Commands\UploadImage;
9
use CultuurNet\UDB3\Media\Properties\MIMEType;
10
use League\Flysystem\FilesystemInterface;
11
use Psr\Log\LoggerAwareInterface;
12
use Psr\Log\LoggerAwareTrait;
13
use Psr\Log\LoggerInterface;
14
use Psr\Log\NullLogger;
15
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
16
use Symfony\Component\HttpFoundation\File\UploadedFile;
17
use ValueObjects\Identity\UUID;
18
use ValueObjects\String\String;
19
20
class ImageUploaderService implements ImageUploaderInterface
21
{
22
    /**
23
     * @var UuidGeneratorInterface
24
     */
25
    protected $uuidGenerator;
26
27
    /**
28
     * @var CommandBusInterface
29
     */
30
    protected $commandBus;
31
32
    /**
33
     * @var string
34
     */
35
    protected $uploadDirectory;
36
37
    /**
38
     * @var FilesystemInterface
39
     */
40
    protected $filesystem;
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
        } else {
68
            $mimeType = MIMEType::fromNative($fileType);
69
        }
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