FileUploaderService   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A remove() 0 4 1
A upload() 0 7 1
A getFullPath() 0 5 1
A getTargetDir() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
/**
4
 * Copyright (c) 2017 Francois-Xavier Soubirou.
5
 *
6
 * This file is part of ci-report.
7
 *
8
 * ci-report is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * ci-report is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with ci-report. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
declare(strict_types=1);
22
23
namespace App\Service;
24
25
use Symfony\Component\HttpFoundation\File\UploadedFile;
26
27
/**
28
 * File uploader service class. Inspired from Symfony example:
29
 * https://symfony.com/doc/current/controller/upload_file.html.
30
 *
31
 * @category  ci-report app
32
 *
33
 * @author    Francois-Xavier Soubirou <[email protected]>
34
 * @copyright 2017 Francois-Xavier Soubirou
35
 * @license   http://www.gnu.org/licenses/   GPLv3
36
 *
37
 * @see      https://www.ci-report.io
38
 */
39
class FileUploaderService
40
{
41
    /**
42
     * @var string
43
     */
44
    private $targetDir;
45
46
    /**
47
     * Constructor.
48
     *
49
     * @param string $targetDir Directory to save temporary files
50
     */
51
    public function __construct(string $targetDir)
52
    {
53
        $this->targetDir = $targetDir;
54
    }
55
56
    /**
57
     * Move file temporary directory.
58
     *
59
     * @param UploadedFile $file File to move
60
     *
61
     * @return string
62
     */
63
    public function upload(UploadedFile $file): string
64
    {
65
        $fileName = md5(uniqid()).'.'.$file->guessExtension();
66
67
        $file->move($this->getTargetDir(), $fileName);
68
69
        return $fileName;
70
    }
71
72
    /**
73
     * Remove file from temporary directory.
74
     *
75
     * @param string $fileName File to remove
76
     */
77
    public function remove(string $fileName)
78
    {
79
        $fileNameWithoutPath = basename($fileName);
80
        unlink($this->getTargetDir().'/'.$fileNameWithoutPath);
81
    }
82
83
    /**
84
     * Get full path of the file.
85
     *
86
     * @param string $fileName File name
87
     *
88
     * @return string
89
     */
90
    public function getFullPath(string $fileName): string
91
    {
92
        $fileNameWithoutPath = basename($fileName);
93
94
        return $this->targetDir.'/'.$fileNameWithoutPath;
95
    }
96
97
    /**
98
     * Get temporary directory name.
99
     *
100
     * @return string
101
     */
102
    public function getTargetDir(): string
103
    {
104
        return $this->targetDir;
105
    }
106
}
107