Passed
Push — dev6 ( 72e99e...1f4a51 )
by Ron
18:35
created

CustomerNoteController::update()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 25
rs 9.7333
ccs 14
cts 14
cp 1
cc 3
nc 2
nop 2
crap 3
1
<?php
2
3
namespace App\Http\Controllers\Customers;
4
5
use App\Http\Controllers\Controller;
6
use App\Http\Requests\Customers\CustomerNoteRequest;
7
8
use App\Events\Customers\Notes\CustomerNoteAddedEvent;
9
use App\Events\Customers\Notes\CustomerNoteDeletedEvent;
10
use App\Events\Customers\Notes\CustomerNoteUpdatedEvent;
11
use App\Events\Customers\Notes\CustomerNoteRestoredEvent;
12
use App\Events\Customers\Notes\CustomerNoteForceDeletedEvent;
13
14
use App\Models\Customer;
15
use App\Models\CustomerNote;
16
17 2
class CustomerNoteController extends Controller
18
{
19 2
    /**
20 2
     * Store a new customer note
21
     */
22
    public function store(CustomerNoteRequest $request)
23 2
    {
24
        $cust    = Customer::findOrFail($request->cust_id);
25 1
        $cust_id = $cust->cust_id;
26
27
        //  If the equipment is shared, it must be assigned to the parent site
28 2
        if($request->shared && $cust->parent_id > 0)
29 2
        {
30 2
            $cust_id = $cust->parent_id;
31 2
        }
32 2
33 2
        $note = CustomerNote::create([
34 2
            'cust_id'    => $cust_id,
35
            'created_by' => $request->user()->user_id,
36
            'urgent'     => $request->urgent,
37 2
            'shared'     => $request->shared,
38 2
            'subject'    => $request->subject,
39
            'details'    => $request->details,
40
        ]);
41
42
        event(new CustomerNoteAddedEvent($cust, $note));
43
        return back()->with([
44 2
            'message' => 'Note Created',
45
            'type'    => 'success',
46 2
        ]);
47
    }
48 2
49 2
    /**
50
     * Update an existing customer note
51 1
     */
52 2
    public function update(CustomerNoteRequest $request, $id)
53 2
    {
54 2
        $cust    = Customer::findOrFail($request->cust_id);
55
        $cust_id = $cust->cust_id;
56
57
        //  If the equipment is shared, it must be assigned to the parent site
58
        if($request->shared && $cust->parent_id > 0)
59
        {
60 2
            $cust_id = $cust->parent_id;
61
        }
62 2
63 2
        $note = CustomerNote::find($id);
64
        $note->update([
65
            'cust_id'    => $cust_id,
66 2
            'updated_by' => $request->user()->user_id,
67
            'urgent'     => $request->urgent,
68 1
            'shared'     => $request->shared,
69
            'subject'    => $request->subject,
70
            'details'    => $request->details,
71 2
        ]);
72 2
73 2
        event(new CustomerNoteUpdatedEvent($cust, $note));
1 ignored issue
show
Bug introduced by
It seems like $note can also be of type null; however, parameter $note of App\Events\Customers\Not...tedEvent::__construct() does only seem to accept App\Models\CustomerNote, 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

73
        event(new CustomerNoteUpdatedEvent($cust, /** @scrutinizer ignore-type */ $note));
Loading history...
74 2
        return back()->with([
75 2
            'message' => 'Note Updated',
76 2
            'type'    => 'success',
77 2
        ]);
78
    }
79
80 2
    /**
81 2
     * Delete a customer note
82
     */
83
    public function destroy($id)
84
    {
85
        $this->authorize('delete', CustomerNote::class);
86
        $note = CustomerNote::find($id);
87 2
        $note->delete();
88
89 2
        event(new CustomerNoteDeletedEvent(Customer::find($note->cust_id), $note));
2 ignored issues
show
Bug introduced by
It seems like App\Models\Customer::find($note->cust_id) can also be of type null; however, parameter $cust of App\Events\Customers\Not...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

89
        event(new CustomerNoteDeletedEvent(/** @scrutinizer ignore-type */ Customer::find($note->cust_id), $note));
Loading history...
Bug introduced by
It seems like $note can also be of type null; however, parameter $note of App\Events\Customers\Not...tedEvent::__construct() does only seem to accept App\Models\CustomerNote, 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

89
        event(new CustomerNoteDeletedEvent(Customer::find($note->cust_id), /** @scrutinizer ignore-type */ $note));
Loading history...
90
        return back()->with([
91 1
            'message' => 'Note Deleted',
92
            'type'    => 'danger',
93 1
        ]);
94 1
    }
95
96
    /*
97
    *   Restore a deleted note
98
    */
99
    public function restore($id)
100 2
    {
101
        $this->authorize('restore', CustomerNote::class);
102 2
        $note = CustomerNote::withTrashed()->where('note_id', $id)->first();
103 1
        $note->restore();
104 1
105
        event(new CustomerNoteRestoredEvent(Customer::find($note->cust_id), $note));
1 ignored issue
show
Bug introduced by
It seems like App\Models\Customer::find($note->cust_id) can also be of type null; however, parameter $cust of App\Events\Customers\Not...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

105
        event(new CustomerNoteRestoredEvent(/** @scrutinizer ignore-type */ Customer::find($note->cust_id), $note));
Loading history...
106 1
        return redirect()->back()->with(['message' => 'Customer Note restored', 'type' => 'success']);
107 1
    }
108
109
    /*
110
    *   Permanently delete a note
111
    */
112
    public function forceDelete($id)
113 2
    {
114
        $this->authorize('forceDelete', CustomerNote::class);
115 2
116
        $note = CustomerNote::withTrashed()->where('note_id', $id)->first();
117 1
        $note->forceDelete();
118
119 1
        event(new CustomerNoteForceDeletedEvent(Customer::find($note->cust_id), $note));
1 ignored issue
show
Bug introduced by
It seems like App\Models\Customer::find($note->cust_id) can also be of type null; however, parameter $cust of App\Events\Customers\Not...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

119
        event(new CustomerNoteForceDeletedEvent(/** @scrutinizer ignore-type */ Customer::find($note->cust_id), $note));
Loading history...
120 1
        return redirect()->back()->with(['message' => 'Note permanently deleted', 'type' => 'danger']);
121
    }
122
}
123