HasFileTrait   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 70.37%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 27
c 3
b 1
f 0
dl 0
loc 110
ccs 19
cts 27
cp 0.7037
rs 10
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setFile() 0 13 4
A setFileFromUploadedFile() 0 7 1
A sanitizeFileName() 0 3 1
A getFile() 0 3 1
A getPathToFile() 0 3 1
A setFileName() 0 5 1
A setPathToFile() 0 3 1
A setFileFromSymfonyFile() 0 8 1
1
<?php
2
3
namespace ByTIC\MediaLibrary\FileAdder\Traits;
4
5
use Nip\Logger\Exception;
6
use Symfony\Component\HttpFoundation\File\File as SymfonyFile;
7
use Symfony\Component\HttpFoundation\File\UploadedFile;
8
9
/**
10
 * Trait HasFileTrait
11
 * @package ByTIC\MediaLibrary\FileAdder\Traits
12
 */
13
trait HasFileTrait
14
{
15
    /** @var string|\Symfony\Component\HttpFoundation\File\File */
16
    protected $file;
17
18
    /** @var string */
19
    protected $fileName;
20
21
    /** @var string */
22
    protected $pathToFile;
23
24 7
    /**
25
     * @return string|SymfonyFile
26 7
     */
27
    public function getFile()
28
    {
29
        return $this->file;
30
    }
31
32 3
    /**
33
     * @return string
34 3
     */
35
    public function getPathToFile(): string
36
    {
37
        return $this->pathToFile;
38
    }
39
40 6
    /**
41
     * @param string $pathToFile
42 6
     */
43 6
    public function setPathToFile(string $pathToFile)
44
    {
45
        $this->pathToFile = $pathToFile;
46
    }
47
48
    /**
49
     * @param $file
50
     *
51
     * @throws Exception
52 6
     *
53
     * @return $this
54 6
     */
55 6
    public function setFile($file)
56
    {
57 6
        if (is_string($file)) {
58
            $file = new SymfonyFile($file, true);
59
        }
60 6
        if ($file instanceof UploadedFile) {
61 6
            return $this->setFileFromUploadedFile($file);
62
        }
63
        if ($file instanceof SymfonyFile) {
64
            return $this->setFileFromSymfonyFile($file);
65
        }
66
67
        throw new Exception('Invalid File');
68
    }
69
70
    /**
71
     * @param SymfonyFile $file
72
     *
73
     * @throws Exception
74 6
     *
75
     * @return $this
76 6
     */
77 6
    public function setFileFromSymfonyFile($file)
78 6
    {
79 6
        $this->file = $file;
80
        $this->setPathToFile($file->getPath() . '/' . $file->getFilename());
81 6
        $this->setFileName(pathinfo($file->getFilename(), PATHINFO_BASENAME));
0 ignored issues
show
Bug introduced by
It seems like pathinfo($file->getFilen...aits\PATHINFO_BASENAME) can also be of type array; however, parameter $fileName of ByTIC\MediaLibrary\FileA...ileTrait::setFileName() 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

81
        $this->setFileName(/** @scrutinizer ignore-type */ pathinfo($file->getFilename(), PATHINFO_BASENAME));
Loading history...
82
        $this->mediaName = pathinfo($file->getFilename(), PATHINFO_FILENAME);
0 ignored issues
show
Bug Best Practice introduced by
The property mediaName does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
83
84
        return $this;
85
    }
86
87
    /**
88
     * @param UploadedFile $file
89
     *
90
     * @return $this
91
     */
92
    public function setFileFromUploadedFile($file)
93
    {
94
        $this->file = $file;
95
        $this->setPathToFile($file->getPath() . '/' . $file->getFilename());
96
        $this->setFileName($file->getClientOriginalName());
97
        $this->mediaName = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
0 ignored issues
show
Bug Best Practice introduced by
The property mediaName does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
98
        return $this;
99
    }
100
101
    /**
102
     * Set the name of the file that is stored on disk.
103
     *
104
     * @param string $fileName
105
     *
106
     * @return $this
107
     */
108
    public function setFileName(string $fileName)
109
    {
110
        $this->fileName = $this->sanitizeFileName($fileName);
111
112
        return $this;
113
    }
114
115
    /**
116
     * @param $fileName
117
     *
118
     * @return string
119
     */
120
    protected function sanitizeFileName(string $fileName): string
121
    {
122
        return str_replace(['#', '/', '\\'], '-', $fileName);
123
    }
124
}
125