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

Files   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 42.86%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 3
dl 0
loc 76
ccs 12
cts 28
cp 0.4286
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A systemFiles() 0 4 1
A fileLinkFiles() 0 4 1
A cleanFileName() 0 34 5
A deleteFile() 0 23 2
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