Test Failed
Push — dev5 ( 7acea7...d3ea14 )
by Ron
07:30
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.0226

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
rs 8.6897
ccs 32
cts 35
cp 0.9143
crap 6.0226

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 12
{
19
    public function __construct()
20 12
    {
21 12
        $this->middleware('auth');
22
    }
23
24 2
    //  Store a new customer file
25
    public function store(Request $request)
26
    {
27 2
        //  Validate the form
28 2
        $request->validate([
29
            'cust_id' => 'required',
30
            'name'    => 'required',
31
            'type'    => 'required'
32
        ]);
33 2
34
        $receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request));
35
36 2
        //  Verify that the upload is valid and being processed
37
        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 2
        //  Receive and process the file
46
        $save = $receiver->receive();
47
48 2
        //  See if the upload has finished
49
        if($save->isFinished())
50 2
        {
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;
1 ignored issue
show
Bug introduced by
The property cust_id does not seem to exist on Illuminate\Http\Request.
Loading history...
56 2
            }
57
58
            $file = $save->getFile();
59 2
            //  Set file locationi and clean filename for duplicates
60
            $filePath = config('filesystems.paths.customers').DIRECTORY_SEPARATOR.$request->cust_id;
61 2
            $fileName = Files::cleanFileName($filePath, $file->getClientOriginalName());
62 2
63
            //  Store the file
64
            $file->storeAs($filePath, $fileName);
65
66 2
            //  Put the file in the database
67
            $file = Files::create(
68
            [
69 2
                'file_name' => $fileName,
70 2
                'file_link' => $filePath.DIRECTORY_SEPARATOR
71 2
            ]);
72 2
73 2
            //  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
                'cust_id'      => $request->cust_id,
81
                'user_id'      => Auth::user()->user_id,
82
                'shared'       => $request->shared ? 1 : 0,
83 2
                'name'         => $request->name
84
            ]);
85 2
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
        $handler = $save->handler();
93
94 2
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
95
        Log::debug('File being uploaded.  Percentage done - '.$handler->getPercentageDone());
96 2
        return response()->json([
97 2
            'done'   => $handler->getPercentageDone(),
98 2
            'status' => true
99 2
        ]);
100 2
    }
101
102 2
    //  Get the files for the customer
103
    public function show($id)
104
    {
105
        $files = CustomerFiles::where('cust_id', $id)
106
            ->with('Files')
107
            ->with('CustomerFileTypes')
108
            ->with('User')
109
            ->get();
110
111
        //  Determine if there is a parent site with shared files
112
        $parent = Customers::find($id)->parent_id;
113
        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 2
    //  Update the test information of the file, but not the file itself
127 2
    public function update(Request $request, $id)
128 2
    {
129
        $request->validate([
130 2
            'cust_id' => 'required',
131 2
            'name' => 'required',
132
            'type' => 'required'
133
        ]);
134 2
135
        CustomerFiles::find($id)->update([
136 2
            '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
    public function destroy($id)
145
    {
146
        //  Remove the file from SystemFiles table
147
        $data = CustomerFiles::find($id);
148
        $fileID = $data->file_id;
149
        $data->delete();
150
151
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
152
        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
        Files::deleteFile($fileID);
156
157
        return response()->json(['success' => true]);
158
    }
159
}
160