Passed
Push — dev6 ( 9c7079...72e99e )
by Ron
15:54
created

CustomerEquipmentController::restore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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

98
        event(new CustomerEquipmentDeletedEvent($cust, /** @scrutinizer ignore-type */ $equip));
Loading history...
Bug introduced by
It seems like $cust can also be of type null; however, parameter $cust of App\Events\Customers\Equ...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

98
        event(new CustomerEquipmentDeletedEvent(/** @scrutinizer ignore-type */ $cust, $equip));
Loading history...
99 2
        return back()->with([
100
            'message' => 'Equipment Deleted',
101
            'type'    => 'warning',
102
        ]);
103
    }
104
105 2
    /*
106
    *   Restore a piece of equipment that was deleted
107 2
    */
108
    public function restore($id)
109 2
    {
110
        $equip = CustomerEquipment::withTrashed()->where('cust_equip_id', $id)->first();
111 1
112 1
        $this->authorize('restore', $equip);
113 1
        $equip->restore();
114
115
        event(new CustomerEquipmentForceDeletedEvent(Customer::find($equip->cust_id), $equip));
1 ignored issue
show
Bug introduced by
It seems like App\Models\Customer::find($equip->cust_id) can also be of type null; however, parameter $cust of App\Events\Customers\Equ...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

115
        event(new CustomerEquipmentForceDeletedEvent(/** @scrutinizer ignore-type */ Customer::find($equip->cust_id), $equip));
Loading history...
116
        return back()->with([
117
            'message' => 'Equipment Restored',
118
            'type'    => 'success',
119 2
        ]);
120
    }
121 2
122
    /*
123 2
    *   Completely delete the equipment
124
    */
125 1
    public function forceDelete($id)
126 1
    {
127
        $equip = CustomerEquipment::withTrashed()->where('cust_equip_id', $id)->first();
128 1
        $this->authorize('forceDelete', $equip);
129
        $equip->forceDelete();
130
131
        event(new CustomerEquipmentRestoredEvent(Customer::find($equip->cust_id), $equip));
1 ignored issue
show
Bug introduced by
It seems like App\Models\Customer::find($equip->cust_id) can also be of type null; however, parameter $cust of App\Events\Customers\Equ...redEvent::__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

131
        event(new CustomerEquipmentRestoredEvent(/** @scrutinizer ignore-type */ Customer::find($equip->cust_id), $equip));
Loading history...
132
        return back()->with(['message' => 'Equipment permanently deleted', 'type' => 'danger']);
133
    }
134
}
135