Code Duplication    Length = 175-183 lines in 3 locations

src/Http/Controllers/EnrollmentsController.php 1 location

@@ 16-190 (lines=175) @@
13
 * Class EnrollmentsController
14
 * @package App\Http\Controllers
15
 */
16
class EnrollmentsController extends Controller
17
{
18
19
    /**
20
     * @var EnrollmentRepository
21
     */
22
    protected $repository;
23
24
    /**
25
     * @var EnrollmentValidator
26
     */
27
    protected $validator;
28
29
    /**
30
     * EnrollmentsController constructor.
31
     * @param EnrollmentRepository $repository
32
     * @param EnrollmentValidator $validator
33
     */
34
    public function __construct(EnrollmentRepository $repository, EnrollmentValidator $validator)
35
    {
36
        $this->repository = $repository;
37
        $this->validator  = $validator;
38
    }
39
40
41
    /**
42
     * Display a listing of the resource.
43
     *
44
     * @return \Illuminate\Http\Response
45
     */
46
    public function index()
47
    {
48
        $this->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
49
        $enrollments = $this->repository->all();
50
        if (request()->wantsJson()) {
51
            return response()->json([
52
                'data' => $enrollments,
53
            ]);
54
        }
55
56
        return view('enrollments.index', compact('enrollments'));
57
    }
58
59
    /**
60
     * Store a newly created resource in storage.
61
     *
62
     * @param  EnrollmentCreateRequest $request
63
     *
64
     * @return \Illuminate\Http\Response
65
     */
66
    public function store(EnrollmentCreateRequest $request)
67
    {
68
        try {
69
            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE);
70
71
            $enrollment = $this->repository->create($request->all());
72
73
            $response = [
74
                'message' => 'Enrollment created.',
75
                'data'    => $enrollment->toArray(),
76
            ];
77
78
            if ($request->wantsJson()) {
79
                return response()->json($response);
80
            }
81
82
            return redirect()->back()->with('message', $response['message']);
83
        } catch (ValidatorException $e) {
84
            if ($request->wantsJson()) {
85
                return response()->json([
86
                    'error'   => true,
87
                    'message' => $e->getMessageBag()
88
                ]);
89
            }
90
91
            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
92
        }
93
    }
94
95
96
    /**
97
     * Display the specified resource.
98
     *
99
     * @param  int $id
100
     *
101
     * @return \Illuminate\Http\Response
102
     */
103
    public function show($id)
104
    {
105
        $enrollment = $this->repository->find($id);
106
107
        if (request()->wantsJson()) {
108
            return response()->json([
109
                'data' => $enrollment,
110
            ]);
111
        }
112
113
        return view('enrollments.show', compact('enrollment'));
114
    }
115
116
117
    /**
118
     * Show the form for editing the specified resource.
119
     *
120
     * @param  int $id
121
     *
122
     * @return \Illuminate\Http\Response
123
     */
124
    public function edit($id)
125
    {
126
        $enrollment = $this->repository->find($id);
127
128
        return view('enrollments.edit', compact('enrollment'));
129
    }
130
131
132
    /**
133
     * Update the specified resource in storage.
134
     *
135
     * @param  EnrollmentUpdateRequest $request
136
     * @param  string            $id
137
     *
138
     * @return Response
139
     */
140
    public function update(EnrollmentUpdateRequest $request, $id)
141
    {
142
        try {
143
            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_UPDATE);
144
145
            $enrollment = $this->repository->update($id, $request->all());
146
147
            $response = [
148
                'message' => 'Enrollment updated.',
149
                'data'    => $enrollment->toArray(),
150
            ];
151
152
            if ($request->wantsJson()) {
153
                return response()->json($response);
154
            }
155
156
            return redirect()->back()->with('message', $response['message']);
157
        } catch (ValidatorException $e) {
158
            if ($request->wantsJson()) {
159
                return response()->json([
160
                    'error'   => true,
161
                    'message' => $e->getMessageBag()
162
                ]);
163
            }
164
165
            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
166
        }
167
    }
168
169
170
    /**
171
     * Remove the specified resource from storage.
172
     *
173
     * @param  int $id
174
     *
175
     * @return \Illuminate\Http\Response
176
     */
177
    public function destroy($id)
178
    {
179
        $deleted = $this->repository->delete($id);
180
181
        if (request()->wantsJson()) {
182
            return response()->json([
183
                'message' => 'Enrollment deleted.',
184
                'deleted' => $deleted,
185
            ]);
186
        }
187
188
        return redirect()->back()->with('message', 'Enrollment deleted.');
189
    }
