AbstractStrategy   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 46.67%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
eloc 18
c 2
b 1
f 1
dl 0
loc 44
ccs 7
cts 15
cp 0.4667
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A makeFileName() 0 28 5
1
<?php
2
3
namespace ByTIC\MediaLibrary\Collections\UploadStrategy;
4
5
use ByTIC\MediaLibrary\FileAdder\FileAdder;
6
use Symfony\Component\HttpFoundation\File\File as SymfonyFile;
7
use Symfony\Component\HttpFoundation\File\UploadedFile;
8
9
/**
10
 * Class AbstractStrategy.
11
 */
12
abstract class AbstractStrategy implements AbstractStrategyInterface
13
{
14
    /**
15
     * @param FileAdder $fileAdder
16
     *
17
     * @return string
18
     */
19 2
    public static function makeFileName($fileAdder)
20
    {
21 2
        $fileName = $fileAdder->getFileName();
22
        if (!empty($fileName)) {
23 2
            return $fileName;
24
        }
25
        $file = $fileAdder->getFile();
26
27
        if ($file instanceof UploadedFile) {
28
            $path = $file->getRealPath();
29 2
            $extension = $file->getClientOriginalExtension();
30 2
31 2
            return static::transformFileName($path, $extension);
32
        }
33 2
        if ($file instanceof SymfonyFile) {
34
            $path = $file->getRealPath();
35
            $extension = $file->getExtension();
36
37
            return static::transformFileName($path, $extension);
38
        }
39
        if (is_string($file)) {
0 ignored issues
show
introduced by
The condition is_string($file) is always true.
Loading history...
40
            $path = $file;
41
            $extension = pathinfo($file, PATHINFO_EXTENSION);
42
43
            return static::transformFileName($path, $extension);
44
        }
45
46
        throw new \RuntimeException(__METHOD__ . ' needs a UploadedFile|SplFileInfo|string instance or a file path string');
47
    }
48
49
    /**
50
     * @param $path
51
     * @param $extension
52
     *
53
     * @return string
54
     */
55
    abstract public static function transformFileName($path, $extension);
56
}
57