Completed
Push — master ( 113c9d...6bb3ba )
by Lorenzo
02:38
created

FileHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 0
dl 0
loc 51
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkDirExistOrCreate() 0 9 4
A getFilenameWithoutExtension() 0 10 4
A unlinkSafe() 0 8 2
1
<?php
2
3
namespace Padosoft\Uploadable\Helpers;
4
5
/**
6
 * Helper Class FileHelper
7
 * @package Padosoft\Uploadable\Helpers
8
 */
9
class FileHelper
10
{
11
    /**
12
     * Check if passed path exists or try to create it.
13
     * Return false if it fails to create it.
14
     * @param string $filedPath
15
     * @param string $modeMask Ex.: '0755'
16
     * @return bool
17
     */
18
    public static function checkDirExistOrCreate(string $filedPath, string $modeMask) : bool
19
    {
20
        if (!$filedPath) {
21
            return false;
22
        }
23
24
        return file_exists($filedPath)
25
        || (mkdir($filedPath, $modeMask, true) && is_dir($filedPath));
26
    }
27
28
    /**
29
     * Return the file name of file (without path and witout extension).
30
     * Ex.: \public\upload\pippo.txt return 'pippo'
31
     * @param string $filePath
32
     * @return string
33
     */
34
    public function getFilenameWithoutExtension(string $filePath) : string
35
    {
36
        if(!$filePath){
37
            return '';
38
        }
39
40
        $info = pathinfo($filePath);
41
42
        return (is_array($info) && array_key_exists('filename', $info)) ? $info['filename'] : '';
43
    }
44
45
    /**
46
     * unlink file if exists.
47
     * Return false if exists and unlink fails.
48
     * @param string $filePath
49
     * @return bool
50
     */
51
    public function unlinkSafe(string $filePath) : bool
52
    {
53
        if (!file_exists($filePath)) {
54
            return false;
55
        }
56
57
        return unlink($filePath);
58
    }
59
}
60