Completed
Push — master ( 7ec402...2e144b )
by Manel
12:30
created

EnrollmentsController::destroy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Scool\EnrollmentMobile\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 Scool\EnrollmentMobile\Http\Requests\EnrollmentCreateRequest;
11
use Scool\EnrollmentMobile\Http\Requests\EnrollmentUpdateRequest;
12
use Scool\EnrollmentMobile\Repositories\EnrollmentRepository;
13
use Scool\EnrollmentMobile\Validators\EnrollmentValidator;
14
15
/**
16
 * Class EnrollmentsController
17
 * @package App\Http\Controllers
18
 */
19
class EnrollmentsController extends Controller
20
{
21
22
    /**
23
     * @var EnrollmentRepository
24
     */
25
    protected $repository;
26
27
    /**
28
     * @var EnrollmentValidator
29
     */
30
    protected $validator;
31
32
    /**
33
     * EnrollmentsController constructor.
34
     * @param EnrollmentRepository $repository
35
     * @param EnrollmentValidator $validator
36
     */
37
    public function __construct(EnrollmentRepository $repository, EnrollmentValidator $validator)
38
    {
39
        $this->repository = $repository;
40
        $this->validator  = $validator;
41
    }
42
43
44
    /**
45
     * Display a listing of the resource.
46
     *
47
     * @return \Illuminate\Http\Response
48
     */
49 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...
50
    {
51
        $this->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
52
        $enrollments = $this->repository->all();
53
54
        if (request()->wantsJson()) {
55
            return response()->json([
56
                'data' => $enrollments,
57
            ]);
58
        }
59
60
        return view('enrollments.index', compact('enrollments'));
61
    }
62
63
    /**
64
     * Store a newly created resource in storage.
65
     *
66
     * @param  EnrollmentCreateRequest $request
67
     *
68
     * @return \Illuminate\Http\Response
69
     */
70 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...
71
    {
72
        try {
73
            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE);
74
75
            $enrollment = $this->repository->create($request->all());
76
77
            $response = [
78
                'message' => 'Enrollment created.',
79
                'data'    => $enrollment->toArray(),
80
            ];
81
82
            if ($request->wantsJson()) {
83
                return response()->json($response);
84
            }
85
86
            return redirect()->back()->with('message', $response['message']);
87
        } catch (ValidatorException $e) {
88
            if ($request->wantsJson()) {
89
                return response()->json([
90
                    'error'   => true,
91
                    'message' => $e->getMessageBag()
92
                ]);
93
            }
94
95
            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
96
        }
97
    }
98
99
100
    /**
101
     * Display the specified resource.
102
     *
103
     * @param  int $id
104
     *
105
     * @return \Illuminate\Http\Response
106
     */
107 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...
108
    {
109
        $enrollment = $this->repository->find($id);
110
111
        if (request()->wantsJson()) {
112
            return response()->json([
113
                'data' => $enrollment,
114
            ]);
115
        }
116
117
        return view('enrollments.show', compact('enrollment'));
118
    }
119
120
121
    /**
122
     * Show the form for editing the specified resource.
123
     *
124
     * @param  int $id
125
     *
126
     * @return \Illuminate\Http\Response
127
     */
128
    public function edit($id)
129
    {
130
        $enrollment = $this->repository->find($id);
131
132
        return view('enrollments.edit', compact('enrollment'));
133
    }
134
135
136
    /**
137
     * Update the specified resource in storage.
138
     *
139
     * @param  EnrollmentUpdateRequest $request
140
     * @param  string            $id
141
     *
142
     * @return Response
143
     */
144 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...
145
    {
146
        try {
147
            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_UPDATE);
148
149
            $enrollment = $this->repository->update($id, $request->all());
0 ignored issues
show
Documentation introduced by
$id is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
150
151
            $response = [
152
                'message' => 'Enrollment updated.',
153
                'data'    => $enrollment->toArray(),
154
            ];
155
156
            if ($request->wantsJson()) {
157
                return response()->json($response);
158
            }
159
160
            return redirect()->back()->with('message', $response['message']);
161
        } catch (ValidatorException $e) {
162
            if ($request->wantsJson()) {
163
                return response()->json([
164
                    'error'   => true,
165
                    'message' => $e->getMessageBag()
166
                ]);
167
            }
168
169
            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
170
        }
171
    }
172
173
174
    /**
175
     * Remove the specified resource from storage.
176
     *
177
     * @param  int $id
178
     *
179
     * @return \Illuminate\Http\Response
180
     */
181
    public function destroy($id)
182
    {
183
        $deleted = $this->repository->delete($id);
184
185
        if (request()->wantsJson()) {
186
            return response()->json([
187
                'message' => 'Enrollment deleted.',
188
                'deleted' => $deleted,
189
            ]);
190
        }
191
192
        return redirect()->back()->with('message', 'Enrollment deleted.');
193
    }
194
}
195