Completed
Push — master ( 0151d5...060799 )
by Manel
02:48
created

EnrollmentsController::store()   B

Complexity

Conditions 4
Paths 14

Size

Total Lines 31
Code Lines 16

Duplication

Lines 31
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 31
loc 31
ccs 0
cts 23
cp 0
rs 8.5806
cc 4
eloc 16
nc 14
nop 1
crap 20
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
7
use App\Http\Requests;
8
use Prettus\Validator\Contracts\ValidatorInterface;
9
use Prettus\Validator\Exceptions\ValidatorException;
10
use App\Http\Requests\EnrollmentCreateRequest;
11
use App\Http\Requests\EnrollmentUpdateRequest;
12
use App\Repositories\EnrollmentRepository;
13
use App\Validators\EnrollmentValidator;
14
15
16
/**
17
 * Class EnrollmentsController
18
 * @package App\Http\Controllers
19
 */
20
class EnrollmentsController extends Controller
21
{
22
23
    /**
24
     * @var EnrollmentRepository
25
     */
26
    protected $repository;
27
28
    /**
29
     * @var EnrollmentValidator
30
     */
31
    protected $validator;
32
33
    /**
34
     * EnrollmentsController constructor.
35
     * @param EnrollmentRepository $repository
36
     * @param EnrollmentValidator $validator
37
     */
38
    public function __construct(EnrollmentRepository $repository, EnrollmentValidator $validator)
39
    {
40
        $this->repository = $repository;
41
        $this->validator  = $validator;
42
    }
43
44
45
    /**
46
     * Display a listing of the resource.
47
     *
48
     * @return \Illuminate\Http\Response
49
     */
50 View Code Duplication
    public function index()
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...
51
    {
52
        $this->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
53
        $enrollments = $this->repository->all();
54
55
        if (request()->wantsJson()) {
56
57
            return response()->json([
58
                'data' => $enrollments,
59
            ]);
60
        }
61
62
        return view('enrollments.index', compact('enrollments'));
63
    }
64
65
    /**
66
     * Store a newly created resource in storage.
67
     *
68
     * @param  EnrollmentCreateRequest $request
69
     *
70
     * @return \Illuminate\Http\Response
71
     */
72 View Code Duplication
    public function store(EnrollmentCreateRequest $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...
73
    {
74
75
        try {
76
77
            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE);
78
79
            $enrollment = $this->repository->create($request->all());
80
81
            $response = [
82
                'message' => 'Enrollment created.',
83
                'data'    => $enrollment->toArray(),
84
            ];
85
86
            if ($request->wantsJson()) {
87
88
                return response()->json($response);
89
            }
90
91
            return redirect()->back()->with('message', $response['message']);
92
        } catch (ValidatorException $e) {
93
            if ($request->wantsJson()) {
94
                return response()->json([
95
                    'error'   => true,
96
                    'message' => $e->getMessageBag()
97
                ]);
98
            }
99
100
            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
101
        }
102
    }
103
104
105
    /**
106
     * Display the specified resource.
107
     *
108
     * @param  int $id
109
     *
110
     * @return \Illuminate\Http\Response
111
     */
112 View Code Duplication
    public function show($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...
113
    {
114
        $enrollment = $this->repository->find($id);
115
116
        if (request()->wantsJson()) {
117
118
            return response()->json([
119
                'data' => $enrollment,
120
            ]);
121
        }
122
123
        return view('enrollments.show', compact('enrollment'));
124
    }
125
126
127
    /**
128
     * Show the form for editing the specified resource.
129
     *
130
     * @param  int $id
131
     *
132
     * @return \Illuminate\Http\Response
133
     */
134
    public function edit($id)
135
    {
136
137
        $enrollment = $this->repository->find($id);
138
139
        return view('enrollments.edit', compact('enrollment'));
140
    }
141
142
143
    /**
144
     * Update the specified resource in storage.
145
     *
146
     * @param  EnrollmentUpdateRequest $request
147
     * @param  string            $id
148
     *
149
     * @return Response
150
     */
151 View Code Duplication
    public function update(EnrollmentUpdateRequest $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...
152
    {
153
154
        try {
155
156
            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_UPDATE);
157
158
            $enrollment = $this->repository->update($id, $request->all());
159
160
            $response = [
161
                'message' => 'Enrollment updated.',
162
                'data'    => $enrollment->toArray(),
163
            ];
164
165
            if ($request->wantsJson()) {
166
167
                return response()->json($response);
168
            }
169
170
            return redirect()->back()->with('message', $response['message']);
171
        } catch (ValidatorException $e) {
172
173
            if ($request->wantsJson()) {
174
175
                return response()->json([
176
                    'error'   => true,
177
                    'message' => $e->getMessageBag()
178
                ]);
179
            }
180
181
            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
182
        }
183
    }
184
185
186
    /**
187
     * Remove the specified resource from storage.
188
     *
189
     * @param  int $id
190
     *
191
     * @return \Illuminate\Http\Response
192
     */
193
    public function destroy($id)
194
    {
195
        $deleted = $this->repository->delete($id);
196
197
        if (request()->wantsJson()) {
198
199
            return response()->json([
200
                'message' => 'Enrollment deleted.',
201
                'deleted' => $deleted,
202
            ]);
203
        }
204
205
        return redirect()->back()->with('message', 'Enrollment deleted.');
206
    }
207
}
208