Passed
Push — dev5 ( b08d9a...c43caf )
by Ron
04:08
created

SystemFilesController::replace()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 60
ccs 0
cts 27
cp 0
rs 8.8727
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12

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\Systems;
4
5
use App\Files;
6
use App\SystemTypes;
7
use App\SystemFiles;
8
use App\SystemFileTypes;
9
use Illuminate\Http\Request;
10
use Illuminate\Http\UploadedFile;
11
use Illuminate\Support\Facades\Log;
12
use App\Http\Controllers\Controller;
13
use Illuminate\Support\Facades\Auth;
14
use Illuminate\Support\Facades\Route;
15
use Pion\Laravel\ChunkUpload\Receiver\FileReceiver;
16
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory;
17
use Pion\Laravel\ChunkUpload\Handler\AbstractHandler;
18
use Pion\Laravel\ChunkUpload\Exceptions\UploadMissingFileException;
19
20
class SystemFilesController extends Controller
21
{
22
    public function __construct()
23
    {
24
        $this->middleware('auth');
25
    }
26
27
    //  Store a new system file
28
    public function store(Request $request)
29
    {
30
        //  Validate the form
31
        $request->validate([
32
            'name'  => 'required',
33
            'sysID' => 'required',
34
            'type'  => 'required',
35
            'file'  => 'required'
36
        ]);
37
        
38
        $receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request));
39
        
40
        //  Verify that the upload is valid and being processed
41
        if($receiver->isUploaded() === false)
42
        {
43
            Log::error('Upload File Missing - '.$request->toArray());
44
            throw new UploadMissingFileException();
45
        }
46
        
47
            //  Recieve and process the file
48
        $save = $receiver->receive();
49
50
        //  See if the uploade has finished
51
        if($save->isFinished())
52
        {
53
            
54
            //  Get the system information
55
            $sys = SystemTypes::where('sys_id', $request->sysID)
56
                ->join('system_categories', 'system_types.cat_id', '=', 'system_categories.cat_id')
57
                ->select('system_types.*', 'system_categories.name AS cat_name')
58
                ->first();
59
60
            //  Set the file location and clean the file name for storage
61
            $filePath = config('filesystems.paths.systems')
62
                .DIRECTORY_SEPARATOR.strtolower($sys->cat_name)
63
                .DIRECTORY_SEPARATOR.$sys->folder_location;
64
65
            $fileName = Files::cleanFileName($filePath, $request->file->getClientOriginalName());
66
67
            //  Store the file and place it in the database
68
            $request->file->storeAs($filePath, $fileName);
69
            $file = Files::create([
70
                'file_name' => $fileName,
71
                'file_link' => $filePath.DIRECTORY_SEPARATOR
72
            ]);
73
74
            //  Get the information for the system files table
75
            $fileID = $file->file_id;
76
            $typeID = SystemFileTypes::where('description', $request->type)->first()->type_id;
77
78
            //  Attach file to system type
79
            SystemFiles::create([
80
                'sys_id'      => $request->sysID,
81
                'type_id'     => $typeID,
82
                'file_id'     => $fileID,
83
                'name'        => $request->name,
84
                'description' => $request->description,
85
                'user_id'     => Auth::user()->user_id
0 ignored issues
show
Bug introduced by
Accessing user_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
86
            ]);
87
88
            Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
0 ignored issues
show
Bug introduced by
Accessing user_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
89
            Log::debug('Submitted Data - ', $request->toArray());
90
            Log::info('File ID-'.$fileID.' Added For System ID-'.$request->sysID.' By User ID-'.Auth::user()->user_id);
0 ignored issues
show
Bug introduced by
Accessing user_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
91
92
            return response()->json(['success' => true]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
93
        }
94
95
        //  Get the current progress
96
        $handler = $save->handler();
97
98
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
0 ignored issues
show
Bug introduced by
Accessing user_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
99
        Log::debug('File being uploaded.  Percentage done - '.$handler->getPercentageDone());
100
        return response()->json([
101
            'done'   => $handler->getPercentageDone(),
102
            'status' => true
103
        ]);
104
    }
105
106
    //  Return JSON array of the system files
107
    public function show($id)
