Passed
Push — dev6 ( fe5c4e...e6ba74 )
by Ron
16:02
created

TechTipsController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 1
c 3
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Http\Controllers\TechTips;
4
5
use Inertia\Inertia;
6
7
use Illuminate\Http\Request;
8
9
use App\Http\Controllers\Controller;
10
use App\Http\Requests\TechTips\NewTipRequest;
11
use App\Models\EquipmentCategory;
12
use App\Models\EquipmentType;
13
use App\Models\FileUploads;
14
use App\Models\TechTip;
15
use App\Models\TechTipFile;
16
use App\Models\TechTipType;
17
use App\Traits\FileTrait;
18
use Illuminate\Support\Facades\Log;
19
use Illuminate\Support\Str;
20
21
class TechTipsController extends Controller
22
{
23
    use FileTrait;
24
25
    protected $disk;
26
    protected $tmpFolder;
27
28
    public function __construct()
29
    {
30
        $this->disk      = 'tips';
31
        $this->tmpFolder = '_tmp';
32
    }
33
34
    /**
35
     *  Show all of the Tech Tips
36
     */
37
    public function index()
38
    {
39
        return Inertia::render('TechTips/index');
40
    }
41
42
    /**
43
     *  Form to create a new Tech Tip
44
     */
45
    public function create()
46
    {
47
        $this->authorize('create', TechTip::class);
48
49
        return Inertia::render('TechTips/create', [
50
            'tipTypes'  => TechTipType::all(),
51
            'equipment' => EquipmentCategory::with('EquipmentType')->get(),
52
        ]);
53
    }
54
55
    /**
56
     *  Create the new Tech Tip
57
     */
58
    public function store(NewTipRequest $request)
59
    {
60
        //  Determine if we are in the process of uploading a file, or if this is the initial request
61
        if($request->file)
62
        {
63
            $status = $this->getChunk($request, $this->disk, $this->tmpFolder);
64
65
            //  If the file is completely uploaded, save the name and location in session data and move onto the next file
66
            if($status['done'] === 100)
67
            {
68
                $newFile = FileUploads::create([
69
                    'disk'      => $this->disk,
70
                    'folder'    => $this->tmpFolder,
71
                    'file_name' => $status['filename'],
72
                    'public'    => 1,
73
                ]);
74
75
                $fileArr = session('new-file-upload') !== null ? session('new-file-upload') : [];
76
                $fileArr[] = $newFile;
77
78
                session(['new-file-upload' => $fileArr]);
79
80
                return response()->noContent();
81
            }
82
83
            //  Continue the file upload
84
            return response($status);
85
        }
86
87
        //  All file uploads are done, create the new Tech Tip
88
        $newTip = TechTip::create([
89
            'user_id'     => $request->user()->user_id,
90
            'tip_type_id' => $request->tip_type_id,
91
            'sticky'      => $request->sticky,
92
            'subject'     => $request->subject,
93
            'slug'        => Str::slug($request->subject),
94
            'details'     => $request->details,
95
        ]);
96
97
98
        //  Move the files into a folder with the name of the tip_id
99
        if(session('new-file-upload') !== null)
100
        {
101
            $files = session('new-file-upload');
102
            foreach($files as $file)
103
            {
104
                $this->moveFile($file->file_id, $this->disk, $newTip->tip_id);
105
                TechTipFile::create([
106
                    'tip_id'  => $newTip->tip_id,
107
                    'file_id' => $file->file_id,
108
                ]);
109
            }
110
111
            session()->forget('new-file-upload');
112
        }
113
114
        Log::channel('user')->info('New Tech Tip - '.$request->subject.' was created by '.$request->user()->username);
115
        return redirect(route('tech-tips.show', $newTip->slug));
116
    }
117
118
    /**
119
     * Display the specified resource.
120
     *
121
     * @param  int  $id
122
     * @return \Illuminate\Http\Response
123
     */
124
    public function show($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

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

124
    public function show(/** @scrutinizer ignore-unused */ $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
125
    {
126
        //
127
        return 'show new tip';
128
    }
129
130
    /**
131
     * Show the form for editing the specified resource.
132
     *
133
     * @param  int  $id
134
     * @return \Illuminate\Http\Response
135
     */
136
    public function edit($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

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

136
    public function edit(/** @scrutinizer ignore-unused */ $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
137
    {
138
        //
139
    }
140
141
    /**
142
     * Update the specified resource in storage.
143
     *
144
     * @param  \Illuminate\Http\Request  $request
145
     * @param  int  $id
146
     * @return \Illuminate\Http\Response
147
     */
148
    public function update(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

148
    public function update(/** @scrutinizer ignore-unused */ Request $request, $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

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

148
    public function update(Request $request, /** @scrutinizer ignore-unused */ $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
149
    {
150
        //
151
    }
152
153
    /**
154
     * Remove the specified resource from storage.
155
     *
156
     * @param  int  $id
157
     * @return \Illuminate\Http\Response
158
     */
159
    public function destroy($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

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

159
    public function destroy(/** @scrutinizer ignore-unused */ $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
160
    {
161
        //
162
    }
163
}
164