Completed
Push — master ( 011068...5fe610 )
by Manel
02:30
created

CyclesController::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 Scool\EnrollmentMobile\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
7
use Scool\EnrollmentMobile\Http\Requests;
8
use Prettus\Validator\Contracts\ValidatorInterface;
9
use Prettus\Validator\Exceptions\ValidatorException;
10
use Scool\EnrollmentMobile\Http\Requests\CycleCreateRequest;
11
use Scool\EnrollmentMobile\Http\Requests\CycleUpdateRequest;
12
use Scool\EnrollmentMobile\Repositories\CycleRepository;
13
use Scool\EnrollmentMobile\Validators\CycleValidator;
14
15
16 View Code Duplication
class CyclesController extends Controller
0 ignored issues
show
Duplication introduced by
This class 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...
17
{
18
19
    /**
20
     * @var CycleRepository
21
     */
22
    protected $repository;
23
24
    /**
25
     * @var CycleValidator
26
     */
27
    protected $validator;
28
29
    public function __construct(CycleRepository $repository, CycleValidator $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
        $cycles = $this->repository->all();
45
46
        if (request()->wantsJson()) {
47
48
            return response()->json([
49
                'data' => $cycles,
50
            ]);
51
        }
52
53
        return view('cycles.index', compact('cycles'));
54
    }
55
56
    /**
57
     * Store a newly created resource in storage.
58
     *
59
     * @param  CycleCreateRequest $request
60
     *
61
     * @return \Illuminate\Http\Response
62
     */
63
    public function store(CycleCreateRequest $request)
64
    {
65
66
        try {
67
68
            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE);
69
70
            $cycle = $this->repository->create($request->all());
71
72
            $response = [
73
                'message' => 'Cycle created.',
74
                'data'    => $cycle->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
        $cycle = $this->repository->find($id);
106
107
        if (request()->wantsJson()) {
108
109
            return response()->json([
110
                'data' => $cycle,
111
            ]);
112
        }
113
114
        return view('cycles.show', compact('cycle'));
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
        $cycle = $this->repository->find($id);
129
130
        return view('cycles.edit', compact('cycle'));
131
    }
132
133
134
    /**
135
     * Update the specified resource in storage.
136
     *
137
     * @param  CycleUpdateRequest $request
138
     * @param  string            $id
139
     *
140
     * @return Response
141
     */
142
    public function update(CycleUpdateRequest $request, $id)
143
    {
144
145
        try {
146
147
            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_UPDATE);
148
149
            $cycle = $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' => 'Cycle updated.',
153
                'data'    => $cycle->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' => 'Cycle deleted.',
192
                'deleted' => $deleted,
193
            ]);
194
        }
195
196
        return redirect()->back()->with('message', 'Cycle deleted.');
197
    }
198
}
199