190
}
191
//mock del repository del create(crear nosalters lenrollment per fer el test).
192
//TODO: Fer testos de validació enrollment_mobile i todos_Backend.

src/Http/Controllers/ClassroomsController.php 1 location

@@ 16-198 (lines=183) @@
13
use Scool\EnrollmentMobile\Validators\ClassroomValidator;
14
15
16
class ClassroomsController extends Controller
17
{
18
19
    /**
20
     * @var ClassroomRepository
21
     */
22
    protected $repository;
23
24
    /**
25
     * @var ClassroomValidator
26
     */
27
    protected $validator;
28
29
    public function __construct(ClassroomRepository $repository, ClassroomValidator $validator)
30
    {
31
        $this->repository = $repository;
32
        $this->validator  = $validator;
33
    }
34
35
36
    /**
37
     * Display a listing of the resource.
38
     *
39
     * @return \Illuminate\Http\Response
40
     */
41
    public function index()
42
    {
43
        $this->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
44
        $classrooms = $this->repository->all();
45
46
        if (request()->wantsJson()) {
47
48
            return response()->json([
49
                'data' => $classrooms,
50
            ]);
51
        }
52
53
        return view('classrooms.index', compact('classrooms'));
54
    }
55
56
    /**
57
     * Store a newly created resource in storage.
58
     *
59
     * @param  ClassroomCreateRequest $request
60
     *
61
     * @return \Illuminate\Http\Response
62
     */
63
    public function store(ClassroomCreateRequest $request)
64
    {
65
66
        try {
67
68
            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE);
69
70
            $classroom = $this->repository->create($request->all());
71
72
            $response = [
73
                'message' => 'Classroom created.',
74
                'data'    => $classroom->toArray(),
75
            ];
76
77
            if ($request->wantsJson()) {
78
79
                return response()->json($response);
80
            }
81
82
            return redirect()->back()->with('message', $response['message']);
83
        } catch (ValidatorException $e) {
84
            if ($request->wantsJson()) {
85
                return response()->json([
86
                    'error'   => true,
87
                    'message' => $e->getMessageBag()
88
                ]);
89
            }
90
91
            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
92
        }
93
    }
94
95
96
    /**
97
     * Display the specified resource.
98
     *
99
     * @param  int $id
100
     *
101
     * @return \Illuminate\Http\Response
102
     */
103
    public function show($id)
104
    {
105
        $classroom = $this->repository->find($id);
106
107
        if (request()->wantsJson()) {
108
109
            return response()->json([
110
                'data' => $classroom,
111
            ]);
112
        }
113
114
        return view('classrooms.show', compact('classroom'));
115
    }
116
117
118
    /**
119
     * Show the form for editing the specified resource.
120
     *
121
     * @param  int $id
122
     *
123
     * @return \Illuminate\Http\Response
124
     */
125
    public function edit($id)
126
    {
127
128
        $classroom = $this->repository->find($id);
129
130
        return view('classrooms.edit', compact('classroom'));
131
    }
132
133
134
    /**
135
     * Update the specified resource in storage.
136
     *
137
     * @param  ClassroomUpdateRequest $request
138
     * @param  string            $id
139
     *
140
     * @return Response
141
     */
142
    public function update(ClassroomUpdateRequest $request, $id)
143
    {
144
145
        try {
146
147
            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_UPDATE);
148
149
            $classroom = $this->repository->update($id, $request->all());
150
151
            $response = [
152
                'message' => 'Classroom updated.',
153
                'data'    => $classroom->toArray(),
154
            ];
155
156
            if ($request->wantsJson()) {
157
158
                return response()->json($response);
159
            }
160
161
            return redirect()->back()->with('message', $response['message']);
162
        } catch (ValidatorException $e) {
163
164
            if ($request->wantsJson()) {
165
166
                return response()->json([
167
                    'error'   => true,
168
                    'message' => $e->getMessageBag()
169
                ]);
170
            }
171
172
            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
173
        }
174
    }
175
176
177
    /**
178
     * Remove the specified resource from storage.
179
     *
180
     * @param  int $id
181
     *
182
     * @return \Illuminate\Http\Response
183
     */
184
    public function destroy($id)
