Passed
Pull Request — master (#156)
by David
13:11
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_LIIP_IMAGE = 'liip-image';
25
    public const FILE_TYPE_VIDEO = 'liip-image';
26
27
    /**
28
     * @var string
29
     */
30
    private $fileTargetPath;
31
32
    /**
33
     * @var string
34
     */
35
    private $imageTargetPath;
36
37
    /**
38
     * @var string
39
     */
40
    private $publicDirectory;
41
42
    /**
43
     * @param string $fileTargetPath
44
     * @param string $imageTargetPath
45
     * @param string $publicDirectory
46
     */
47
    public function __construct(string $fileTargetPath, string $imageTargetPath, string $publicDirectory)
48
    {
49
        $this->fileTargetPath = $fileTargetPath;
50
        $this->imageTargetPath = $imageTargetPath;
51
        $this->publicDirectory = $publicDirectory;
52
    }
53
54
    /**
55
     * Upload a file and return the path to it.
56
     *
57
     * @param UploadedFile $file
58
     * @param string $type
59
     *
60
     * @return mixed|string
61
     */
62
    public function upload(UploadedFile $file, ?string $type = self::FILE_TYPE_DOCUMENT)
63
    {
64
        $type = $type ?? self::FILE_TYPE_DOCUMENT;
65
66
        // See @https://symfony.com/doc/4.4/controller/upload_file.html
67
        $originalFilename = pathinfo($file->getClientOriginalName(), \PATHINFO_FILENAME);
68
        $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

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