Passed
Push — master ( fabd45...a8694e )
by Ron
03:26 queued 11s
created

CustomerFilesController::store()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 74
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 6.0493

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 38
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 74
ccs 32
cts 36
cp 0.8889
crap 6.0493
rs 8.6897

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers\Customers;
4
5
use App\Files;
6
use App\Customers;
7
use App\CustomerFiles;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Log;
10
use App\Http\Controllers\Controller;
11
use Illuminate\Support\Facades\Auth;
12
use Illuminate\Support\Facades\Route;
13
use Pion\Laravel\ChunkUpload\Receiver\FileReceiver;
14
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory;
15
use Pion\Laravel\ChunkUpload\Exceptions\UploadMissingFileException;
16
17
class CustomerFilesController extends Controller
18
{
19 12
    public function __construct()
20
    {
21 12
        $this->middleware('auth');
22 12
    }
23
24
    //  Store a new customer file
25 2
    public function store(Request $request)
26
    {
27
        //  Validate the form
28 2
        $request->validate([
29 2
            'cust_id' => 'required',
30
            'name'    => 'required',
31
            'type'    => 'required'
32
        ]);
33
34 2
        $receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request));
35
36
        //  Verify that the upload is valid and being processed
37 2
        if($receiver->isUploaded() === false)
38
        {
39
            Log::error('Upload File Missing - ' .
40
            /** @scrutinizer ignore-type */
41
            $request->toArray());
42
            throw new UploadMissingFileException();
43
        }
44
45
        //  Receive and process the file
46 2
        $save = $receiver->receive();
47
48
        //  See if the upload has finished
49 2
        if($save->isFinished())
50
        {
51
            //  Determine if the note should go to the customer, or its parent
52 2
            $details = Customers::find($request->cust_id);
53 2
            if ($details->parent_id && $request->shared)
54
            {
55
                $request->cust_id = $details->parent_id;
56
            }
57
58 2
            $file = $save->getFile();
59
            //  Set file locationi and clean filename for duplicates
60 2
            $filePath = config('filesystems.paths.customers').DIRECTORY_SEPARATOR.$request->cust_id;
61 2
            $fileName = Files::cleanFileName($filePath, $file->getClientOriginalName());
62
63
            //  Store the file
64 2
            $file->storeAs($filePath, $fileName);
65
66
            //  Put the file in the database
67 2
            $file = Files::create(
68
            [
69 2
                'file_name' => $fileName,
70 2
                'file_link' => $filePath.DIRECTORY_SEPARATOR
71
            ]);
72
73
            //  Get information for system files table
74 2
            $fileID = $file->file_id;
75
76
            //  Input the file into the customer files table
77 2
            CustomerFiles::create([
78 2
                'file_id'      => $fileID,
79 2
                'file_type_id' => $request->type,
80 2
                'cust_id'      => $request->cust_id,
81 2
                'user_id'      => Auth::user()->user_id,
82 2
                'shared'       => $request->shared ? 1 : 0,
83 2
                'name'         => $request->name
84
            ]);
85
86 2
            Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
87 2
            Log::debug('Submitted Data - ', $request->toArray());
88 2
            Log::info('File added for Customer ID-'.$request->custID.' by User ID-'.Auth::user()->user_id.'.  New File ID-'.$fileID);
89
        }
90
91
        //  Get the current progress
92 2
        $handler = $save->handler();
93
94 2
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
95 2
        Log::debug('File being uploaded.  Percentage done - '.$handler->getPercentageDone());
96 2
        return response()->json([
97 2
            'done'   => $handler->getPercentageDone(),
98
            'status' => true
99
        ]);
100
    }
101
102
    //  Get the files for the customer
103 2
    public function show($id)
104
    {
105 2
        $files = CustomerFiles::where('cust_id', $id)
106 2
            ->with('Files')
107 2
            ->with('CustomerFileTypes')
108 2
            ->with('User')
109 2
            ->get();
110
111
        //  Determine if there is a parent site with shared files
112 2
        $parent = Customers::find($id)->parent_id;
113 2
        if ($parent) {
114
            $parentList = Customerfiles::where('cust_id', $parent)
115
                ->with('Files')
116
                ->with('CustomerFileTypes')
117
                ->with('User')
118
                ->get();
119
120
            $files = $files->merge($parentList);
121
        }
122
123 2
        return $files;
124
    }
125
126
    //  Update the test information of the file, but not the file itself
127
    public function update(Request $request, $id)
128
    {
129
        $request->validate([
130
            'cust_id' => 'required',
131
            'name' => 'required',
132
            'type' => 'required'
133
        ]);
134
135
        CustomerFiles::find($id)->update([
136
            'name' => $request->name,
137
            'file_type_id' => $request->type
138
        ]);
139
140
        return response()->json(['success' => true]);
141
    }
142
143
    //  Remove a customer file
144 2
    public function destroy($id)
145
    {
146
        //  Remove the file from SystemFiles table
147 2
        $data = CustomerFiles::find($id);
148 2
        $fileID = $data->file_id;
149 2
        $data->delete();
150
151 2
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
152 2
        Log::info('File Deleted For Customer ID-'.$data->custID.' by User ID-'.Auth::user()->user_id.'.  File ID-'.$id);
153
154
        //  Delete from system if no longer in use
155 2
        Files::deleteFile($fileID);
156
157 2
        return response()->json(['success' => true]);
158
    }
159
}
160