Passed
Pull Request — master (#156)
by David
07:26 queued 02:16
created

FileUploader::upload()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 17
rs 9.9666
c 1
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of Monsieur Biz' Rich Editor plugin for Sylius.
5
 *
6
 * (c) Monsieur Biz <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace MonsieurBiz\SyliusRichEditorPlugin\Uploader;
15
16
use Symfony\Component\HttpFoundation\File\Exception\FileException;
17
use Symfony\Component\HttpFoundation\File\File;
18
use Symfony\Component\HttpFoundation\File\UploadedFile;
19
20
class FileUploader
21
{
22
    public const FILE_TYPE_DOCUMENT = 'document';
23
    public const FILE_TYPE_IMAGE = 'image';
24
    public const FILE_TYPE_VIDEO = 'video';
25
26
    /**
27
     * @var string
28
     */
29
    private $fileTargetPath;
30
31
    /**
32
     * @var string
33
     */
34
    private $imageTargetPath;
35
36
    /**
37
     * @var string
38
     */
39
    private $publicDirectory;
40
41
    /**
42
     * @param string $fileTargetPath
43
     * @param string $imageTargetPath
44
     * @param string $publicDirectory
45
     */
46
    public function __construct(string $fileTargetPath, string $imageTargetPath, string $publicDirectory)
47
    {
48
        $this->fileTargetPath = $fileTargetPath;
49
        $this->imageTargetPath = $imageTargetPath;
50
        $this->publicDirectory = $publicDirectory;
51
    }
52
53
    /**
54
     * Upload a file and return the path to it.
55
     *
56
     * @param UploadedFile $file
57
     * @param string|null $type
58
     *
59
     * @return string
60
     */
61
    public function upload(UploadedFile $file, ?string $type = null): string
62
    {
63
        $type = $type ?? self::FILE_TYPE_DOCUMENT;
64
65
        // See @https://symfony.com/doc/4.4/controller/upload_file.html
66
        $originalFilename = pathinfo($file->getClientOriginalName(), \PATHINFO_FILENAME);
67
        $safeFilename = transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()', $originalFilename);
0 ignored issues
show
Bug introduced by
It seems like $originalFilename can also be of type array; however, parameter $subject of transliterator_transliterate() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
        $safeFilename = transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()', /** @scrutinizer ignore-type */ $originalFilename);
Loading history...
68
        $fileName = $safeFilename . '-' . uniqid() . '.' . $file->guessExtension();
69
70
        try {
71
            $file = $file->move($this->getTargetDirectory($type), $fileName);
72
        } catch (FileException $e) {
73
            return '';
74
        }
75
76
        // Generate path from public folder
77
        return $this->getFinalPath($file, $type);
78
    }
79
80
    /**
81
     * The directory to write the file.
82
     *
83
     * @param string $type
84
     *
85
     * @return string
86
     */
87
    private function getTargetDirectory(string $type): string
88
    {
89
        switch ($type) {
90
            case self::FILE_TYPE_IMAGE:
91
                return $this->publicDirectory . $this->imageTargetPath;
92
            default:
93
                return $this->publicDirectory . $this->fileTargetPath;
94
        }
95
    }
96
97
    /**
98
     * @param File $file
99
     * @param string $type
100
     *
101
     * @return string
102
     */
103
    private function getFinalPath(File $file, string $type): string
104
    {
105
        switch ($type) {
106
            case self::FILE_TYPE_IMAGE:
107
                return $file->getBasename();
108
            default:
109
                return str_replace($this->publicDirectory, '', $file->getPathname());
110
        }
111
    }
112
}
113