Passed
Push — dev5 ( 3534c9...5191cd )
by Ron
06:25
created

CustomerDetailsController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Test Coverage

Coverage 46%

Importance

Changes 0
Metric Value
eloc 76
dl 0
loc 154
ccs 23
cts 50
cp 0.46
rs 10
c 0
b 0
f 0
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 25 1
A details() 0 40 4
A __construct() 0 3 1
A destroy() 0 2 1
A store() 0 30 1
A show() 0 13 2
A create() 0 6 1
1
<?php
2
3
namespace App\Http\Controllers\Customers;
4
5
use App\Customers;
6
use App\CustomerFavs;
7
use App\PhoneNumberType;
8
use App\CustomerFileTypes;
9
use Illuminate\Http\Request;
10
use App\Http\Traits\SystemsTrait;
11
use Illuminate\Support\Facades\Log;
12
use Illuminate\Support\Facades\Auth;
13
use App\Http\Controllers\Controller;
14
use Illuminate\Support\Facades\Route;
15
16
class CustomerDetailsController extends Controller
17
{
18
    use SystemsTrait;
1 ignored issue
show
introduced by
The trait App\Http\Traits\SystemsTrait requires some properties which are not provided by App\Http\Controllers\Cus...stomerDetailsController: $cat_name, $sys_id, $name
Loading history...
19
20 14
    public function __construct()
21
    {
22 14
        $this->middleware('auth');
23 14
    }
24
25
    //  New Customer Form
26 4
    public function create()
27
    {
28 4
        $this->authorize('hasAccess', 'add_customer');
29
30 2
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
31 2
        return view('customer.newCustomer');
32
    }
33
34
    //  Submit the new customer form
35 6
    public function store(Request $request)
36
    {
37 6
        $this->authorize('hasAccess', 'add_customer');
38
39 4
        $request->validate([
40 4
            'cust_id'  => 'required|numeric|unique:customers,cust_id',
41
            'name'     => 'required', // |unique:customers,name',
42
            'dba_name' => 'nullable',
43
            'address'  => 'required',
44
            'city'     => 'required',
45
            'zip'      => 'required|numeric'
46
        ]);
47
48
        //  Remove any forward slash (/) from the Customer name field
49 2
        $request->merge(['name' => str_replace('/', '-', $request->name)]);
50
51 2
        Customers::create([
52 2
            'cust_id'  => $request->cust_id,
53 2
            'name'     => $request->name,
54 2
            'dba_name' => $request->dba_name,
55 2
            'address'  => $request->address,
56 2
            'city'     => $request->city,
57 2
            'state'    => $request->selectedState,
58 2
            'zip'      => $request->zip,
59
        ]);
60
61 2
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
62 2
        Log::notice('New Customer ID-'.$request->custID.' created by User ID-'.Auth::user()->user_id);
63
64 2
        return response()->json(['success' => true ]);
65
    }
66
67
68
69
70
71
72
73
    //  Show the customer details
74
    public function details($id, $name)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

74
    public function details($id, /** @scrutinizer ignore-unused */ $name)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

74
    public function details(/** @scrutinizer ignore-unused */ $id, $name)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
    {
76
        echo 'customer details';
77
        die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
78
79
80
        $custDetails = Customers::find($id);
0 ignored issues
show
Unused Code introduced by
$custDetails = App\Customers::find($id) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
81
        $allSystems  = $this->getAllSystems();
82
83
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
84
        if(empty($custDetails))
85
        {
86
            Log::info('User ID-'.Auth::user()->user_id.' visited invalid customer ID-'.$id.'-'.$name);
87
            return view('err.customerNotFound');
88
        }
89
90
        //  Determine if the customer is one of the users bookmarks
91
        $custFav = CustomerFavs::where('user_id', Auth::user()->user_id)->where('cust_id', $custDetails->cust_id)->first();
92
        //  Get the types of phone numbers that can be assigned to a customer contact
93
        $pTypes = PhoneNumberType::all();
94
        $phoneTypes = [];
95
        foreach($pTypes as $type)
96
        {
97
            $phoneTypes[] = [
98
                'value' => $type->phone_type_id,
99
                'text'  => $type->description,
100
                'icon'  => $type->icon_class
101
            ];
102
        }
103
104
        //  Get the types of files that can be attached to a file
105
        $fileTypes = CustomerFileTypes::select('file_type_id as value', 'description as text')->get();
106
107
        Log::debug('Customer Details', $custDetails->toArray());
108
        return view('customer.details', [
109
            'details'    => $custDetails,
110
            'isFav'      => empty($custFav) ? false : true,
111
            'sysList'    => $allSystems,
112
            'phoneTypes' => $phoneTypes,
113
            'fileTypes'  => $fileTypes
114
        ]);
115
    }
116
117
    //  Get the basic details of the customer
118
    public function show($id)
119
    {
120
        $details = Customers::find($id);
121
122
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
123
        if(empty($details))
124
        {
125
            Log::info('User ID-'.Auth::user()->user_id.' visited invalid customer ID-'.$id);
126
            return response()->json(['error' => 'Customer Not Found']);
127
        }
128
129
        Log::debug('Customer Details', $details->toArray());
130
        return response()->json($details);
131
    }
132
133
    //  Update the customer details
134
    public function update(Request $request, $id)
135
    {
136
        $request->validate([
137
            'name'     => 'required',
138
            'dba_name' => 'nullable',
139
            'address'  => 'required',
140
            'city'     => 'required',
141
            'state'    => 'required',
142
            'zip'      => 'required|numeric'
143
        ]);
144
145
        Customers::find($id)->update([
146
            'name'     => $request->name,
147
            'dba_name' => $request->dba_name,
148
            'address'  => $request->address,
149
            'city'     => $request->city,
150
            'state'    => $request->state,
151
            'zip'      => $request->zip
152
        ]);
153
154
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
155
        Log::notice('Customer Details Updated for Customer ID-'.$id.' by User ID-'.Auth::user()->user_id);
156
        Log::debug('Customer Details Submitted - ', $request->toArray());
157
        return response()->json([
158
            'success' => true
159
        ]);
160
    }
161
162
    /**
163
     * Remove the specified resource from storage.
164
     *
165
     * @param  int  $id
166
     * @return \Illuminate\Http\Response
167
     */
168
    public function destroy($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

168
    public function destroy(/** @scrutinizer ignore-unused */ $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
169
    {
170
        //
171
    }
172
}
173