Completed
Push — master ( 81034a...b50ff0 )
by Faiq
20s queued 11s
created

ThreadAskController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 123
rs 10
c 0
b 0
f 0
wmc 13

10 Methods

Rating   Name   Duplication   Size   Complexity  
A currentUser() 0 2 1
A index() 0 4 1
A update() 0 2 1
A create() 0 4 1
A edit() 0 2 1
A show() 0 2 1
A destroy() 0 2 1
A store() 0 21 3
A __construct() 0 4 1
A addTopic() 0 7 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Support\Facades\Auth;
6
use Illuminate\Http\Request;
7
use App\Thread;
8
use App\ThreadTopic;
9
10
class ThreadAskController extends Controller
11
{
12
    public function __construct()
13
    {
14
        $this->middleware('auth', ['except' => [
15
            'index', 'show'
16
        ]]);
17
    }
18
19
20
    /**
21
     * Display a listing of the resource.
22
     *
23
     * @return \Illuminate\Http\Response
24
     */
25
    public function index()
26
    {
27
        $threads = Thread::orderBy('created_at', 'asc')->get();
28
        return view('ask-index')->with('threads', $threads);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('ask-index')...th('threads', $threads) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
29
    }
30
31
    /**
32
     * Show the form for creating a new resource.
33
     *
34
     * @return \Illuminate\Http\Response
35
     */
36
    public function create()
37
    {
38
        $threads = Thread::orderBy('created_at', 'asc')->get();
39
        return view('ask-form')->with('threads', $threads);;
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('ask-form')-...th('threads', $threads) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
40
    }
41
42
    /**
43
     * Store a newly created resource in storage.
44
     *
45
     * @param Request $request
46
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
47
     * @throws \Illuminate\Validation\ValidationException
48
     */
49
    public function store(Request $request)
50
    {
51
        $this->validate($request, [
52
            'topic' => 'required|min:10',
53
            'question' => 'required|min:100'
54
        ]);
55
56
        $topic = $this->addTopic($request->input('topic'));
57
58
        if($topic != null) {
59
            $thread = new Thread;
60
            $thread->user_id = $this->currentUser()->id;
61
            $thread->id_topic = $topic->id;
62
            $thread->question = $request->input('question');
63
64
            if($thread->save()) {
65
                return redirect(route('user.thread.index'))->with('success', 'Pertanyaan dikirim !');
66
            }
67
            return redirect()->back()->with('failed', 'Gagal mengirim pertanyaan, silahkan coba lagi nanti.');
68
        }
69
        return redirect()->back()->with('failed', 'Gagal mengirim pertanyaan, silahkan coba lagi nanti.');
70
    }
71
72
    /**
73
     * Display the specified resource.
74
     *
75
     * @param  int  $id
76
     * @return \Illuminate\Http\Response
77
     */
78
    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

78
    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...
79
    {
80
        //
81
    }
82
83
    /**
84
     * Show the form for editing the specified resource.
85
     *
86
     * @param  int  $id
87
     * @return \Illuminate\Http\Response
88
     */
89
    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

89
    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...
90
    {
91
        //
92
    }
93
94
    /**
95
     * Update the specified resource in storage.
96
     *
97
     * @param  \Illuminate\Http\Request  $request
98
     * @param  int  $id
99
     * @return \Illuminate\Http\Response
100
     */
101
    public function update(Request $request, $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

101
    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...
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

101
    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...
102
    {
103
        //
104
    }
105
106
    /**
107
     * Remove the specified resource from storage.
108
     *
109
     * @param  int  $id
110
     * @return \Illuminate\Http\Response
111
     */
112
    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

112
    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...
113
    {
114
        //
115
    }
116
117
118
    private function currentUser() {
119
        return Auth::guard('web')->user();
120
    }
121
122
    /**
123
     * @param string $topic
124
     * @return ThreadTopic|null
125
     */
126
    private function addTopic(string $topic) {
127
        $new = new ThreadTopic;
128
        $new->topic_name = $topic;
129
        if($new->save()) {
130
            return $new;
131
        }
132
        return null;
133
    }
134
}
135