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

CustomerFilesController::store()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 65
ccs 0
cts 30
cp 0
rs 8.7636
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\Customers;
4
5
use App\Files;
6
use App\Customers;
7
use App\CustomerFiles;
8
use App\CustomerFileTypes;
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 CustomerFilesController extends Controller
21
{
22
    public function __construct()
23
    {
24
        $this->middleware('auth');
25
    }
26
    
27
    //  Get the types of files that can be attached to a customer
28
    public function getFileTypes()
29
    {
30
        $fileTypes = CustomerFileTypes::all();
31
        $fTypes    = [];
32
        foreach($fileTypes as $type)
33
        {
34
            $fTypes[$type->file_type_id] = $type->description;
35
        }
36
        
37
        return response()->json($fileTypes);
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...
38
    }
39
40
    //  Store a new customer file
41
    public function store(Request $request)
42
    {
43
        //  Validate the form
44
        $request->validate([
45
            'custID' => 'required',
46
            'name'   => 'required',
47
            'type'   => 'required'
48
        ]);
49
        
50
        $receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request));
51
        
52
        //  Verify that the upload is valid and being processed
53
        if($receiver->isUploaded() === false)
54
        {
55
            Log::error('Upload File Missing - '.$request->toArray());
56
            throw new UploadMissingFileException();
57
        }
58
        
59
        //  Receive and process the file
60
        $save = $receiver->receive();
61
        
62
        //  See if the upload has finished
63
        if($save->isFinished())
64
        {
65
            //  Set file locationi and clean filename for duplicates
66
            $filePath = config('filesystems.paths.customers').DIRECTORY_SEPARATOR.$request->custID;
67
            $fileName = Files::cleanFileName($filePath, $request->file->getClientOriginalName());
68
69
            //  Store the file
70
            $request->file->storeAs($filePath, $fileName);
71
72
            //  Put the file in the database
73
            $file = Files::create(
74
            [
75
                'file_name' => $fileName,
76
                'file_link' => $filePath.DIRECTORY_SEPARATOR
77
            ]);
78
79
            //  Get information for system files table
80
            $fileID = $file->file_id;
81
82
            //  Input the file into the customer files table
83
            CustomerFiles::create([
84
                'file_id'      => $fileID,
85
                'file_type_id' => $request->type,
86
                'cust_id'      => $request->custID,
87
                '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...
88
                'name'         => $request->name
89
            ]);
90
            
91
            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...
92
            Log::debug('Submitted Data - ', $request->toArray());
93
            Log::info('File added for Customer ID-'.$request->custID.' by User ID-'.Auth::user()->user_id.'.  New File ID-'.$fileID);
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...
94
        }
95
        
96
        //  Get the current progress
97
        $handler = $save->handler();
98
99
        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...
100
        Log::debug('File being uploaded.  Percentage done - '.$handler->getPercentageDone());
101
        return response()->json([
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...
102
            'done'   => $handler->getPercentageDone(),
103
            'status' => true
104
        ]);
105
    }
106
107
    //  Get the files for the customer
108
    public function show($id)
109
    {
110
        $files = CustomerFiles::where('cust_id', $id)
111
            ->select('name', 'customer_file_types.description as type', 
112
            'first_name', 'last_name', 'customer_files.updated_at as added_on',
113
            'files.file_id', 'files.file_name', 'cust_file_id')
114
            ->LeftJoin('customer_file_types', 'customer_files.file_type_id', '=', 'customer_file_types.file_type_id')
115
            ->LeftJoin('users', 'customer_files.user_id', '=', 'users.user_id')
116
            ->Join('files', 'customer_files.file_id', '=', 'files.file_id')
117
            ->orderBy('customer_files.file_type_id', 'asc')
118
            ->get();
119
        
120
        return response()->json($files);
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...
121
    }
122
123
    //  Remove a customer file
124 View Code Duplication
    public function destroy($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
    {
126
        //  Remove the file from SystemFiles table
127
        $data = CustomerFiles::find($id);
128
        $fileID = $data->file_id;
129
        $data->delete();
130
        
131
        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...
132
        Log::info('File Deleted For Customer ID-'.$data->custID.' by User ID-'.Auth::user()->user_id.'.  File ID-'.$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...
133
        
134
        //  Delete from system if no longer in use
135
        Files::deleteFile($fileID);
136
        
137
        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...
138
    }
139
}
140