Passed
Push — dev6 ( 6ea9a3...2cfb0e )
by Ron
15:41
created

CustomerEquipmentController::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 8
c 3
b 0
f 0
dl 0
loc 11
rs 10
ccs 4
cts 4
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace App\Http\Controllers\Customers;
4
5
use App\Events\CustomerEquipmentAddedEvent;
6
use App\Events\CustomerEquipmentDeletedEvent;
7
use App\Events\CustomerEquipmentUpdatedEvent;
8
use App\Http\Controllers\Controller;
9
use App\Http\Requests\Customers\CustomerEquipmentRequest;
10
use App\Models\Customer;
11
use App\Models\CustomerEquipment;
12
use App\Models\CustomerEquipmentData;
13
use App\Models\DataField;
14
15
class CustomerEquipmentController extends Controller
16
{
17
    /**
18
     * Store new equipment for a customer
19
     */
20 2
    public function store(CustomerEquipmentRequest $request)
21
    {
22 2
        $cust    = Customer::findOrFail($request->cust_id);
23 2
        $cust_id = $cust->cust_id;
24
25
        //  If the equipment is shared, it must be assigned to the parent site
26 2
        if($request->shared && $cust->parent_id > 0)
27
        {
28 1
            $cust_id = $cust->parent_id;
29
        }
30
31
        //  Input the equipment type
32 2
        $newEquip = CustomerEquipment::create([
33 2
            'cust_id'  => $cust_id,
34 2
            'equip_id' => $request->equip_id,
35 2
            'shared'   => $request->shared,
36
        ]);
37
38
        //  Input the equipment data
39 2
        foreach($request->data as $field)
40
        {
41 2
            CustomerEquipmentData::create([
42 2
                'cust_equip_id' => $newEquip->cust_equip_id,
43 2
                'field_id'      => DataField::where('equip_id', $request->equip_id)->where('type_id', $field['type_id'])->first()->field_id,//  ???
44 2
                'value'         => isset($field['value']) ? $field['value'] : null,
45
            ]);
46
        }
47
48 2
        event(new CustomerEquipmentAddedEvent($cust, $newEquip));
49 2
        return redirect()->back()->with(['message' => 'Successfully Added Equipment', 'type' => 'success']);
50
    }
51
52
    /**
53
     * Update the equipment for the customer
54
     */
55 2
    public function update(CustomerEquipmentRequest $request, $id)
56
    {
57 2
        //  If the equipment is shared, it must be assigned to the parent site
58 2
        $cust = Customer::findOrFail($request->cust_id);
59 2
        if($request->shared)
60 2
        {
61
            if($cust->parent_id !== null)
62 2
            {
63
                $request->cust_id = $cust->parent_id;
64 1
            }
65 1
        }
66 1
67 1
        //  Update the Customer ID and Shared status of the equipment
68
        $equip = CustomerEquipment::find($id);
69
        $equip->update(['cust_id' => $request->cust_id, 'shared' => $request->shared]);
70 2
71
        //  Insert the data for the equipment into the database
72
        foreach($request->data as $field)
73
        {
74
            CustomerEquipmentData::where('id', $field['id'])->where('cust_equip_id', $id)->update([
75
                'value' => $field['value'],
76 2
            ]);
77
        }
78 2
79
        event(new CustomerEquipmentUpdatedEvent($cust, $equip));
80
        return redirect()->back()->with(['message' => 'Successfully Updated Equipment', 'type' => 'success']);
81 2
    }
82
83 1
    /**
84
     * Remove the specified resource from storage
85 1
     */
86
    public function destroy($id)
87
    {
88
        $equip = CustomerEquipment::find($id);
89 2
        $cust  = Customer::find($equip->cust_id);
90
        $this->authorize('delete', $equip);
91 2
        $equip->delete();
92
93 2
        event(new CustomerEquipmentDeletedEvent($cust, $equip));
2 ignored issues
show
Bug introduced by
It seems like $cust can also be of type null; however, parameter $cust of App\Events\CustomerEquip...tedEvent::__construct() does only seem to accept App\Models\Customer, maybe add an additional type check? ( Ignorable by Annotation )

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

93
        event(new CustomerEquipmentDeletedEvent(/** @scrutinizer ignore-type */ $cust, $equip));
Loading history...
Bug introduced by
It seems like $equip can also be of type null; however, parameter $equip of App\Events\CustomerEquip...tedEvent::__construct() does only seem to accept App\Models\CustomerEquipment, maybe add an additional type check? ( Ignorable by Annotation )

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

93
        event(new CustomerEquipmentDeletedEvent($cust, /** @scrutinizer ignore-type */ $equip));
Loading history...
94 2
        return back()->with([
95
            'message' => 'Equipment Deleted',
96
            'type'    => 'warning',
97
        ]);
98 2
    }
99
}
100