108
    {
109
        $fileTypes = SystemFileTypes::all();        
110
        $files = SystemFiles::where('sys_id', $id)
111
            ->select('system_files.sys_id', 'system_files.type_id', 'system_files.name', 'system_files.description', 'users.first_name', 'users.last_name', 'system_files.created_at')
112
            ->join('files', 'system_files.file_id', '=', 'files.file_id')
113
            ->join('users', 'system_files.user_id', '=', 'users.user_id')
114
            ->get();
115
        
116
        $i = 0;
117
        $fileArr = [];
118
        foreach($fileTypes as $type)
119
        {
120
            $fileArr[$i] = [
121
                'description' => $type->description,
122
                'data'        => false
123
            ];
124
                
125
            foreach($files as $file)
126
            {
127
                if($file->type_id == $type->type_id)
128
                {
129
                    $fileArr[$i]['data'][] = [
130
                        'id'          => $file->sys_file_id,
131
                        'url'         => route('download', [$file->file_id, $file->file_name]),
132
                        'name'        => $file->name,
133
                        'description' => $file->description,
134
                        'user'        => $file->first_name.' '.$file->last_name,
135
                        'added'       => date('M d, Y', strtotime($file->created_at))
136
                    ];
137
                }
138
            }
139
            $i++;
140
        }
141
142
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
0 ignored issues
show
Bug introduced by
Accessing user_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
143
        Log::debug('Fetched Data - ', $fileArr);
144
        return response()->json($fileArr);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
145
    }
146
147
    //  Update the file information
148
    public function update(Request $request, $id)
149
    {
150
        SystemFiles::find($id)->update([
151
            'name'        => $request->name,
152
            'description' => $request->desc
153
        ]);
154
        
155
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
0 ignored issues
show
Bug introduced by
Accessing user_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
156
        Log::debug('Submitted Data - ', $request->toArray());
157
        Log::info('File Information on System File ID-'.$id.' Updated by User ID-'.Auth::user()->user_id);
0 ignored issues
show
Bug introduced by
Accessing user_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
158
        
159
        return response()->json(['success' => true]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
160
    }
161
    
162
    //  Replace the file with a new one
163
    public function replace(Request $request)
164
    {
165
        //  Validate the form
166
        $request->validate(['file' => 'required']);
167
        
168
        $receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request));
169
            
170
        //  Verify that the upload is valid and being processed
171
        if($receiver->isUploaded() === false)
172
        {
173
            Log::error('Upload File Missing - '.$request->toArray());
174
            throw new UploadMissingFileException();
175
        }
176
177
        //  Recieve and process the file
178
        $save = $receiver->receive();
179
180
        //  See if the uploade has finished
181
        if($save->isFinished())
182
        {
183
            //  Get the original file information
184
            $origData = SystemFiles::find($request->fileID)->with('files')->first();
185
            $filePath = $origData->files->file_link;
186
            //  Clean the filename
187
            $fileName = Files::cleanFileName($filePath, $request->file->getClientOriginalName());
188
189
            //  Store the file
190
            $request->file->storeAs($filePath, $fileName);
191
192
            //  Put the file in the database
193
            $file = Files::create(
194
            [
195
                'file_name' => $fileName,
196
                'file_link' => $filePath.DIRECTORY_SEPARATOR
197
            ]);
198
199
            //  Get information for system files table
200
            $fileID = $file->file_id;
201
202
            SystemFiles::find($request->fileID)->update([
203
                'file_id' => $fileID
204
            ]);   
205
206
            Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
0 ignored issues
show
Bug introduced by
Accessing user_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
207
            Log::debug('Submitted Data - ', $request->toArray());
208
            Log::info('System File ID-'.$fileID.' Replaced by User ID-'.Auth::user()->user_id);
0 ignored issues
show
Bug introduced by
Accessing user_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
209
210
            return response()->json(['success' => true]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
211
        }
212
213
        //  Get the current progress
214
        $handler = $save->handler();
215
216
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
0 ignored issues
show
Bug introduced by
Accessing user_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
217
        Log::debug('File being uploaded.  Percentage done - '.$handler->getPercentageDone());
218
        return response()->json([
219
            'done'   => $handler->getPercentageDone(),
220
            'status' => true
221
        ]);
222
    }
223
224
    //  Delete the system file
225
    public function destroy($id)
226
    {
227
        //  Remove the file from SystemFiles table
228
        $data = SystemFiles::find($id);
229
        $fileID = $data->file_id;
230
        
231
        Log::info('System File ID-'.$fileID.' Deleted For System ID-'.$data->sys_id.' by User ID-'.Auth::user()->user_id);
0 ignored issues
show
Bug introduced by
Accessing user_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
232
        
233
        $data->delete();
234
        
235
        //  Delete from system if no longer in use
236
        Files::deleteFile($fileID);
237
        
238
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
0 ignored issues
show
Bug introduced by
Accessing user_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
239
        Log::debug('File Data - ', $data->toArray());
240
        Log::notice('File ID-'.$data->file_id.' deleted by user ID-'.Auth::user()->user_id);
0 ignored issues
show
Bug introduced by
Accessing user_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
241
        return response()->json(['success' => true]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
242
    }
243
}
244