FileUploader::getTargetDirectory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: leazygomalas
5
 * Date: 05/02/2019
6
 * Time: 11:38
7
 */
8
9
namespace App\Service;
10
11
use Symfony\Component\HttpFoundation\File\Exception\FileException;
12
use Symfony\Component\HttpFoundation\File\UploadedFile;
13
14
class FileUploader
15
{
16
    private $targetDirectory;
17
18
    public function __construct($targetDirectory)
19
    {
20
        $this->targetDirectory = $targetDirectory;
21
    }
22
23
    public function upload(UploadedFile $file)
24
    {
25
        $fileName = md5(uniqid()).'.'.$file->guessExtension();
26
27
        try {
28
            $file->move($this->getTargetDirectory(), $fileName);
29
        } catch (FileException $e) {
30
            // ... handle exception if something happens during file upload
31
        }
32
33
        return $fileName;
34
    }
35
36
    public function getTargetDirectory()
37
    {
38
        return $this->targetDirectory;
39
    }
40
}
41