Passed
Push — master ( 054b32...1c77af )
by Ron
07:39 queued 12s
created

CustomerFilesController::store()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 75
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 6.0454

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 39
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 75
ccs 33
cts 37
cp 0.8919
crap 6.0454
rs 8.6737

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