Passed
Push — dev5 ( da16e0...6692a0 )
by Ron
08:20
created

FilesDomain::isFileDup()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 30
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 15.5468

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 30
ccs 3
cts 12
cp 0.25
crap 15.5468
rs 9.5222
1
<?php
2
3
namespace App\Domains;
4
5
use Illuminate\Support\Facades\Log;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Support\Facades\Storage;
8
9
use App\Files;
10
11
class FilesDomain
12
{
13
    protected $receiver, $path, $fileID;
14
15
    //  Constructor will set a default path location in the event one is not set through child class
16 28
    public function __construct()
17
    {
18 28
        $this->path = config('filesystems.paths.default');
19 28
    }
20
21
    //  Save a file after it has been uploaded
22 10
    protected function saveFile($file)
23
    {
24 10
        $fileName = $this->cleanFilename($file->getClientOriginalName());
25 10
        $fileName = $this->isFileDup($fileName);
26 10
        $file->storeAs($this->path, $fileName);
27
28
        //  Place file in Files table of DB
29 10
        $newFile = Files::create([
30 10
            'file_name' => $fileName,
31 10
            'file_link' => $this->path.DIRECTORY_SEPARATOR
32
        ]);
33
34 10
        return  $newFile->file_id;
35
    }
36
37
    //  Try to delete a file - note this will fail if it is still in use
38 2
    protected function deleteFile($fileID)
39
    {
40
        try
41
        {
42
            //  Try to delete file from database - will throw error if foreign key is in use
43 2
            $fileData = Files::find($fileID);
44 2
            $fileLink = $fileData->file_link.$fileData->file_name;
45 2
            $fileData->delete();
46
        }
47
        catch(\Illuminate\Database\QueryException $e)
48
        {
49
            //  Unable to remove file from the database
50
            Log::warning('Attempt to delete file failed.  Reason - '.$e.'. Additional Data - ', ['file_id' => $fileID, 'file_name' => $fileLink, 'user_id' => Auth::user()->user_id]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $fileLink does not seem to be defined for all execution paths leading up to this point.
Loading history...
51
            return false;
52
        }
53
54
        //  Delete the file from the storage system
55 2
        Storage::delete($fileLink);
56
57 2
        Log::notice('File deleted by '.Auth::user()->full_name.'. Additional Information - ', ['file_id' => $fileID, 'file_name' => $fileLink, 'user_id' => Auth::user()->user_id]);
58 2
        return true;
59
    }
60
61
    //  Clean a file name to remove any spaces
62 10
    protected function cleanFilename($name)
63
    {
64
        //  Remove all spaces
65 10
        $fileName = str_replace(' ', '_', $name);
66
67 10
        return $fileName;
68
    }
69
70
    //  Determine if the already exists and should be appended
71 10
    protected function isFileDup($fileName)
72
    {
73
        //  Determine if the filename already exists
74 10
        if (Storage::exists($this->path . DIRECTORY_SEPARATOR . $fileName))
75
        {
76
            $fileParts = pathinfo($fileName);
77
            $extension = isset($fileParts['extension']) ? ('.' . $fileParts['extension']) : '';
78
79
            //  Look to see if a number is already appended to a file.  (example - file(1).pdf)
80
            if (preg_match('/(.*?)(\d+)$/', $fileParts['filename'], $match))
81
            {
82
                // Has a number, increment it
83
                $base = $match[1];
84
                $number = intVal($match[2]);
85
            }
86
            else
87
            {
88
                // No number, add one
89
                $base = $fileParts['filename'];
90
                $number = 0;
91
            }
92
93
            //  Increase the number until one that is not in use is found
94
            do
95
            {
96
                $fileName = $base . '(' . ++$number . ')' . $extension;
97
            } while (Storage::exists($this->path.DIRECTORY_SEPARATOR.$fileName));
98
        }
99
100 10
        return $fileName;
101
    }
102
103
    //  When multiple files are uploaded at once, they are placed in a temporary location.  This moves them to the final folder
104 6
    protected function moveFile($newPath, $fileID)
105
    {
106 6
        $data = Files::find($fileID);
107
        //  Move the file to the proper folder
108
        try{
109
110 6
            Storage::move($data->file_link.$data->file_name, $newPath.DIRECTORY_SEPARATOR.$data->file_name);
111
        }
112 2
        catch(\Exception $e)
113
        {
114 2
            report($e);
115 2
            return false;
116
        }
117
118
        //  Update file link in DB
119 4
        $data->update([
120 4
            'file_link' => $newPath.DIRECTORY_SEPARATOR
121
        ]);
122
123 4
        return true;
124
    }
125
}
126