LessonsController::show()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 12
rs 9.4285
c 1
b 0
f 1
ccs 0
cts 10
cp 0
cc 2
eloc 6
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Scool\Timetables\Http\Controllers;
4
5
use Prettus\Validator\Contracts\ValidatorInterface;
6
use Prettus\Validator\Exceptions\ValidatorException;
7
use Scool\Timetables\Http\Requests\LessonCreateRequest;
8
use Scool\Timetables\Http\Requests\LessonUpdateRequest;
9
use Scool\Timetables\Repositories\LessonRepository;
10
use Scool\Timetables\Validators\LessonValidator;
11
12
class LessonsController extends Controller
13
{
14
15
    /**
16
     * @var LessonRepository
17
     */
18
    protected $repository;
19
20
    /**
21
     * @var LessonValidator
22
     */
23
    protected $validator;
24
25
    public function __construct(LessonRepository $repository, LessonValidator $validator)
26
    {
27
        $this->repository = $repository;
28
        $this->validator  = $validator;
29
    }
30
31
32
    /**
33
     * Display a listing of the resource.
34
     *
35
     * @return \Illuminate\Http\Response
36
     */
37
    public function index()
38
    {
39
        $this->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
40
        $lessons = $this->repository->with(['users'])->paginate(2);
41
42
        if (request()->wantsJson()) {
43
            return response()->json([
44
                'data' => $lessons,
45
            ]);
46
        }
47
48
        return view('timetables::lessons.index', compact('lessons'));
49
    }
50
51
    /**
52
     * Show the form for creating the specified resource.
53
     *
54
     * @return \Illuminate\Http\Response
55
     */
56
    public function create()
57
    {
58
        return view('lessons.create');
59
    }
60
61
    /**
62
     * Store a newly created resource in storage.
63
     *
64
     * @param  LessonCreateRequest $request
65
     *
66
     * @return \Illuminate\Http\Response
67
     */
68 View Code Duplication
    public function store(LessonCreateRequest $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70
        try {
71
            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE);
72
73
            $lesson = $this->repository->create($request->all());
74
75
            if ($request->has('user_id')) {
76
                $user_id = $request->input('user_id');
77
                $user = \App\User::find($user_id);
78
                $lesson->users()->save($user);
79
            }
80
81
            $response = [
82
                'message' => 'Lesson created.',
83
                'data'    => $lesson->toArray(),
84
            ];
85
86
            if ($request->wantsJson()) {
87
                return response()->json($response);
88
            }
89
90
            return redirect()->back()->with('message', $response['message']);
91
        } catch (ValidatorException $e) {
92
            if ($request->wantsJson()) {
93
                return response()->json([
94
                    'error'   => true,
95
                    'message' => $e->getMessageBag()
96
                ]);
97
            }
98
99
            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
100
        }
101
    }
102
103
104
    /**
105
     * Display the specified resource.
106
     *
107
     * @param  int $id
108
     *
109
     * @return \Illuminate\Http\Response
110
     */
111
    public function show($id)
112
    {
113
        $lesson = $this->repository->find($id);
114
115
        if (request()->wantsJson()) {
116
            return response()->json([
117
                'data' => $lesson,
118
            ]);
119
        }
120
121
        return view('lessons.show', compact('lesson'));
122
    }
123
124
125
    /**
126
     * Show the form for editing the specified resource.
127
     *
128
     * @param  int $id
129
     *
130
     * @return \Illuminate\Http\Response
131
     */
132
    public function edit($id)
133
    {
134
        $lesson = $this->repository->find($id);
135
136
        return view('lessons.edit', compact('lesson'));
137
    }
138
139
140
    /**
141
     * Update the specified resource in storage.
142
     *
143
     * @param  LessonUpdateRequest $request
144
     * @param  string            $id
145
     *
146
     * @return Response
147
     */
148 View Code Duplication
    public function update(LessonUpdateRequest $request, $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
149
    {
150
        try {
151
            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_UPDATE);
152
153
            $lesson = $this->repository->update($request->all(), $id);
154
155
            if ($request->has('user_id')) {
156
                $user_id = $request->input('user_id');
157
                $user = \App\User::find($user_id);
158
                $lesson->users()->save($user);
159
            }
160
161
            $response = [
162
                'message' => 'Lesson updated.',
163
                'data'    => $lesson->toArray(),
164
            ];
165
166
            if ($request->wantsJson()) {
167
                return response()->json($response);
168
            }
169
170
            return redirect()->back()->with('message', $response['message']);
171
        } catch (ValidatorException $e) {
172
            if ($request->wantsJson()) {
173
                return response()->json([
174
                    'error'   => true,
175
                    'message' => $e->getMessageBag()
176
                ]);
177
            }
178
179
            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
180
        }
181
    }
182
183
184
    /**
185
     * Remove the specified resource from storage.
186
     *
187
     * @param  int $id
188
     *
189
     * @return \Illuminate\Http\Response
190
     */
191
    public function destroy($id)
192
    {
193
        $lesson = $this->repository->find($id);
194
        $lesson->users()->detach();
195
        $deleted = $this->repository->delete($id);
196
197
198
        if (request()->wantsJson()) {
199
            return response()->json([
200
                'message' => 'Lesson deleted.',
201
                'deleted' => $deleted,
202
            ]);
203
        }
204
205
        return redirect()->back()->with('message', 'Lesson deleted.');
206
    }
207
}
208