ModulesController::store()   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 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 28
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\ModuleCreateRequest;
11
use Scool\EnrollmentMobile\Http\Requests\ModuleUpdateRequest;
12
use Scool\EnrollmentMobile\Repositories\ModuleRepository;
13
use Scool\EnrollmentMobile\Validators\ModuleValidator;
14
15
class ModulesController extends Controller
16
{
17
18
    /**
19
     * @var ModuleRepository
20
     */
21
    protected $repository;
22
23
    /**
24
     * @var ModuleValidator
25
     */
26
    protected $validator;
27
28
    public function __construct(ModuleRepository $repository, ModuleValidator $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
    public function index()
41
    {
42
        $this->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
43
        $modules = $this->repository->all();
44
45
        if (request()->wantsJson()) {
46
            return response()->json([
47
                'data' => $modules,
48
            ]);
49
        }
50
51
        return view('enrollment_mobile::modules.index', compact('modules'));
52
    }
53
54
    /**
55
     * Store a newly created resource in storage.
56
     *
57
     * @param  ModuleCreateRequest $request
58
     *
59
     * @return \Illuminate\Http\Response
60
     */
61
//    public function store(ModuleCreateRequest $request)
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% 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...
62
    public function store(Request $request)
63
    {
64
        try {
65
            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE);
66
67
            $module = $this->repository->create($request->all());
68
69
            $response = [
70
                'message' => 'Module created.',
71
                'data'    => $module->toArray(),
72
            ];
73
74
            if ($request->wantsJson()) {
75
                return response()->json($response);
76
            }
77
78
            return redirect()->back()->with('message', $response['message']);
79
        } catch (ValidatorException $e) {
80
            if ($request->wantsJson()) {
81
                return response()->json([
82
                    'error'   => true,
83
                    'message' => $e->getMessageBag()
84
                ]);
85
            }
86
87
            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
88
        }
89
    }
90
91
92
    /**
93
     * Display the specified resource.
94
     *
95
     * @param  int $id
96
     *
97
     * @return \Illuminate\Http\Response
98
     */
99
    public function show($id)
100
    {
101
        $module = $this->repository->find($id);
102
103
        if (request()->wantsJson()) {
104
            return response()->json([
105
                'data' => $module,
106
            ]);
107
        }
108
109
        return view('modules.show', compact('module'));
110
    }
111
112
113
    /**
114
     * Show the form for editing the specified resource.
115
     *
116
     * @param  int $id
117
     *
118
     * @return \Illuminate\Http\Response
119
     */
120
    public function edit($id)
121
    {
122
        $module = $this->repository->find($id);
123
124
        return view('modules.edit', compact('module'));
125
    }
126
127
128
    /**
129
     * Update the specified resource in storage.
130
     *
131
     * @param  ModuleUpdateRequest $request
132
     * @param  string            $id
133
     *
134
     * @return Response
135
     */
136
//    public function update(ModuleUpdateRequest $request, $id)
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
137
    public function update(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
138
    {
139
        try {
140
            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_UPDATE);
141
142
//            $module = $this->repository->update($id, $request->all());
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
143
144
            $response = [
145
                'message' => 'Module updated.',
146
//                'data'    => $module->toArray(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
147
            ];
148
149
            if ($request->wantsJson()) {
150
                return response()->json($response);
151
            }
152
153
            return redirect()->back()->with('message', $response['message']);
154
        } catch (ValidatorException $e) {
155
            if ($request->wantsJson()) {
156
                return response()->json([
157
                    'error'   => true,
158
                    'message' => $e->getMessageBag()
159
                ]);
160
            }
161
162
            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
163
        }
164
    }
165
166
167
    /**
168
     * Remove the specified resource from storage.
169
     *
170
     * @param  int $id
171
     *
172
     * @return \Illuminate\Http\Response
173
     */
174
    public function destroy($id)
175
    {
176
        $deleted = $this->repository->delete($id);
177
178
        if (request()->wantsJson()) {
179
            return response()->json([
180
                'message' => 'Module deleted.',
181
                'deleted' => $deleted,
182
            ]);
183
        }
184
185
        return redirect()->back()->with('message', 'Module deleted.');
186
    }
187
}
188