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

CustomerController::search()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 9
cts 9
cp 1
rs 8.9818
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2

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\Customers;
6
use App\SystemTypes;
7
use App\CustomerFavs;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Log;
10
use Illuminate\Support\Facades\Auth;
11
use App\Http\Controllers\Controller;
12
use Illuminate\Support\Facades\Route;
13
use App\Http\Resources\CustomerCollection;
14
15
class CustomerController extends Controller
16
{
17 6
    public function __construct()
18
    {
19 6
        $this->middleware('auth');
20 6
    }
21
22
    //  Landing page to search for customer
23
    public function index()
24
    {
25
        $sysTypes = SystemTypes::all();
26
27
        $sysArr = [];
28
        foreach($sysTypes as $sys)
29
        {
30
            $sysArr[] = $sys->name;
31
        }
32
33
        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...
34
        return view('customer.index', [
35
            'sysTypes' => $sysArr
36
        ]);
37
    }
38
39
    //  Search for the customer based on their ID - For new file link form
40
    // public function searchID($id)
41
    // {
42
    //     $id = urldecode($id);
43
    //     if($id === 'NULL')
44
    //     {
45
    //         $id = '';
46
    //     }
47
48
    //     //  Determine if a customer number/name has already been entered
49
    //     if(!empty($id))
50
    //     {
51
    //         $split = explode(' ', $id);
52
    //         if(isset($split[1]) && $split[1] === '-')
53
    //         {
54
    //             $id = $split[0];
55
    //         }
56
    //     }
57
58
    //     $res = Customers::where('cust_id', 'like', '%'.$id.'%')
59
    //         ->orWhere('name', 'like', '%'.$id.'%')
60
    //         ->where('active', 1)
61
    //         ->orderBy('name')
62
    //         ->get();
63
64
    //     Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
65
    //     Log::debug('ID Entered - '.$id.' Resolution - '.$res);
66
    //     return response()->json($res);
67
    // }
68
69
    //  Search for a customer
70 6
    public function search(Request $request)
71
    {
72
        // $customers = Customers::
73
        //         with('CustomerSystems.SystemTypes')
74
        //         ->get();
75
76
        // $custList = [];
77
        // foreach($customers as $cust)
78
        // {
79
        //     $sysArr = '';
80
        //     foreach($cust->CustomerSystems as $sys)
81
        //     {
82
        //         $sysArr .= $sys->SystemTypes->name.'<br />';
83
        //     }
84
85
        //     $custList[] = [
86
        //         'cust_id' => $cust->cust_id,
87
        //         'name' => $cust->name,
88
        //         'dba'  => $cust->dba_name,
89
        //         'city' => $cust->city.', '.$cust->state,
90
        //         'url'  => route('customer.details', [$cust->cust_id, urlencode($cust->name)]),
91
        //         'sys'  => $sysArr
92
        //     ];
93
94
95
        // }
96
97
        // Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
98
        // Log::debug('Customer Data - ', $custList);
99
        // return response()->json($custList);
100
101
102
        // $searchResults = new CustomerCollection(
103
        //     Customers::where('cust_id', $request->search)
104
        //         ->orWhere('name', $request->search)
105
        //         ->orWhere('dba_name', $request->search)
106
        //         ->get()
107
        //     );
108
109 6
        if($request->search)
110
        {
111 4
            $searchResults = new CustomerCollection(
112 4
            Customers::where('cust_id', 'like', '%'.$request->search.'%')
113 4
                ->orWhere('name', 'like', '%'.$request->search.'%')
114 4
                ->orWhere('dba_name', 'like', '%'.$request->search.'%')
115 4
                ->get()
116
            );
117
        }
118
        else
119
        {
120 2
            $searchResults = new CustomerCollection(Customers::all());
121
        }
122
123 6
        return $searchResults;
124
    }
125
126
    //  Check to see if a customer ID already exists
127
    public function checkID($id)
128
    {
129
        $cust = Customers::find($id);
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
        if($cust === null)
133
        {
134
            Log::debug('Customer ID - '.$id.' is available to use');
135
            return response()->json(['dup' => false]);
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...
136
        }
137
138
139
        Log::debug('Customer ID is in use by - '.$cust->name);
140
        return response()->json(['dup' => true, 'url' => route('customer.details', [$cust->cust_id, urlencode($cust->name)])]);
141
    }
142
143
    //  Toggle whether or not the customer is listed as a user favorite
144
    public function toggleFav($action, $id)
145
    {
146
        switch($action)
147
        {
148
            case 'add':
149
                CustomerFavs::create([
150
                    '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...
151
                    'cust_id' => $id
152
                ]);
153
                break;
154
            case 'remove':
155
                $custFav = CustomerFavs::where('user_id', Auth::user()->user_id)->where('cust_id', $id)->first();
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
                $custFav->delete();
157
                break;
158
        }
159
160
        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...
161
        Log::debug('Customer Bookmark Updated.', [
162
            '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...
163
            'cust_id' => $id,
164
            'action'  => $action
165
        ]);
166
        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...
167
    }
168
}
169