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

CustomerSystemsController::store()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5.3906

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 15
cts 20
cp 0.75
rs 8.9528
c 0
b 0
f 0
cc 5
nc 4
nop 1
crap 5.3906
1
<?php
2
3
namespace App\Http\Controllers\Customers;
4
5
use App\Customers;
6
use App\CustomerSystems;
7
use App\SystemDataFields;
8
use App\SystemCategories;
9
use App\CustomerSystemData;
10
use Illuminate\Http\Request;
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
use App\Http\Resources\SystemCategoriesCollection as CategoriesCollection;
16
17
class CustomerSystemsController extends Controller
18
{
19 28
    public function __construct()
20
    {
21 28
        $this->middleware('auth');
22 28
    }
23
24
    //  Get the possible system types that can be assigned to the customer
25 2 View Code Duplication
    public function index()
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 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...
28 2
        $sysList = new CategoriesCollection(SystemCategories::with('SystemTypes')->with('SystemTypes.SystemDataFields.SystemDataFieldTypes')->get());
29
30 2
        return $sysList;
31
    }
32
33
    //  Store a new system for the customer
34 4
    public function store(Request $request)
35
    {
36 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...
37
38 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...
39 4
            'cust_id' => 'required',
40
            'system'  => 'required'
41
            //  TODO:  validate system is unique to customer (write a test for it)
42
        ]);
43
44
        //  Determine if the system is supposed to be added for the parent, or this site
45 4
        $details = Customers::find($request->cust_id);
46 4
        if($details->parent_id && $request->shared == 1)
47
        {
48 2
            $request->cust_id = $details->parent_id;
49
        }
50
51
        //  Insert the system into the DB
52 4
        $sys = CustomerSystems::create([
0 ignored issues
show
Bug introduced by
The method create() does not exist on App\CustomerSystems. 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...
53 4
            'cust_id' => $request->cust_id,
54 4
            'sys_id'  => $request->system,
55 4
            'shared'  => $request->shared,
56
        ]);
57
58
        //  Get the data fields for the new system
59 4
        $fields = SystemDataFields::where('sys_id', $request->system)->get();
60
61
        //  Enter each of the data fields into the DB
62 4
        foreach($fields as $field)
63
        {
64
            $data = 'field_'.$field->field_id;
65
            CustomerSystemData::create([
0 ignored issues
show
Bug introduced by
The method create() does not exist on App\CustomerSystemData. 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...
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 4
        Log::info('New Customer System Added by '.Auth::user()->user_id.' - Customer ID - '.$request->cust_id.' System ID - '.$request->system);
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
        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...
74
    }
75
76
    //  Get the list of systems attached to the customer
77 6 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...
78
    {
79 6
        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...
80
81 6
        $sysList = CustomerSystems::where('cust_id', $id)
82 6
                    ->with('SystemTypes')
83 6
                    ->with('SystemDataFields')
84 6
                    ->with('SystemDataFields.SystemDataFieldTypes')
85 6
                    ->orderBy('cust_sys_id', 'DESC')
86 6
                    ->get();
87
88
        //  determine if there is a parent site with shared systems
89 6
        $parent = Customers::findOrFail($id)->parent_id;
90 4
        if($parent != null)
91
        {
92 2
            $parentList = CustomerSystems::where('cust_id', $parent)
93 2
                                ->where('shared', 1)
94 2
                                ->with('SystemTypes')
95 2
                                ->with('SystemDataFields')
96 2
                                ->with('SystemDataFields.SystemDataFieldTypes')
97 2
                                ->get();
98
99 2
            $sysList = $sysList->merge($parentList);
100
        }
101
102 4
        return $sysList;
103
    }
104
105
    // Update the customers system data
106 4
    public function update(Request $request, $id)
107
    {
108 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...
109
110 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...
111 4
            'cust_id'    => 'required',
112
            'system'    => 'required',
113
        ]);
114
115
        //  Verify the system type and customer ID match
116 4
        $valid = CustomerSystems::where('cust_id', $request->cust_id)->where('cust_sys_id', $id)->first();
117
118 4
        if(!$valid)
119
        {
120 2
            return abort(400);
121
        }
122
123 2
        $fields = SystemDataFields::where('sys_id', $request->system)->get();
124
125 2
        foreach($fields as $data)
126
        {
127
            $fieldName = 'field_' . $data->field_id;
128
            if(isset($request->$fieldName))
129
            {
130
                Log::debug($request->$fieldName);
131
            }
132
            Log::debug($fieldName);
133
            CustomerSystemData::where('cust_sys_id', $id)->where('field_id', $data->field_id)->update([
134
                'value'       => isset($request->$fieldName) ? $request->$fieldName : null
135
            ]);
136
        }
137
138 2
        Log::info('Customer System Updated.  Cust ID-'.$request->custID.' System ID-'.$request->sysstem.' 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...
139 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...
140
    }
141
142
    //  Delete a system attached to a customer
143 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...
144
    {
145 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...
146
147
        // return response('deleted '.$id);
148 2
        $system = CustomerSystems::find($id);
149
150 2
        Log::notice('Customer System Deleted for Customer ID-'.$system->cust_id.' by '.Auth::user()->full_name.'. System 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...
151
152 2
        $system->delete();
153
154 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...
155
    }
156
}
157