StudiesController   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 155
Duplicated Lines 43.23 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 67
loc 155
rs 10
c 2
b 1
f 0
ccs 0
cts 88
cp 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A index() 11 12 2
B store() 23 25 4
A show() 10 11 2
A edit() 0 6 1
B update() 23 25 4
A destroy() 0 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Scool\Inventory\Http\Controllers;
4
5
use Prettus\Validator\Contracts\ValidatorInterface;
6
use Prettus\Validator\Exceptions\ValidatorException;
7
use Scool\Inventory\Http\Requests\StudyCreateRequest;
8
use Scool\Inventory\Http\Requests\StudyUpdateRequest;
9
use Scool\Inventory\Repositories\StudyRepository;
10
use Scool\Inventory\Validators\StudyValidator;
11
12
/**
13
 * Class StudiesController.
14
 */
15
class StudiesController extends Controller
16
{
17
    /**
18
     * @var StudyRepository
19
     */
20
    protected $repository;
21
    /**
22
     * @var StudyValidator
23
     */
24
    protected $validator;
25
26
    public function __construct(StudyRepository $repository, StudyValidator $validator)
27
    {
28
        $this->repository = $repository;
29
        $this->validator = $validator;
30
    }
31
32
    /**
33
     * Display a listing of the resource.
34
     *
35
     * @return \Illuminate\Http\Response
36
     */
37 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...
38
    {
39
        $this->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
40
        $studies = $this->repository->all();
41
        if (request()->wantsJson()) {
42
            return response()->json([
43
                'data' => $studies,
44
            ]);
45
        }
46
47
        return view('studies.index', compact('studies'));
48
    }
49
50
    /**
51
     * Store a newly created resource in storage.
52
     *
53
     * @param StudyCreateRequest $request
54
     *
55
     * @return \Illuminate\Http\Response
56
     */
57 View Code Duplication
    public function store(StudyCreateRequest $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...
58
    {
59
        try {
60
            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE);
61
            $study = $this->repository->create($request->all());
62
            $response = [
63
                'message' => 'Study created.',
64
                'data'    => $study->toArray(),
65
            ];
66
            if ($request->wantsJson()) {
67
                return response()->json($response);
68
            }
69
70
            return redirect()->back()->with('message', $response['message']);
71
        } catch (ValidatorException $e) {
0 ignored issues
show
Bug introduced by
The class Prettus\Validator\Exceptions\ValidatorException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
72
            if ($request->wantsJson()) {
73
                return response()->json([
74
                    'error'   => true,
75
                    'message' => $e->getMessageBag(),
76
                ]);
77
            }
78
79
            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
80
        }
81
    }
82
83
    /**
84
     * Display the specified resource.
85
     *
86
     * @param int $id
87
     *
88
     * @return \Illuminate\Http\Response
89
     */
90 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...
91
    {
92
        $study = $this->repository->find($id);
93
        if (request()->wantsJson()) {
94
            return response()->json([
95
                'data' => $study,
96
            ]);
97
        }
98
99
        return view('studies.show', compact('study'));
100
    }
101
102
    /**
103
     * Show the form for editing the specified resource.
104
     *
105
     * @param int $id
106
     *
107
     * @return \Illuminate\Http\Response
108
     */
109
    public function edit($id)
110
    {
111
        $study = $this->repository->find($id);
112
113
        return view('studies.edit', compact('study'));
114
    }
115
116
    /**
117
     * Update the specified resource in storage.
118
     *
119
     * @param StudyUpdateRequest $request
120
     * @param string             $id
121
     *
122
     * @return Response
123
     */
124 View Code Duplication
    public function update(StudyUpdateRequest $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...
125
    {
126
        try {
127
            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_UPDATE);
128
            $study = $this->repository->update($id, $request->all());
129
            $response = [
130
                'message' => 'Study updated.',
131
                'data'    => $study->toArray(),
132
            ];
133
            if ($request->wantsJson()) {
134
                return response()->json($response);
135
            }
136
137
            return redirect()->back()->with('message', $response['message']);
138
        } catch (ValidatorException $e) {
0 ignored issues
show
Bug introduced by
The class Prettus\Validator\Exceptions\ValidatorException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
139
            if ($request->wantsJson()) {
140
                return response()->json([
141
                    'error'   => true,
142
                    'message' => $e->getMessageBag(),
143
                ]);
144
            }
145
146
            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
147
        }
148
    }
149
150
    /**
151
     * Remove the specified resource from storage.
152
     *
153
     * @param int $id
154
     *
155
     * @return \Illuminate\Http\Response
156
     */
157
    public function destroy($id)
158
    {
159
        $deleted = $this->repository->delete($id);
160
        if (request()->wantsJson()) {
161
            return response()->json([
162
                'message' => 'Study deleted.',
163
                'deleted' => $deleted,
164
            ]);
165
        }
166
167
        return redirect()->back()->with('message', 'Study deleted.');
168
    }
169
}
170