PeopleController::destroy()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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