| Total Complexity | 4 |
| Total Lines | 45 |
| Duplicated Lines | 0 % |
| Coverage | 50% |
| Changes | 0 | ||
| 1 | <?php |
||
| 10 | class Helper |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Generate a unique name in a specific dir |
||
| 14 | * |
||
| 15 | * @param string $dir the especific dir where the file will be saved |
||
| 16 | * @param bool $withPath |
||
| 17 | * |
||
| 18 | * @return string |
||
| 19 | */ |
||
| 20 | 1 | public static function generateUniqueName(File $file, string $dir, $withPath = false) : string |
|
| 21 | { |
||
| 22 | // the provided path has to be a dir |
||
| 23 | 1 | if (!is_dir($dir)) { |
|
| 24 | throw new Exception("The dir provided: '{$dir}' isn't a valid one."); |
||
| 25 | } |
||
| 26 | |||
| 27 | 1 | $path = tempnam($dir . '/', ''); |
|
| 28 | |||
| 29 | //this function creates a file (like touch) so, we have to delete it. |
||
| 30 | 1 | unlink($path); |
|
| 31 | 1 | $uniqueName = $path; |
|
| 32 | 1 | if (!$withPath) { |
|
| 33 | 1 | $uniqueName = str_replace($dir, '', $path); |
|
| 34 | } |
||
| 35 | |||
| 36 | 1 | return $uniqueName . '.' . strtolower($file->getExtension()); |
|
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Create a File instance from a given path |
||
| 41 | * |
||
| 42 | * @param string $path Path of the file to be used |
||
| 43 | * |
||
| 44 | * @return File |
||
| 45 | */ |
||
| 46 | public static function pathToFile(string $path) : File |
||
| 55 | ]); |
||
| 56 | } |
||
| 58 |