Passed
Push — dev5 ( 0d97ef...978ab8 )
by Ron
08:29
created

CustomerFilesController::store()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 66
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 35
nc 3
nop 1
dl 0
loc 66
ccs 33
cts 33
cp 1
crap 5
rs 9.0488
c 1
b 0
f 0

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 22
    public function __construct()
20
    {
21 22
        $this->middleware('auth');
22 22
    }
23
24
    //  Store a new customer file
25 4
    public function store(Request $request)
26
    {
27 4
        Log::debug('Uploaded Data - ', $request->toArray());
28
        //  Validate the form
29 4
        $request->validate([
30 4
            'cust_id' => 'required',
31
            'name'    => 'required',
32
            'type'    => 'required'
33
        ]);
34
35 4
        $receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request));
36
37
        //  Receive and process the file
38 4
        $save = $receiver->receive();
39
40
        //  See if the upload has finished
41 4
        if($save->isFinished())
42
        {
43
            //  Determine if the note should go to the customer, or its parent
44 4
            $details = Customers::find($request->cust_id);
45 4
            if ($details->parent_id && $request->shared == 'true')
46
            {
47 2
                $request->cust_id = $details->parent_id;
48
            }
49
50 4
            $file = $save->getFile();
51
            //  Set file locationi and clean filename for duplicates
52 4
            $filePath = config('filesystems.paths.customers').DIRECTORY_SEPARATOR.$request->cust_id;
53 4
            $fileName = Files::cleanFileName($filePath, $file->getClientOriginalName());
54
55
            //  Store the file
56 4
            $file->storeAs($filePath, $fileName);
57
58
            //  Put the file in the database
59 4
            $file = Files::create(
60
            [
61 4
                'file_name' => $fileName,
62 4
                'file_link' => $filePath.DIRECTORY_SEPARATOR
63
            ]);
64
65
            //  Get information for system files table
66 4
            $fileID = $file->file_id;
67
68
            //  Input the file into the customer files table
69 4
            CustomerFiles::create([
70 4
                'file_id'      => $fileID,
71 4
                'file_type_id' => $request->type,
72 4
                'cust_id'      => $request->cust_id,
73 4
                'user_id'      => Auth::user()->user_id,
74 4
                'shared'       => $request->shared === 'true' ? 1 : 0,
75 4
                'name'         => $request->name
76
            ]);
77
78 4
            Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
79 4
            Log::debug('Submitted Data - ', $request->toArray());
80 4
            Log::info('File added for Customer ID-'.$request->custID.' by User ID-'.Auth::user()->user_id.'.  New File ID-'.$fileID);
81
        }
82
83
        //  Get the current progress
84 4
        $handler = $save->handler();
85
86 4
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
87 4
        Log::debug('File being uploaded.  Percentage done - '.$handler->getPercentageDone());
88 4
        return response()->json([
89 4
            'done'   => $handler->getPercentageDone(),
90
            'status' => true
91
        ]);
92
    }
93
94
    //  Get the files for the customer
95 4
    public function show($id)
96
    {
97 4
        $files = CustomerFiles::where('cust_id', $id)
98 4
            ->with('Files')
99 4
            ->with('CustomerFileTypes')
100 4
            ->with('User')
101 4
            ->get();
102
103
        //  Determine if there is a parent site with shared files
104 4
        $parent = Customers::find($id)->parent_id;
105 4
        if ($parent) {
106 2
            $parentList = Customerfiles::where('cust_id', $parent)
107 2
                ->where('shared', 1)
108 2
                ->with('Files')
109 2
                ->with('CustomerFileTypes')
110 2
                ->with('User')
111 2
                ->get();
112
113 2
            $files = $files->merge($parentList);
114
        }
115
116 4
        return $files;
117
    }
118
119
    //  Update the information of the file, but not the file itself
120 4
    public function update(Request $request, $id)
121
    {
122 4
        $request->validate([
123 4
            'cust_id' => 'required',
124
            'name' => 'required',
125
            'type' => 'required'
126
        ]);
127
128 4
        $details = Customers::find($request->cust_id);
129 4
        if ($details->parent_id && $request->shared == 1) {
130 2
            $request->cust_id = $details->parent_id;
131
        }
132
133 4
        CustomerFiles::find($id)->update([
134 4
            'name'         => $request->name,
135 4
            'file_type_id' => $request->type,
136 4
            'cust_id'      => $request->cust_id,
137 4
            'shared'       => $request->shared == 1 ? 1 : 0,
138
        ]);
139
140 4
        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