Passed
Push — dev5 ( b08d9a...c43caf )
by Ron
04:08
created

Files::cleanFileName()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 13.2951

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 4
cts 13
cp 0.3076
rs 9.0648
c 0
b 0
f 0
cc 5
nc 5
nop 2
crap 13.2951
1
<?php
2
3
namespace App;
4
5
use Illuminate\Support\Facades\Log;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Facades\Storage;
9
10
class Files extends Model
11
{
12
    protected $primaryKey = 'file_id';
13
    protected $fillable = ['file_name', 'file_link', 'mime_type'];
14
15
    public function systemFiles()
16
    {
17
        return $this->hasMany('App\SystemFiles', 'file_id', 'file_id');
18
    }
19
20
    public function fileLinkFiles()
21
    {
22
        return $this->belongsTo('App\FileLinkFiles', 'file_id', 'file_id');
23
    }
24
25
    //  Remove any illegal characters from filename and make sure it is unique
26 10
    public static function cleanFileName($path, $fileName)
27
    {
28
        //  Remove all spaces
29 10
        $fileName = str_replace(' ', '_', $fileName);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $fileName. This often makes code more readable.
Loading history...
30
31
        //  Determine if the filename already exists
32 10
        if(Storage::exists($path.DIRECTORY_SEPARATOR.$fileName))
33
        {
34
            $fileParts = pathinfo($fileName);
35
            $extension = isset($fileParts['extension']) ? ('.'.$fileParts['extension']) : '';
36
37
            //  Look to see if a number is already appended to a file.  (example - file(1).pdf)
38
            if(preg_match('/(.*?)(\d+)$/', $fileParts['filename'], $match))
39
            {
40
                // Has a number, increment it
41
                $base = $match[1];
42
                $number = intVal($match[2]);
43
            }
44
            else
45
            {
46
                // No number, add one
47
                $base = $fileParts['filename'];
48
                $number = 0;
49
            }
50
51
            //  Increase the number until one that is not in use is found
52
            do
53
            {
54
                $fileName = $base.'('.++$number.')'.$extension;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $fileName. This often makes code more readable.
Loading history...
55
            } while(Storage::exists($path.DIRECTORY_SEPARATOR.$fileName));
56
        }
57
58 10
        return $fileName;
59
    }
60
61
    //  Delete a file from both the database and file system
62 4
    public static function deleteFile($fileID)
63
    {
64 4
        $fileLink = '';
65
        try
66
        {
67
            //  Try to delete file from database - will throw error if foreign key is in use
68 4
            $fileData = Files::find($fileID);
69 4
            $fileLink = $fileData->file_link.$fileData->file_name;
70 4
            $fileData->delete();
71
        }
72
        catch(\Illuminate\Database\QueryException $e)
73
        {
74
            //  Unable to remove file from the database
75
            Log::info('File Unable to delete - '.$e, ['file_id' => $fileID, 'file_name' => $fileLink, 'user_id' => Auth::user()->user_id]);
0 ignored issues
show
Bug introduced by
Accessing user_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
76
            return false;
77
        }
78
79
        //  Delete the file from the storage system
80 4
        Storage::delete($fileLink);
81
82 4
        Log::info('File Deleted', ['file_id' => $fileID, 'file_name' => $fileLink, 'user_id' => Auth::user()->user_id]);
0 ignored issues
show
Bug introduced by
Accessing user_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
83 4
        return true;
84
    }
85
}
86