Completed
Push — master ( 7d04c8...2a4aa6 )
by Jan
10:55
created

CommentController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 203
Duplicated Lines 33 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 5
dl 67
loc 203
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B associateRelationships() 0 83 5
A approve() 34 34 2
A spam() 33 33 2
A __construct() 0 8 1

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
 * Comment module controller
4
 * 
5
 * Controller for module Comment
6
 * 
7
 * @category Controller
8
 * @subpackage Admin
9
 * @package Olapus
10
 * @author Jan Drda <[email protected]>
11
 * @copyright Jan Drda
12
 * @license https://opensource.org/licenses/MIT MIT
13
 */
14
15
namespace App\Http\Controllers\Admin;
16
17
use Illuminate\Http\Request;
18
use Illuminate\Support\Facades\Validator;
19
use App\Comment;
20
use Illuminate\Support\Facades\Redirect;
21
22
class CommentController extends AdminModuleController {
23
    
24
    /**
25
     * Constructor
26
     * 
27
     * @param Request $request
28
     */
29
    public function __construct(Request $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
30
        parent::__construct();
31
        
32
        /**
33
         * Add lookup tables
34
         */
35
        $this->middleware('add.lookup.tables:CommentStatus', ['only' => ['index','create','edit']]);
36
    }
37
38
    /**
39
     * Validation rules
40
     * 
41
     * @var array
42
     */
43
    protected $arValidationArray = [
44
        'name' => 'max:255',
45
        'headline' => 'max:255|required',
46
        'text' => 'max:1000',
47
        'url' => 'max:255',
48
        'email' => 'max:255',
49
        'rating' => 'integer|max:1|min:-1|required'
50
    ];
51
52
    /**
53
     * Associate relationships to other table
54
     * 
55
     * @param object $object
56
     * @param Request $request
57
     */
58
    public function associateRelationships($object, Request $request) {
59
        
60
        /**
61
         * Validate commentstatus ID, if failed set to default
62
         */
63
        $validator = Validator::make($request->all(), [
64
            'commentstatus_id' => 'required|integer|min:1|exists:commentstatus,id',
65
        ]);
66
67
        /**
68
         * Validator fails - try to set the valid comment id
69
         *
70
         */
71
        if ($validator->fails()) {
72
            
73
            /**
74
             * Auto approve ?
75
             */
76
            if(env('AUTO_APPROVE_COMMENTS', 0) == 1){
77
                
78
                $object->commentstatuses()->associate(2);
79
            }
80
            
81
            /**
82
             * Manual approve
83
             */
84
            else{
85
                $object->commentstatuses()->associate(1);
86
            }
87
            
88
        }
89
        else{
90
91
            $object->commentstatuses()->associate($request->input('commentstatus_id'));
92
        }
93
94
        /**
95
         * Validate article ID, if failed set to actual authorized article
96
         */
97
        $validator = Validator::make($request->all(), [
98
                    'article_id' => 'required|integer|min:1|exists:article,id',
99
        ]);
100
        
101
        /**
102
         * Validator fails - nothing to do
103
         */
104
        if ($validator->fails()) {
105
            
106
        } 
107
        
108
        /**
109
         * Validator OK - save it
110
         */
111
        else {
112
113
            $object->articles()->associate($request->input('article_id'));
114
        }
115
        
116
        /**
117
         * Validate page ID, if failed set to actual authorized page
118
         */
119
        $validator = Validator::make($request->all(), [
120
                    'page_id' => 'required|integer|min:1|exists:page,id',
121
        ]);
122
        
123
        /**
124
         * Validator fails - nothing to do
125
         */
126
        if ($validator->fails()) {
127
128
            // nothing
129
            
130
        } 
131
        
132
        /**
133
         * Validator OK - save it
134
         */
135
        else {
136
137
            $object->pages()->associate($request->input('page_id'));
138
        }
139
140
    }
141
    
142
    /**
143
     * Approve comment
144
     * 
145
     * @param integer $id
146
     * @param Request $request
147
     * @return Response
148
     */
149 View Code Duplication
    public function approve($id, Request $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...
150
        
151
        /**
152
         * Add ID to request
153
         */
154
        $request->merge(array('id' => $id));
155
        
156
        /**
157
         * Validate comment ID
158
         */
159
        $validator = Validator::make($request->all(), [
160
                    'id' => 'required|integer|min:1|exists:comment,id',
161
        ]);
162
        
163
        /**
164
         * Failed - redirect to index
165
         */
166
        if ($validator->fails()) {
167
             
168
        } 
169
        
170
        /**
171
         * OK, validate
172
         */
173
        else {
174
            
175
            $object = Comment::find($id);
176
            $object->commentstatus_id = 2;
177
            $object->save();
178
        }
179
        
180
         return Redirect::back();
181
       
182
    }
183
184
    /**
185
     * Mark as SPAM
186
     * 
187
     * @param integer $id
188
     * @param Request $request
189
     * @return Response
190
     */
191 View Code Duplication
    public function spam($id, Request $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...
192
        
193
        /**
194
         * Add ID to request
195
         */
196
        $request->merge(array('id' => $id));
197
        
198
        /**
199
         * Validate comment ID
200
         */
201
        $validator = Validator::make($request->all(), [
202
                    'id' => 'required|integer|min:1|exists:comment,id',
203
        ]);
204
        
205
        /**
206
         * Failed - redirect to index
207
         */
208
        if ($validator->fails()) {
209
       
210
        } 
211
        
212
        /**
213
         * OK, validate
214
         */
215
        else {
216
            
217
            $object = Comment::find($id);
218
            $object->commentstatus_id = 4;
219
            $object->save();
220
        }
221
        
222
         return Redirect::back();
223
    }
224
}
225