DocumentStorageService::getFullPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright (c) 2018 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 App\Entity\Campaign;
26
use App\Entity\Project;
27
use App\Entity\ZipFile;
28
use Symfony\Component\HttpFoundation\File\UploadedFile;
29
30
/**
31
 * File uploader service class. Inspired from Symfony example:
32
 * https://symfony.com/doc/current/controller/upload_file.html.
33
 *
34
 * @category  ci-report app
35
 *
36
 * @author    Francois-Xavier Soubirou <[email protected]>
37
 * @copyright 2018 Francois-Xavier Soubirou
38
 * @license   http://www.gnu.org/licenses/   GPLv3
39
 *
40
 * @see      https://www.ci-report.io
41
 */
42
class DocumentStorageService
43
{
44
    /**
45
     * @var string
46
     */
47
    private $storageDir;
48
49
    /**
50
     * Constructor.
51
     *
52
     * @param string $storageDir Directory to save temporary files
53
     */
54
    public function __construct(string $storageDir)
55
    {
56
        $this->storageDir = $storageDir;
57
    }
58
59
    /**
60
     * Attach zip to a suite.
61
     *
62
     * @param Project      $project  Project
63
     * @param Campaign     $campaign Campaign
64
     * @param UploadedFile $file     File to move
65
     *
66
     * @return ZipFile
67
     */
68
    public function storeZip(Project $project, Campaign $campaign, UploadedFile $file): ZipFile
69
    {
70
        $fileName = sha1(uniqid());
71
72
        $targetDir = $this->getTargetDir($project, $campaign, true);
73
        $file->move($targetDir, $fileName);
74
75
        return new ZipFile($targetDir, $fileName);
76
    }
77
78
    /**
79
     * Remove zip document.
80
     *
81
     * @param Project  $project  Project
82
     * @param Campaign $campaign Campaign
83
     * @param string   $fileName File to remove
84
     */
85
    public function remove(Project $project, Campaign $campaign, string $fileName)
86
    {
87
        $path = $this->getFullPath($project, $campaign, $fileName);
88
89
        if (file_exists($path)) {
90
            unlink($path);
91
        }
92
    }
93
94
    /**
95
     * Get full path of the file.
96
     *
97
     * @param Project  $project  Project
98
     * @param Campaign $campaign Campaign
99
     * @param string   $fileName File name
100
     *
101
     * @return string
102
     */
103
    public function getFullPath(Project $project, Campaign $campaign, string $fileName): string
104
    {
105
        $fileNameWithoutPath = basename($fileName);
106
107
        return $this->getTargetDir($project, $campaign).'/'.$fileNameWithoutPath;
108
    }
109
110
    /**
111
     * Get document directory.
112
     *
113
     * @param Project  $project  Project
114
     * @param Campaign $campaign Campaign
115
     * @param bool     $create   If true create directories if don't exist
116
     *
117
     * @return string
118
     */
119
    public function getTargetDir(Project $project, Campaign $campaign, bool $create = false): string
120
    {
121
        $targetDir = $this->storageDir.'/'.$project->getId().'/'.$campaign->getId();
122
123
        if ($create && !file_exists($targetDir)) {
124
            mkdir($targetDir, 0755, true);
125
        }
126
127
        return $targetDir;
128
    }
129
}
130