bytic /
MediaLibrary
| 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
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 |