Passed
Push — dev5 ( e66ef8...5a922b )
by Ron
05:53
created

CustomerSystemsController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Customers;
4
5
use App\Customers;
6
use App\SystemTypes;
7
use App\CustomerSystems;
8
use Illuminate\Http\Request;
9
use App\CustomerSystemData;
10
use App\SystemCustDataFields;
0 ignored issues
show
Bug introduced by
The type App\SystemCustDataFields was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use App\Http\Traits\SystemsTrait;
12
use Illuminate\Support\Facades\Log;
13
use Illuminate\Support\Facades\Auth;
14
use App\Http\Controllers\Controller;
15
use Illuminate\Support\Facades\Route;
16
17
18
19
// use App\SystemTypes;
20
use App\SystemDataFields;
21
use App\SystemCategories;
22
use App\Http\Resources\SystemCategoriesCollection as CategoriesCollection;
23
24
// use App\Http\Resources\CustomerSystemsCollection;
25
26
class CustomerSystemsController extends Controller
27
{
28
    // use SystemsTrait;
29
30 22
    public function __construct()
31
    {
32 22
        $this->middleware('auth');
33 22
    }
34
35
    //  Get the possible system types that can be assigned to the customer
36 2
    public function index()
37
    {
38 2
        $sysList = new CategoriesCollection(SystemCategories::with('SystemTypes')->with('SystemTypes.SystemDataFields.SystemDataFieldTypes')->get());
39
40 2
        return $sysList;
41
    }
42
43
    //  Store a new system for the customer
44 2
    public function store(Request $request)
45
    {
46 2
        $request->validate([
47 2
            'cust_id' => 'required',
48
            'system'  => 'required'
49
            //  TODO:  validate system is unique to customer (write a test for it)
50
        ]);
51
52
        //  Insert the system into the DB
53 2
        $sys = CustomerSystems::create([
54 2
            'cust_id' => $request->cust_id,
55 2
            'sys_id'  => $request->system
56
        ]);
57
58
        //  Get the data fields for the new system
59 2
        $fields = SystemDataFields::where('sys_id', $request->system)->get();
60
61
        //  Enter each of the data fields into the DB
62 2
        foreach($fields as $field)
63
        {
64
            $data = 'field_'.$field->field_id;
65
            CustomerSystemData::create([
66
                'cust_sys_id' => $sys->cust_sys_id,
67
                'field_id'    => $field->field_id,
68
                'value'       => isset($request->$data) ? $request->$data : null
69
            ]);
70
        }
71
72 2
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
73 2
        Log::info('New Customer System Added - Customer ID-'.$request->cust_id.' System ID-'.$request->system);
74 2
        Log::debug('Submitted System Data', $request->toArray());
75 2
        return response()->json(['success' => true]);
76
    }
77
78
    //  Get the list of systems attached to the customer
79 4
    public function show($id)
80
    {
81 4
        $sysList = CustomerSystems::where('cust_id', $id)
82 4
                    ->with('SystemTypes')
83 4
                    ->with('SystemDataFields')
84 4
                    ->with('SystemDataFields.SystemDataFieldTypes')
85 4
                    ->get();
86
87 4
        return $sysList;
88
    }
89
90
    // Update the customers system data
91 2
    public function update(Request $request, $id)
92
    {
93 2
        $request->validate([
94 2
            'cust_id'    => 'required',
95
            'system'    => 'required',
96
        ]);
97
98
        //  Verify the system type and customer ID match
99 2
        $valid = CustomerSystems::where('cust_id', $request->cust_id)->where('cust_sys_id', $id)->first();
100
101 2
        if(!$valid)
102
        {
103
            //  TODO:  Make this a proper error code
104
            return response('Customer System Not Found');
105
        }
106
107 2
        $fields = SystemDataFields::where('sys_id', $request->system)->get();
108
109 2
        foreach($fields as $data)
110
        {
111
            $fieldName = 'field_' . $data->field_id;
112
            if(isset($request->$fieldName))
113
            {
114
                Log::debug($request->$fieldName);
115
            }
116
            Log::debug($fieldName);
117
            CustomerSystemData::where('cust_sys_id', $id)->where('field_id', $data->field_id)->update([
118
                'value'       => isset($request->$fieldName) ? $request->$fieldName : null
119
            ]);
120
        }
121
122 2
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
123 2
        Log::notice('Customer System Updated.  Cust ID-'.$request->custID.' System ID-'.$request->sysstem.' User ID-'.Auth::user()->user_id);
124 2
        Log::debug('Submitted Data - ID:'.$id.' - ', $request->toArray());
125 2
        return response()->json(['success' => true]);
126
    }
127
128
    //  Delete a system attached to a customer
129 2
    public function destroy($id)
130
    {
131
        // return response('deleted '.$id);
132 2
        $system = CustomerSystems::find($id);
133
134 2
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
135 2
        Log::notice('Customer System Deleted for Customer ID-'.$system->cust_id.' by User ID-'.Auth::user()->user_id.'. System ID-'.$id);
136 2
        Log::debug('System Data', $system->toArray());
137
138 2
        $system->delete();
139
140 2
        return response()->json(['success' => true]);
141
    }
142
}
143