Completed
Push — master ( 4e34cd...643d5b )
by Manel
13:04
created

PeopleController::store()   B

Complexity

Conditions 4
Paths 14

Size

Total Lines 31
Code Lines 16

Duplication

Lines 31
Ratio 100 %

Importance

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