185
    {
186
        $deleted = $this->repository->delete($id);
187
188
        if (request()->wantsJson()) {
189
190
            return response()->json([
191
                'message' => 'Classroom deleted.',
192
                'deleted' => $deleted,
193
            ]);
194
        }
195
196
        return redirect()->back()->with('message', 'Classroom deleted.');
197
    }
198
}
199

src/Http/Controllers/CoursesController.php 1 location

@@ 16-198 (lines=183) @@
13
use Scool\EnrollmentMobile\Validators\CourseValidator;
14
15
16
class CoursesController extends Controller
17
{
18
19
    /**
20
     * @var CourseRepository
21
     */
22
    protected $repository;
23
24
    /**
25
     * @var CourseValidator
26
     */
27
    protected $validator;
28
29
    public function __construct(CourseRepository $repository, CourseValidator $validator)
30
    {
31
        $this->repository = $repository;
32
        $this->validator  = $validator;
33
    }
34
35
36
    /**
37
     * Display a listing of the resource.
38
     *
39
     * @return \Illuminate\Http\Response
40
     */
41
    public function index()
42
    {
43
        $this->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
44
        $courses = $this->repository->all();
45
46
        if (request()->wantsJson()) {
47
48
            return response()->json([
49
                'data' => $courses,
50
            ]);
51
        }
52
53
        return view('courses.index', compact('courses'));
54
    }
55
56
    /**
57
     * Store a newly created resource in storage.
58
     *
59
     * @param  CourseCreateRequest $request
60
     *
61
     * @return \Illuminate\Http\Response
62
     */
63
    public function store(CourseCreateRequest $request)
64
    {
65
66
        try {
67
68
            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE);
69
70
            $course = $this->repository->create($request->all());
71
72
            $response = [
73
                'message' => 'Course created.',
74
                'data'    => $course->toArray(),
75
            ];
76
77
            if ($request->wantsJson()) {
78
79
                return response()->json($response);
80
            }
81
82
            return redirect()->back()->with('message', $response['message']);
83
        } catch (ValidatorException $e) {
84
            if ($request->wantsJson()) {
85
                return response()->json([
86
                    'error'   => true,
87
                    'message' => $e->getMessageBag()
88
                ]);
89
            }
90
91
            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
92
        }
93
    }
94
95
96
    /**
97
     * Display the specified resource.
98
     *
99
     * @param  int $id
100
     *
101
     * @return \Illuminate\Http\Response
102
     */
103
    public function show($id)
104
    {
105
        $course = $this->repository->find($id);
106
107
        if (request()->wantsJson()) {
108
109
            return response()->json([
110
                'data' => $course,
111
            ]);
112
        }
113
114
        return view('courses.show', compact('course'));
115
    }
116
117
118
    /**
119
     * Show the form for editing the specified resource.
120
     *
121
     * @param  int $id
122
     *
123
     * @return \Illuminate\Http\Response
124
     */
125
    public function edit($id)
126
    {
127
128
        $course = $this->repository->find($id);
129
130
        return view('courses.edit', compact('course'));
131
    }
132
133
134
    /**
135
     * Update the specified resource in storage.
136
     *
137
     * @param  CourseUpdateRequest $request
138
     * @param  string            $id
139
     *
140
     * @return Response
141
     */
142
    public function update(CourseUpdateRequest $request, $id)
143
    {
144
145
        try {
146
147
            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_UPDATE);
148
149
            $course = $this->repository->update($id, $request->all());
150
151
            $response = [
152
                'message' => 'Course updated.',
153
                'data'    => $course->toArray(),
154
            ];
155
156
            if ($request->wantsJson()) {
157
158
                return response()->json($response);
159
            }
160
161
            return redirect()->back()->with('message', $response['message']);
162
        } catch (ValidatorException $e) {
163
164
            if ($request->wantsJson()) {
165
166
                return response()->json([
167
                    'error'   => true,
168
                    'message' => $e->getMessageBag()
169
                ]);
170
            }
171
172
            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
173
        }
174
    }
175
176
177
    /**
178
     * Remove the specified resource from storage.
179
     *
180
     * @param  int $id
181
     *
182
     * @return \Illuminate\Http\Response
183
     */
184
    public function destroy($id)
185
    {
186
        $deleted = $this->repository->delete($id);
187
188
        if (request()->wantsJson()) {
189
190
            return response()->json([
191
                'message' => 'Course deleted.',
192
                'deleted' => $deleted,
193
            ]);
194
        }
195
196
        return redirect()->back()->with('message', 'Course deleted.');
197
    }
198
}
199