Completed
Push — master ( 49faca...44018c )
by David Martínez
05:46
created

LessonsController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 5
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 5
loc 5
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 5
cp 0
cc 1
eloc 3
nc 1
nop 2
crap 2
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 View Code Duplication
class LessonsController extends Controller
0 ignored issues
show
Duplication introduced by
This class 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...
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->all();
41
42
        if (request()->wantsJson()) {
43
44
            return response()->json([
45
                'data' => $lessons,
46
            ]);
47
        }
48
49
        return view('lessons.index', compact('lessons'));
50
    }
51
52
    /**
53
     * Show the form for creating the specified resource.
54
     *
55
     * @return \Illuminate\Http\Response
56
     */
57
    public function create()
58
    {
59
        return view('lessons.create');
60
    }
61
62
    /**
63
     * Store a newly created resource in storage.
64
     *
65
     * @param  LessonCreateRequest $request
66
     *
67
     * @return \Illuminate\Http\Response
68
     */
69
    public function store(LessonCreateRequest $request)
70
    {
71
72
        try {
73
74
            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE);
75
76
            $lesson = $this->repository->create($request->all());
77
78
            $response = [
79
                'message' => 'Lesson created.',
80
                'data'    => $lesson->toArray(),
81
            ];
82
83
            if ($request->wantsJson()) {
84
85
                return response()->json($response);
86
            }
87
88
            return redirect()->back()->with('message', $response['message']);
89
        } catch (ValidatorException $e) {
90
            if ($request->wantsJson()) {
91
                return response()->json([
92
                    'error'   => true,
93
                    'message' => $e->getMessageBag()
94
                ]);
95
            }
96
97
            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
98
        }
99
    }
100
101
102
    /**
103
     * Display the specified resource.
104
     *
105
     * @param  int $id
106
     *
107
     * @return \Illuminate\Http\Response
108
     */
109
    public function show($id)
110
    {
111
        $lesson = $this->repository->find($id);
112
113
        if (request()->wantsJson()) {
114
115
            return response()->json([
116
                'data' => $lesson,
117
            ]);
118
        }
119
120
        return view('lessons.show', compact('lesson'));
121
    }
122
123
124
    /**
125
     * Show the form for editing the specified resource.
126
     *
127
     * @param  int $id
128
     *
129
     * @return \Illuminate\Http\Response
130
     */
131
    public function edit($id)
132
    {
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
    public function update(LessonUpdateRequest $request, $id)
149
    {
150
151
        try {
152
153
            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_UPDATE);
154
155
            $lesson = $this->repository->update($request->all(), $id);
156
157
            $response = [
158
                'message' => 'Lesson updated.',
159
                'data'    => $lesson->toArray(),
160
            ];
161
162
            if ($request->wantsJson()) {
163
164
                return response()->json($response);
165
            }
166
167
            return redirect()->back()->with('message', $response['message']);
168
        } catch (ValidatorException $e) {
169
170
            if ($request->wantsJson()) {
171
172
                return response()->json([
173
                    'error'   => true,
174
                    'message' => $e->getMessageBag()
175
                ]);
176
            }
177
178
            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
179
        }
180
    }
181
182
183
    /**
184
     * Remove the specified resource from storage.
185
     *
186
     * @param  int $id
187
     *
188
     * @return \Illuminate\Http\Response
189
     */
190
    public function destroy($id)
191
    {
192
        $deleted = $this->repository->delete($id);
193
194
        if (request()->wantsJson()) {
195
196
            return response()->json([
197
                'message' => 'Lesson deleted.',
198
                'deleted' => $deleted,
199
            ]);
200
        }
201
202
        return redirect()->back()->with('message', 'Lesson deleted.');
203
    }
204
}
205