Conditions | 16 |
Paths | 18 |
Total Lines | 54 |
Code Lines | 29 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
48 | public function check() |
||
49 | { |
||
50 | // check if user is auth'd or guest name is defined |
||
51 | if (!App::$User->isAuth() && ((int)$this->_configs['guestAdd'] !== 1 || Str::length($this->guestName) < 2)) { |
||
52 | throw new JsonException(__('Guest name is not defined')); |
||
53 | } |
||
54 | |||
55 | // guest moderation |
||
56 | if (!App::$User->isAuth() && (bool)$this->_configs['guestModerate']) { |
||
57 | $captcha = App::$Request->request->get('captcha'); |
||
58 | if (!App::$Captcha->validate($captcha)) { |
||
59 | throw new JsonException(__('Captcha is incorrect! Click on image to refresh and try again')); |
||
60 | } |
||
61 | } |
||
62 | |||
63 | // check if replayTo is defined |
||
64 | if ($this->replayTo < 1) { |
||
65 | throw new JsonException(__('Comment post thread is not founded')); |
||
66 | } |
||
67 | |||
68 | // check if message length is correct |
||
69 | if (Str::length($this->message) < (int)$this->_configs['minLength'] || Str::length($this->message) > (int)$this->_configs['maxLength']) { |
||
70 | throw new JsonException(__('Message length is incorrect. Current: %cur%, min - %min%, max - %max%', [ |
||
71 | 'cur' => Str::length($this->message), |
||
72 | 'min' => $this->_configs['minLength'], |
||
73 | 'max' => $this->_configs['maxLength'] |
||
74 | ])); |
||
75 | } |
||
76 | |||
77 | $count = CommentPost::where('id', '=', $this->replayTo)->count(); |
||
78 | if ($count !== 1) { |
||
79 | throw new JsonException(__('Comment post thread is not founded')); |
||
80 | } |
||
81 | |||
82 | // check to prevent spam |
||
83 | $query = CommentAnswer::where('user_id', $this->_userId) |
||
84 | ->orWhere('ip', $this->ip) |
||
85 | ->orderBy('created_at', 'DESC') |
||
86 | ->first(); |
||
87 | |||
88 | // something is founded :D |
||
89 | if ($query) { |
||
90 | $isModerator = false; |
||
91 | if (App::$User->isAuth() && App::$User->identity()->role->can('global/modify')) { |
||
92 | $isModerator = true; |
||
93 | } |
||
94 | $answerTime = Date::convertToTimestamp($query->created_at); |
||
|
|||
95 | $delay = $answerTime + $this->_configs['delay'] - time(); |
||
96 | if ($delay > 0 && !$isModerator) { // sounds like config time is not passed now |
||
97 | throw new JsonException(__('Spam protection: please, wait %sec% seconds', ['sec' => $delay])); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | return true; |
||
102 | } |
||
136 |