Completed
Push — master ( c854c2...bcd47a )
by David Martínez
02:02
created

AttendancesController::index()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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