FileUploader   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 9
dl 0
loc 25
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getTargetDirectory() 0 3 1
A upload() 0 11 2
A __construct() 0 3 1
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