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

CustomerDetailsController::details()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 18
cts 18
cp 1
rs 8.8817
c 0
b 0
f 0
cc 6
nc 3
nop 2
crap 6
1
<?php
2
3
namespace App\Http\Controllers\Customers;
4
5
use App\Customers;
6
use App\CustomerFavs;
7
use App\PhoneNumberTypes;
8
use App\CustomerFileTypes;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Facades\Log;
11
use Illuminate\Support\Facades\Auth;
12
use App\Http\Controllers\Controller;
13
use Illuminate\Support\Facades\Route;
14
use App\Http\Resources\PhoneNumberTypesCollection;
15
use App\Http\Resources\CustomerFileTypesCollection;
16
17
class CustomerDetailsController extends Controller
18
{
19 44
    public function __construct()
20
    {
21 44
        $this->middleware('auth');
22 44
    }
23
24
    //  New Customer Form
25 4 View Code Duplication
    public function create()
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...
26
    {
27 4
        $this->authorize('hasAccess', 'Add Customer');
28
29 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...
30 2
        return view('customer.newCustomer');
31
    }
32
33
    //  Submit the new customer form
34 10
    public function store(Request $request)
35
    {
36 10
        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...
37 10
        $this->authorize('hasAccess', 'Add Customer');
38
39 8
        $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...
40 8
            'cust_id'   => 'nullable|numeric|unique:customers,cust_id',
41
            'parent_id' => 'nullable|numeric|exists:customers,cust_id',
42
            'name'      => 'required',
43
            'dba_name'  => 'nullable',
44
            'address'   => 'required',
45
            'city'      => 'required',
46
            'zip'       => 'required|numeric'
47
        ]);
48
49
        //  Remove any forward slash (/) from the Customer name field
50 6
        $request->merge(['name' => str_replace('/', '-', $request->name)]);
51
52
        //  Check if the parent ID noted, has a parent of its own
53 6
        if($request->parent_id)
54
        {
55 4
            $parentsParent = Customers::find($request->parent_id);
56
57 4
            if($parentsParent->parent_id)
58
            {
59 2
                $request->parent_id = $parentsParent->parent_id;
60
            }
61
        }
62
63 6
        $custData = Customers::create([
0 ignored issues
show
Bug introduced by
The method create() does not exist on App\Customers. 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...
64 6
            'cust_id'   => $request->cust_id,
65 6
            'parent_id' => $request->parent_id,
66 6
            'name'      => $request->name,
67 6
            'dba_name'  => $request->dba_name,
68 6
            'address'   => $request->address,
69 6
            'city'      => $request->city,
70 6
            'state'     => $request->selectedState,
71 6
            'zip'       => $request->zip,
72
        ]);
73
74 6
        Log::notice('New Customer ID-'.$request->custID.' created 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...
75 6
        return response()->json(['success' => true, 'cust_id' => $custData->cust_id]);
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...
76
    }
77
78
    //  Show the customer details
79 4
    public function details($id, $name)
80
    {
81 4
        $custDetails = Customers::find($id);
82
83 4
        if($custDetails === null)
84
        {
85 2
            Log::info('User - '.Auth::user()->user_id.' visited invalid customer ID - '.$id.' - '.$name);
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 2
            return view('customer.customerNotFound');
87
        }
88
89 2
        $custFav   = CustomerFavs::where('user_id', Auth::user()->user_id)->where('cust_id', $custDetails->cust_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...
90 2
        $numTypes  = new PhoneNumberTypesCollection(PhoneNumberTypes::all());
91 2
        $fileTypes = new CustomerFileTypesCollection(CustomerFileTypes::all());
92 2
        $parent    = $custDetails->parent_id ? Customers::find($custDetails->parent_id)->name : null;
93
94 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...
95 2
        return view('customer.details', [
96 2
            'cust_id'     => $custDetails->cust_id,
97 2
            'details'     => $custDetails->toJson(),
98 2
            'isFav'       => empty($custFav) ? 'false' : 'true',
99 2
            'numberTypes' => $numTypes,
100 2
            'fileTypes'   => $fileTypes,
101 2
            'parent'      => $parent,
102 2
            'linked'      => $custDetails->child_count || $parent ? 'true' : 'false',
103
        ]);
104
    }
105
106
    //  Update the customer details
107 6
    public function update(Request $request, $id)
108
    {
109 6
        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...
110
111 6
        $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...
112 6
            'name'     => 'required',
113
            'dba_name' => 'nullable',
114
            'address'  => 'required',
115
            'city'     => 'required',
116
            'state'    => 'required',
117
            'zip'      => 'required|numeric'
118
        ]);
119
120 2
        Customers::find($id)->update([
121 2
            'name'     => $request->name,
122 2
            'dba_name' => $request->dba_name,
123 2
            'address'  => $request->address,
124 2
            'city'     => $request->city,
125 2
            'state'    => $request->state,
126 2
            'zip'      => $request->zip
127
        ]);
128
129 2
        Log::info('Customer Details Updated for Customer ID-'.$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...
130 2
        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...
131 2
            'success' => true
132
        ]);
133
    }
134
135
    //  Link a site to a parent site
136 4
    public function linkParent(Request $request)
137
    {
138 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...
139
140 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...
141 4
            'parent_id' => 'required|numeric|exists:customers,cust_id',
142
            'cust_id'   => 'required|numeric|exists:customers,cust_id'
143
        ]);
144
145 4
        $parentsParent = Customers::find($request->parent_id);
146
147 4
        if($parentsParent->parent_id)
148
        {
149 2
            $request->parent_id = $parentsParent->parent_id;
150
        }
151
152 4
        Customers::find($request->cust_id)->update([
153 4
            'parent_id' => $request->parent_id,
154
        ]);
155
156 4
        Log::info('Customer ID '.$request->cust_id.' was linked to parent ID '.$request->parent_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...
157 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...
158
    }
159
160
161 2
    public function removeParent($id)
162
    {
163 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...
164 2
        Customers::find($id)->update(['parent_id' => null]);
165 2
        Log::info('Parent Customer ID was removed for Customer 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...
166 2
    }
167
168
    //  Deactivate a customer - note this will not remove it from the database, but make it inaccessable
169 4
    public function destroy($id)
170
    {
171 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...
172 4
        $this->authorize('hasAccess', 'Deactivate Customer');
173
174
        //  Remove the customer from any users favorites
175 2
        CustomerFavs::where('cust_id', $id)->delete();
176
177
        //  Disable the tip
178 2
        Customers::destroy($id);
179
180 2
        Log::notice('User - '.Auth::user()->full_name.' has deactivated Customer 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...
181 2
    }
182
}
183