SEPAExportFileNamer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 30
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A name() 0 28 3
1
<?php
2
/*
3
 * Copyright (C) 2020  Jan Böhmer
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Affero General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Affero General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Affero General Public License
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace App\Services\Upload;
20
21
use App\Entity\PaymentOrder;
22
use App\Entity\SEPAExport;
23
use InvalidArgumentException;
24
use function pathinfo;
25
use function strtolower;
26
use Symfony\Component\HttpFoundation\File\UploadedFile;
27
use Symfony\Component\String\Slugger\AsciiSlugger;
28
use Vich\UploaderBundle\Mapping\PropertyMapping;
29
use Vich\UploaderBundle\Naming\NamerInterface;
30
31
/**
32
 * Use a custom namer to create file names which are useful when viewing the directory structure.
33
 * It contains the department, current date and the project name all in slugged form.
34
 */
35
class SEPAExportFileNamer implements NamerInterface
36
{
37
    public function name($object, PropertyMapping $mapping): string
38
    {
39
        if (!$object instanceof SEPAExport) {
40
            throw new InvalidArgumentException('$object must be an PaymentOrder!');
41
        }
42
43
        $file = $mapping->getFile($object);
44
        if ($file instanceof UploadedFile) {
45
            $originalName = $file->getClientOriginalName();
46
        } else {
47
            throw new InvalidArgumentException('$file must be an UploadedFile instance!');
48
        }
49
        $originalExtension = strtolower(pathinfo($originalName, PATHINFO_EXTENSION));
0 ignored issues
show
Bug introduced by
It seems like pathinfo($originalName, ...oad\PATHINFO_EXTENSION) can also be of type array; however, parameter $string of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
        $originalExtension = strtolower(/** @scrutinizer ignore-type */ pathinfo($originalName, PATHINFO_EXTENSION));
Loading history...
50
        $originalBasename = pathinfo($originalName, PATHINFO_FILENAME);
0 ignored issues
show
Unused Code introduced by
The assignment to $originalBasename is dead and can be removed.
Loading history...
51
52
        $slugger = new AsciiSlugger();
0 ignored issues
show
Unused Code introduced by
The assignment to $slugger is dead and can be removed.
Loading history...
53
54
        //TODO: Improve this
55
        $filename = sprintf("XML%06d", $object->getId());
56
57
        $filename .= '_'.date('ymd-His');
58
59
        $filename .= '_'.bin2hex(random_bytes(5));
60
61
        //Add original extension
62
        $filename .= '.'.$originalExtension;
63
64
        return $filename;
65
    }
66
}
67