Passed
Push — dev5 ( 1140b1...da16e0 )
by Ron
09:38
created

CustomerFilesController::store()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 65
ccs 30
cts 30
cp 1
rs 8.4525
c 0
b 0
f 0
cc 5
nc 3
nop 1
crap 5

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
16
class CustomerFilesController extends Controller
17
{
18 22
    public function __construct()
19
    {
20 22
        $this->middleware('auth');
21 22
    }
22
23
    //  Store a new customer file
24 4
    public function store(Request $request)
25
    {
26 4
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name.'. Submitted Data - ', $request->toArray());
0 ignored issues
show
Bug introduced by
Accessing full_name 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...
27
        //  Validate the form
28 4
        $request->validate([
0 ignored issues
show
Bug introduced by
The call to validate() misses a required argument $...$params.

This check looks for function calls that miss required arguments.

Loading history...
29 4
            'cust_id' => 'required',
30
            'name'    => 'required',
31
            'type'    => 'required'
32
        ]);
33
34 4
        $receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request));
35
36
        //  Receive and process the file
37 4
        $save = $receiver->receive();
38
39
        //  See if the upload has finished
40 4
        if($save->isFinished())
41
        {
42
            //  Determine if the note should go to the customer, or its parent
43 4
            $details = Customers::find($request->cust_id);
44 4
            if($details->parent_id && $request->shared == 'true')
45
            {
46 2
                $request->cust_id = $details->parent_id;
47
            }
48
49 4
            $file = $save->getFile();
50
            //  Set file locationi and clean filename for duplicates
51 4
            $filePath = config('filesystems.paths.customers').DIRECTORY_SEPARATOR.$request->cust_id;
52 4
            $fileName = Files::cleanFileName($filePath, $file->getClientOriginalName());
53
54
            //  Store the file
55 4
            $file->storeAs($filePath, $fileName);
56
57
            //  Put the file in the database
58 4
            $file = Files::create(
0 ignored issues
show
Bug introduced by
The method create() does not exist on App\Files. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
59
            [
60 4
                'file_name' => $fileName,
61 4
                'file_link' => $filePath.DIRECTORY_SEPARATOR
62
            ]);
63
64
            //  Get information for system files table
65 4
            $fileID = $file->file_id;
66
67
            //  Input the file into the customer files table
68 4
            CustomerFiles::create([
0 ignored issues
show
Bug introduced by
The method create() does not exist on App\CustomerFiles. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
69 4
                'file_id'      => $fileID,
70 4
                'file_type_id' => $request->type,
71 4
                'cust_id'      => $request->cust_id,
72 4
                '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...
73 4
                'shared'       => $request->shared === 'true' ? 1 : 0,
74 4
                'name'         => $request->name
75
            ]);
76
77 4
            Log::info('File added for Customer ID - '.$request->custID.' by '.Auth::user()->full_name.'.  New File ID - '.$fileID);
0 ignored issues
show
Bug introduced by
Accessing full_name 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...
78
        }
79
80
        //  Get the current progress
81 4
        $handler = $save->handler();
82
83 4
        Log::debug('File being uploaded.  Percentage done - '.$handler->getPercentageDone());
84 4
        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...
85 4
            'done'   => $handler->getPercentageDone(),
86
            'status' => true
87
        ]);
88
    }
89
90
    //  Get the files for the customer
91 4 View Code Duplication
    public function show($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...
92
    {
93 4
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name);
0 ignored issues
show
Bug introduced by
Accessing full_name 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 4
        $files = CustomerFiles::where('cust_id', $id)
96 4
            ->with('Files')
97 4
            ->with('CustomerFileTypes')
98 4
            ->with('User')
99 4
            ->get();
100
101
        //  Determine if there is a parent site with shared files
102 4
        $parent = Customers::find($id)->parent_id;
103 4
        if($parent) {
104 2
            $parentList = Customerfiles::where('cust_id', $parent)
105 2
                ->where('shared', 1)
106 2
                ->with('Files')
107 2
                ->with('CustomerFileTypes')
108 2
                ->with('User')
109 2
                ->get();
110
111 2
            $files = $files->merge($parentList);
112
        }
113
114 4
        return $files;
115
    }
116
117
    //  Update the information of the file, but not the file itself
118 4 View Code Duplication
    public function update(Request $request, $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...
119
    {
120 4
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name.'. Submitted Data - ', $request->toArray());
0 ignored issues
show
Bug introduced by
Accessing full_name 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...
121
122 4
        $request->validate([
0 ignored issues
show
Bug introduced by
The call to validate() misses a required argument $...$params.

This check looks for function calls that miss required arguments.

Loading history...
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
        Log::info('File information updated for customer file ID - '.$id.' by '.Auth::user()->full_name);
0 ignored issues
show
Bug introduced by
Accessing full_name 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...
141 4
        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...
142
    }
143
144
    //  Remove a customer file
145 2 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...
146
    {
147
        //  Remove the file from SystemFiles table
148 2
        $data = CustomerFiles::find($id);
149 2
        $fileID = $data->file_id;
150 2
        $data->delete();
151
152 2
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name);
0 ignored issues
show
Bug introduced by
Accessing full_name 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...
153 2
        Log::info('File Deleted For Customer ID-'.$data->custID.' by '.Auth::user()->full_name.'.  File ID - '.$id);
0 ignored issues
show
Bug introduced by
Accessing full_name 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...
154
155
        //  Delete from system if no longer in use
156 2
        Files::deleteFile($fileID);
157
158 2
        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...
159
    }
160
}
161