Completed
Push — master ( 14bf01...b7c3ef )
by FX
04:41
created

FileUploaderService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getFullPath() 0 5 1
A remove() 0 4 1
A __construct() 0 3 1
A upload() 0 7 1
A getTargetDir() 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 AppBundle\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
     * @return string
78
     */
79
    public function remove(string $fileName): string
80
    {
81
        $fileNameWithoutPath = basename($fileName);
82
        unlink($this->getTargetDir().'/'.$fileNameWithoutPath);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
83
    }
84
85
    /**
86
     * Get full path of the file.
87
     *
88
     * @param string $fileName File name
89
     *
90
     * @return string
91
     */
92
    public function getFullPath(string $fileName): string
93
    {
94
        $fileNameWithoutPath = basename($fileName);
95
96
        return $this->targetDir.'/'.$fileNameWithoutPath;
97
    }
98
99
    /**
100
     * Get temporary directory name.
101
     *
102
     * @return string
103
     */
104
    public function getTargetDir(): string
105
    {
106
        return $this->targetDir;
107
    }
108
}
109