Conditions | 11 |
Paths | 10 |
Total Lines | 68 |
Code Lines | 37 |
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 |
||
35 | public function sendWallAnswer(string $postId): ?string |
||
36 | { |
||
37 | $this->setJsonHeader(); |
||
38 | |||
39 | // not auth? what are you doing there? ;) |
||
40 | if (!App::$User->isAuth()) { |
||
41 | throw new ForbiddenException('Auth required'); |
||
42 | } |
||
43 | |||
44 | // no post id? wtf you doing man! |
||
45 | if (!Any::isInt($postId) || $postId < 1) { |
||
46 | throw new NativeException('Wrong input data'); |
||
47 | } |
||
48 | |||
49 | // get current(sender) user object |
||
50 | $viewer = App::$User->identity(); |
||
51 | |||
52 | // get message from post and validate minlength |
||
53 | $message = $this->request->get('message'); |
||
54 | $message = App::$Security->strip_tags($message); |
||
55 | if (!Any::isStr($message) || Str::length($message) < 3) { |
||
|
|||
56 | throw new ForbiddenException('Wrong input data'); |
||
57 | } |
||
58 | |||
59 | // try to find this post |
||
60 | $wallPost = WallPost::where('id', '=', $postId); |
||
61 | if ($wallPost->count() < 1) { |
||
62 | throw new NativeException('Wrong input data'); |
||
63 | } |
||
64 | |||
65 | $wallRow = $wallPost->first(); |
||
66 | $targetId = $wallRow->target_id; |
||
67 | // check if in blacklist |
||
68 | if (!Blacklist::check($viewer->id, $targetId)) { |
||
69 | throw new ForbiddenException('User is blocked!'); |
||
70 | } |
||
71 | |||
72 | // check delay between user last post and current |
||
73 | $lastAnswer = WallAnswer::where('user_id', '=', App::$User->identity()->getId()) |
||
74 | ->orderBy('created_at', 'DESC') |
||
75 | ->first(); |
||
76 | if (!$lastAnswer) { |
||
77 | $now = time(); |
||
78 | $answerTime = Date::convertToTimestamp($lastAnswer->created_at); |
||
79 | $cfgs = \Apps\ActiveRecord\App::getConfigs('app', 'Profile'); |
||
80 | // hmm, maybe past less then delay required? |
||
81 | if ($now - (int)$cfgs['delayBetweenPost'] < $answerTime) { |
||
82 | throw new ForbiddenException('Delay between answers not pass'); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | // make new row ;) |
||
87 | $answers = new WallAnswer(); |
||
88 | $answers->post_id = $postId; |
||
89 | $answers->user_id = $viewer->id; |
||
90 | $answers->message = $message; |
||
91 | $answers->save(); |
||
92 | |||
93 | // add notification for target user |
||
94 | if ($viewer->id !== $targetId) { |
||
95 | $notify = new EntityAddNotification($targetId); |
||
96 | $notify->add('/profile/show/' . $targetId . '#wall-post-' . $wallRow->id, EntityAddNotification::MSG_ADD_WALLANSWER, [ |
||
97 | 'snippet' => Text::snippet($message, 50), |
||
98 | 'post' => $wallRow->message |
||
99 | ]); |
||
100 | } |
||
101 | |||
102 | return json_encode(['status' => 1, 'message' => 'ok']); |
||
103 | } |
||
105 |