EnrollmentsController::update()   B
last analyzed

Complexity

Conditions 4
Paths 14

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 28
ccs 0
cts 23
cp 0
rs 8.5806
cc 4
eloc 16
nc 14
nop 2
crap 20
1
<?php
2
3
namespace Scool\EnrollmentMobile\Http\Controllers;
4
5
use Prettus\Validator\Contracts\ValidatorInterface;
6
use Prettus\Validator\Exceptions\ValidatorException;
7
use Scool\EnrollmentMobile\Http\Requests\EnrollmentBrowseRequest;
8
use Scool\EnrollmentMobile\Http\Requests\EnrollmentCreateRequest;
9
use Scool\EnrollmentMobile\Http\Requests\EnrollmentDeleteRequest;
10
use Scool\EnrollmentMobile\Http\Requests\EnrollmentUpdateRequest;
11
use Scool\EnrollmentMobile\Repositories\EnrollmentRepository;
12
use Scool\EnrollmentMobile\Validators\EnrollmentValidator;
13
14
/**
15
 * Class EnrollmentsController
16
 * @package App\Http\Controllers
17
 */
18
class EnrollmentsController extends Controller
19
{
20
21
    /**
22
     * @var EnrollmentRepository
23
     */
24
    protected $repository;
25
26
    /**
27
     * @var EnrollmentValidator
28
     */
29
    protected $validator;
30
31
    /**
32
     * EnrollmentsController constructor.
33
     * @param EnrollmentRepository $repository
34
     * @param EnrollmentValidator $validator
35
     */
36
    public function __construct(EnrollmentRepository $repository, EnrollmentValidator $validator)
37
    {
38
        $this->repository = $repository;
39
        $this->validator  = $validator;
40
    }
41
42
43
    /**
44
     * Display a listing of the resource.
45
     *
46
     * @return \Illuminate\Http\Response
47
     */
48
    public function index(EnrollmentBrowseRequest $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    {
50
        $this->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
51
        $enrollments = $this->repository->with(['classroom', 'course', 'study'])->all();
52
        if (request()->wantsJson()) {
53
            return response()->json([
54
                'data' => $enrollments,
55
            ]);
56
        }
57
58
        return view('enrollment_mobile::enrollments.index', compact('enrollments'));
59
    }
60
61
    /**
62
     * Store a newly created resource in storage.
63
     *
64
     * @param  EnrollmentCreateRequest $request
65
     *
66
     * @return \Illuminate\Http\Response
67
     */
68
    public function store(EnrollmentCreateRequest $request)
69
    {
70
        try {
71
            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE);
72
73
            $enrollment = $this->repository->create($request->all());
74
75
            $response = [
76
                'message' => 'Enrollment created.',
77
                'data'    => $enrollment->toArray(),
78
            ];
79
80
            if ($request->wantsJson()) {
81
                return response()->json($response);
82
            }
83
84
            return redirect()->back()->with('message', $response['message']);
85
        } catch (ValidatorException $e) {
86
            if ($request->wantsJson()) {
87
                return response()->json([
88
                    'error'   => true,
89
                    'message' => $e->getMessageBag()
90
                ]);
91
            }
92
93
            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
94
        }
95
    }
96
97
98
    /**
99
     * Display the specified resource.
100
     *
101
     * @param  int $id
102
     *
103
     * @return \Illuminate\Http\Response
104
     */
105
    public function show($id)
106
    {
107
        //return Auth::user()->can('view enrollment');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
108
        $enrollment = $this->repository->find($id);
109
110
        if (request()->wantsJson()) {
111
            return response()->json([
112
                'data' => $enrollment,
113
            ]);
114
        }
115
116
        return view('enrollment_mobile::enrollments.show', compact('enrollment'));
117
    }
118
119
120
    /**
121
     * Show the form for editing the specified resource.
122
     *
123
     * @param  int $id
124
     *
125
     * @return \Illuminate\Http\Response
126
     */
127
    public function edit($id)
128
    {
129
        $enrollment = $this->repository->find($id);
130
131
        return view('enrollment_mobile::enrollments.edit', compact('enrollment'));
132
    }
133
134
135
    /**
136
     * Update the specified resource in storage.
137
     *
138
     * @param  EnrollmentUpdateRequest $request
139
     * @param  $id string
140
     *
141
     * @return Response
142
     */
143
    public function update(EnrollmentUpdateRequest $request, $id)
144
    {
145
        try {
146
            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_UPDATE);
147
148
            $enrollment = $this->repository->update($request->all(), $id);
149
150
            $response = [
151
                'message' => 'Enrollment updated.',
152
                'data'    => $enrollment->toArray(),
153
            ];
154
155
            if ($request->wantsJson()) {
156
                return response()->json($response);
157
            }
158
159
            return redirect()->back()->with('message', $response['message']);
160
        } catch (ValidatorException $e) {
161
            if ($request->wantsJson()) {
162
                return response()->json([
163
                    'error'   => true,
164
                    'message' => $e->getMessageBag()
165
                ]);
166
            }
167
168
            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
169
        }
170
    }
171
172
173
    /**
174
     * Remove the specified resource from storage.
175
     *
176
     * @param  int $id
177
     *
178
     * @return \Illuminate\Http\Response
179
     */
180
    public function destroy(EnrollmentDeleteRequest $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
181
    {
182
        $deleted = $this->repository->delete($id);
183
184
        if (request()->wantsJson()) {
185
            return response()->json([
186
                'message' => 'Enrollment deleted.',
187
                'deleted' => $deleted,
188
            ]);
189
        }
190
191
        return redirect()->back()->with('message', 'Enrollment deleted.');
192
    }
193
}
194
//TODO: Fer testos de validació enrollment_mobile i todos_Backend. mock del repository del create(crear nosalters lenrollment per fer el test).
195