Test Failed
Push — dev6 ( ab8d6f...f89a9a )
by Ron
19:50
created

TechTipsController   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 226
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 18
eloc 100
c 3
b 0
f 0
dl 0
loc 226
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A edit() 0 9 1
A destroy() 0 10 1
A show() 0 32 2
A create() 0 7 1
A store() 0 37 4
B update() 0 73 8
A index() 0 10 1
1
<?php
2
3
namespace App\Http\Controllers\TechTips;
4
5
use Inertia\Inertia;
6
use Illuminate\Support\Str;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Auth;
9
use App\Http\Controllers\Controller;
10
11
use App\Traits\FileTrait;
12
13
use App\Models\TechTip;
14
use App\Models\TechTipType;
15
use App\Models\TechTipFile;
16
use App\Models\TechTipEquipment;
17
use App\Models\EquipmentCategory;
18
use App\Models\UserTechTipBookmark;
19
20
use App\Events\TechTips\TechTipCreatedEvent;
21
use App\Events\TechTips\TechTipDeletedEvent;
22
use App\Events\TechTips\TechTipUpdatedEvent;
23
use App\Http\Requests\TechTips\CreateTipRequest;
24
use App\Http\Requests\TechTips\UpdateTipRequest;
25
26
class TechTipsController extends Controller
27
{
28
    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\TechTips\TechTipsController.
Loading history...
29
30
    /**
31
     * Tech Tips search page
32
     */
33
    public function index(Request $request)
34
    {
35
        $filterData = [
36
            'tip_types'   => TechTipType::all(),
37
            'equip_types' => EquipmentCategory::with('EquipmentType')->get(),
38
        ];
39
40
        return Inertia::render('TechTips/Index', [
41
            'filter_data' => $filterData,
42
            'create'      => $request->user()->can('create', TechTip::class),
43
        ]);
44
    }
45
46
    /**
47
     * Show the form for creating a new Tech Tip
48
     */
49
    public function create()
50
    {
51
        $this->authorize('create', TechTip::class);
52
53
        return Inertia::render('TechTips/Create', [
54
            'tip_types' => TechTipType::all(),
55
            'equipment' => EquipmentCategory::with('EquipmentType')->get(),
56
        ]);
57
    }
58
59
    /**
60
     * Store a newly created Tech Tip
61
     */
62
    public function store(CreateTipRequest $request)
63
    {
64
        //  Create the new Tip
65
        $newTip = TechTip::create([
66
            'user_id'     => $request->user()->user_id,
67
            'tip_type_id' => $request->tip_type_id,
68
            'sticky'      => $request->sticky,
69
            'subject'     => $request->subject,
70
            'slug'        => Str::slug($request->subject),
71
            'details'     => $request->details,
72
        ]);
73
74
        //  Add the equipment for this Tech Tip
75
        foreach($request->equipment as $equip)
76
        {
77
            TechTipEquipment::create([
78
                'tip_id'   => $newTip->tip_id,
79
                'equip_id' => $equip['equip_id'],
80
            ]);
81
        }
82
83
        //  If any files were included, move them to the proper folder and attach to tip
84
        $fileData = $request->session()->pull('new-file-upload');
85
        if($fileData)
86
        {
87
            foreach($fileData as $file)
88
            {
89
                $this->moveStoredFile($file->file_id, $newTip->tip_id);
90
                TechTipFile::create([
91
                    'tip_id'  => $newTip->tip_id,
92
                    'file_id' => $file->file_id,
93
                ]);
94
            }
95
        }
96
97
        event(new TechTipCreatedEvent($newTip));
98
        return redirect(route('tech-tips.show', $newTip->slug));
99
    }
100
101
    /**
102
     * Display the Tech Tip
103
     */
104
    public function show($id)
105
    {
106
        //  Check if we are passing the Tech Tip name or number
107
        if(is_numeric($id))
108
        {
109
            //  To keep things uniform, redirect to a link that has the Tech Tip subject rather than the ID
110
            $tip = TechTip::findOrFail($id);
111
            return redirect(route('tech-tips.show', $tip->slug));
112
        }
113
114
        //  Pull the Tech Tip Information
115
        $tip = TechTip::where('slug', $id)
116
                ->with('EquipmentType')
117
                ->with('FileUploads')
118
                ->with('TechTipComment.User')
119
                ->firstOrFail()
120
                ->makeHidden(['summary', 'sticky']);
121
122
        //  Determine if the Tech Tip is bookmarked by the user
123
        $isFav = UserTechTipBookmark::where('user_id', Auth::user()->user_id)
1 ignored issue
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?
Loading history...
124
                    ->where('tip_id', $tip->tip_id)
125
                    ->count();
126
127
        return Inertia::render('TechTips/Show', [
128
            'tip' => $tip,
129
            //  User Permissions for Tech Tips
130
            'user_data' => [
131
                'fav' => $isFav,
132
                'permissions' => [
133
                    'edit'      => Auth::user()->can('update', $tip),
134
                    'delete'    => Auth::user()->can('delete', $tip),
135
                    'manage'    => Auth::user()->can('manage', TechTip::class),
136
                    // 'comment'   => [
137
                    //     'create'  => Auth::user()->can('create', TechTipComment::class),
138
                    //     'manage'  => Auth::user()->can('manage', TechTipComment::class),
139
                    // ],
140
                ],
141
            ]
142
        ]);
143
    }
144
145
    /**
146
     * Show the form to Edit the Tech Tip
147
     */
148
    public function edit($id)
149
    {
150
        $tip = TechTip::with(['EquipmentType', 'FileUploads'])->findOrFail($id)->makeVisible(['tip_type_id']);
151
        $this->authorize('update', $tip);
152
153
        return Inertia::render('TechTips/Edit', [
154
            'data'      => $tip,
155
            'tip_types' => TechTipType::all(),
156
            'equipment' => EquipmentCategory::with('EquipmentType')->get(),
157
        ]);
158
    }
159
160
    /**
161
     * Update the Tech Tip
162
     */
163
    public function update(UpdateTipRequest $request, $id)
164
    {
165
        //  Update the Tech Tip
166
        $tip = TechTip::findOrFail($id);
167
        $tip->update([
168
            'updated_id'  => $request->user()->user_id,
169
            'tip_type_id' => $request->tip_type_id,
170
            'sticky'      => $request->sticky,
171
            'subject'     => $request->subject,
172
            'slug'        => Str::slug($request->subject),
173
            'details'     => $request->details,
174
        ]);
175
176
        //  Update the equipment attached to the Tech Tip
177
        $currentEquip = TechTipEquipment::where('tip_id', $id)->get();
178
        $newEquip     = [];
179
180
        foreach($request->equipment as $equip)
181
        {
182
            //  If the laravel_through_key value exists, then it was an existing equipment that has stayed in place
183
            if(isset($equip['laravel_through_key']))
184
            {
185
                //  Remove that piece from the current equipment list so it is not updated later
186
                $currentEquip = $currentEquip->filter(function($i) use ($equip)
187
                {
188
                    return $i->equip_id != $equip['equip_id'];
189
                });
190
            }
191
            else
192
            {
193
                $newEquip[] = $equip;
194
            }
195
        }
196
197
        //  Remove the Equipment left over in the CurrentEquipment array
198
        foreach($currentEquip as $cur)
199
        {
200
            TechTipEquipment::find($cur->tip_equip_id)->delete();
201
        }
202
203
        //  Add the new equipment
204
        foreach($newEquip as $new)
205
        {
206
            TechTipEquipment::create([
207
                'tip_id'   => $id,
208
                'equip_id' => $new['equip_id'],
209
            ]);
210
        }
211
212
        //  Remove any files that are no longer needed
213
        foreach($request->removedFiles as $file)
214
        {
215
            TechTipFile::where('tip_id', $tip->tip_id)->where('file_id', $file)->first()->delete();
216
            $this->deleteFile($file);
217
        }
218
219
        //  Add any additional files
220
        $fileData = $request->session()->pull('new-file-upload');
221
        if($fileData)
222
        {
223
            foreach($fileData as $file)
224
            {
225
                TechTipFile::create([
226
                    'tip_id'  => $tip->tip_id,
227
                    'file_id' => $file->file_id,
228
                ]);
229
            }
230
        }
231
232
        event(new TechTipUpdatedEvent($tip));
233
        return redirect(route('tech-tips.show', $tip->slug))->with([
234
            'message' => 'Tech Tip Updated',
235
            'type'    => 'success',
236
        ]);
237
    }
238
239
    /**
240
     * Soft Delete the Tech Tip
241
     */
242
    public function destroy($id)
243
    {
244
        $tip = TechTip::findOrFail($id);
245
        $this->authorize('delete', $tip);
246
        $tip->delete();
247
248
        event(new TechTipDeletedEvent($tip));
249
        return redirect(route('tech-tips.index'))->with([
250
            'message' => 'Tech Tip Deleted',
251
            'type'    => 'danger',
252
        ]);
253
    }
254
}
255