Passed
Push — dev6 ( 1f4a51...d7b093 )
by Ron
16:11
created

CustomerFileController::store()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 28
rs 9.7
cc 3
nc 2
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Customers;
4
5
use App\Traits\FileTrait;
6
use Illuminate\Http\Request;
7
8
use App\Models\Customer;
9
use App\Models\CustomerFile;
10
use App\Models\CustomerFileType;
11
12
use App\Http\Controllers\Controller;
13
use App\Http\Requests\Customers\CustomerFileRequest;
14
15
use App\Events\Customers\Files\CustomerFileAddedEvent;
16
use App\Events\Customers\Files\CustomerFileDeletedEvent;
17
use App\Events\Customers\Files\CustomerFileForceDeletedEvent;
18
use App\Events\Customers\Files\CustomerFileRestoredEvent;
19
use App\Events\Customers\Files\CustomerFileUpdatedEvent;
20
21
class CustomerFileController extends Controller
22
{
23
    use FileTrait;
1 ignored issue
show
Bug introduced by
The trait App\Traits\FileTrait requires the property $username which is not provided by App\Http\Controllers\Cus...\CustomerFileController.
Loading history...
24
25
    /**
26
     * Store a file for a specific customer
27
     */
28
    public function store(CustomerFileRequest $request)
29
    {
30
        //  Make sure that the file information was stored in the users session
31
        $this->checkForFile();
32
        $fileData = $request->session()->pull('new-file-upload');
33
34
        $cust    = Customer::findOrFail($request->cust_id);
35
        $cust_id = $cust->cust_id;
36
37
        //  If the equipment is shared, it must be assigned to the parent site
38
        if($request->shared && $cust->parent_id > 0)
39
        {
40
            $cust_id = $cust->parent_id;
41
        }
42
43
        $newFile = CustomerFile::create([
44
            'file_id'      => $fileData[0]->file_id,
45
            'file_type_id' => CustomerFileType::where('description', $request->type)->first()->file_type_id,
46
            'cust_id'      => $cust_id,
47
            'user_id'      => $request->user()->user_id,
48
            'shared'       => $request->shared,
49
            'name'         => $request->name,
50
        ]);
51
52
        event(new CustomerFileAddedEvent(Customer::find($request->cust_id), $newFile));
1 ignored issue
show
Bug introduced by
It seems like App\Models\Customer::find($request->cust_id) can also be of type null; however, parameter $cust of App\Events\Customers\Fil...dedEvent::__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

52
        event(new CustomerFileAddedEvent(/** @scrutinizer ignore-type */ Customer::find($request->cust_id), $newFile));
Loading history...
53
        return back()->with([
54
            'message' => 'File Added',
55
            'type'    => 'success',
56
        ]);
57
    }
58
59
    /**
60
     * Update the details for a customer uploaded file - not the file itself
61
     */
62
    public function update(CustomerFileRequest $request, $id)
63
    {
64
        $cust    = Customer::findOrFail($request->cust_id);
65
        $cust_id = $cust->cust_id;
66
67
        //  If the equipment is shared, it must be assigned to the parent site
68
        if($request->shared && $cust->parent_id > 0)
69
        {
70
            $cust_id = $cust->parent_id;
71
        }
72
73
        $fileData = CustomerFile::find($id);
74
        $fileData->update([
75
            'file_type_id' => CustomerFileType::where('description', $request->type)->first()->file_type_id,
76
            'cust_id'      => $cust_id,
77
            'shared'       => $request->shared,
78
            'name'         => $request->name,
79
        ]);
80
81
        event(new CustomerFileUpdatedEvent($cust, $fileData));
1 ignored issue
show
Bug introduced by
It seems like $fileData can also be of type null; however, parameter $file of App\Events\Customers\Fil...tedEvent::__construct() does only seem to accept App\Models\CustomerFile, 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

81
        event(new CustomerFileUpdatedEvent($cust, /** @scrutinizer ignore-type */ $fileData));
Loading history...
82
        return back()->with([
83
            'message' => 'File Information Updated',
84
            'type'    => 'success'
85
        ]);
86
    }
87
88
    /**
89
     * Delete a customer file
90
     */
91
    public function destroy($id)
92
    {
93
        $this->authorize('delete', CustomerFile::class);
94
        $file = CustomerFile::find($id);
95
        $file->delete();
96
97
        event(new CustomerFileDeletedEvent(Customer::find($file->cust_id), $file));
2 ignored issues
show
Bug introduced by
It seems like $file can also be of type null; however, parameter $file of App\Events\Customers\Fil...tedEvent::__construct() does only seem to accept App\Models\CustomerFile, 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

97
        event(new CustomerFileDeletedEvent(Customer::find($file->cust_id), /** @scrutinizer ignore-type */ $file));
Loading history...
Bug introduced by
It seems like App\Models\Customer::find($file->cust_id) can also be of type null; however, parameter $cust of App\Events\Customers\Fil...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

97
        event(new CustomerFileDeletedEvent(/** @scrutinizer ignore-type */ Customer::find($file->cust_id), $file));
Loading history...
98
        return back()->with([
99
            'message' => 'File Deleted',
100
            'type'    => 'danger',
101
        ]);
102
    }
103
104
    /*
105
    *   Restore a deleted file
106
    */
107
    public function restore($id)
108
    {
109
        $this->authorize('restore', CustomerFile::class);
110
        $file = CustomerFile::withTrashed()->where('cust_file_id', $id)->first();
111
        $file->restore();
112
113
        event(new CustomerFileRestoredEvent(Customer::find($file->cust_id), $file));
1 ignored issue
show
Bug introduced by
It seems like App\Models\Customer::find($file->cust_id) can also be of type null; however, parameter $cust of App\Events\Customers\Fil...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

113
        event(new CustomerFileRestoredEvent(/** @scrutinizer ignore-type */ Customer::find($file->cust_id), $file));
Loading history...
114
        return back()->with(['message' => 'Customer File restored', 'type' => 'success']);
115
    }
116
117
    /*
118
    *   Permanently delete a file
119
    */
120
    public function forceDelete($id)
121
    {
122
        $this->authorize('forceDelete', CustomerFile::class);
123
124
        $file = CustomerFile::withTrashed()->where('cust_file_id', $id)->first();
125
        $file->forceDelete();
126
        $this->deleteFile($file->file_id);
127
128
        event(new CustomerFileForceDeletedEvent(Customer::find($file->cust_id), $file));
1 ignored issue
show
Bug introduced by
It seems like App\Models\Customer::find($file->cust_id) can also be of type null; however, parameter $cust of App\Events\Customers\Fil...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

128
        event(new CustomerFileForceDeletedEvent(/** @scrutinizer ignore-type */ Customer::find($file->cust_id), $file));
Loading history...
129
        return back()->with(['message' => 'File permanently deleted', 'type' => 'danger']);
130
    }
131
}
132