| Conditions | 11 |
| Paths | 7 |
| Total Lines | 64 |
| Code Lines | 39 |
| 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 |
||
| 31 | public function show(string $userId): ?string |
||
| 32 | { |
||
| 33 | $cfg = $this->application->configs; |
||
| 34 | if (!(bool)$cfg['guestView'] && !App::$User->isAuth()) { |
||
| 35 | throw new ForbiddenException(__('You must login to view other profile')); |
||
| 36 | } |
||
| 37 | |||
| 38 | // check if target exists |
||
| 39 | if (!App::$User->isExist($userId)) { |
||
| 40 | throw new NotFoundException(__('This profile is not exist')); |
||
| 41 | } |
||
| 42 | |||
| 43 | $targetPersone = App::$User->identity($userId); // target user object instance of Apps\ActiveRecord\User |
||
| 44 | $viewerPersone = App::$User->identity(); // current user object(viewer) instance of Apps\ActiveRecord\User |
||
| 45 | |||
| 46 | $wallModel = null; |
||
| 47 | // if current user is auth - allow to post messages on wall current user |
||
| 48 | if (App::$User->isAuth() && $viewerPersone->role->can('global/write')) { |
||
| 49 | $wallModel = new FormWallPost(); |
||
| 50 | // check if request post is done and rules validated |
||
| 51 | if ($wallModel->send() && $wallModel->validate()) { |
||
| 52 | // maybe in blacklist? |
||
| 53 | if (!Blacklist::check($viewerPersone->getId(), $targetPersone->getId())) { |
||
| 54 | App::$Session->getFlashBag()->add('error', __('This user are in your black list or you are in blacklist!')); |
||
| 55 | } else { |
||
| 56 | // check if message added |
||
| 57 | if ($wallModel->makePost($targetPersone, $viewerPersone, (int)$cfg['delayBetweenPost'])) { |
||
| 58 | App::$Session->getFlashBag()->add('success', __('The message was successful posted!')); |
||
| 59 | } else { |
||
| 60 | App::$Session->getFlashBag()->add('warning', __('Posting message was failed! Please, wait few seconds')); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | // get wall posts by target user_id |
||
| 67 | $wallQuery = WallPost::where('target_id', $targetPersone->getId()); |
||
| 68 | |||
| 69 | // pagination and query params |
||
| 70 | $wallPage = (int)$this->request->query->get('page'); |
||
| 71 | $wallStep = (int)$cfg['wallPostOnPage']; |
||
| 72 | $wallOffset = $wallPage * $wallStep; |
||
| 73 | $wallTotalCount = $wallQuery->count(); |
||
| 74 | |||
| 75 | // get wall messages as object |
||
| 76 | $wallRecords = $wallQuery->with(['senderUser', 'senderUser.profile', 'senderUser.role']) |
||
| 77 | ->orderBy('id', 'desc') |
||
| 78 | ->skip($wallOffset) |
||
| 79 | ->take($wallStep) |
||
| 80 | ->get(); |
||
| 81 | |||
| 82 | // render output view |
||
| 83 | return $this->view->render('profile/show', [ |
||
| 84 | 'user' => $targetPersone, |
||
| 85 | 'viewer' => $viewerPersone, |
||
| 86 | 'isSelf' => ($viewerPersone !== null && $viewerPersone->id === $targetPersone->id), |
||
| 87 | 'wall' => $wallModel, |
||
| 88 | 'wallRecords' => $wallRecords, |
||
| 89 | 'pagination' => [ |
||
| 90 | 'step' => $wallStep, |
||
| 91 | 'total' => $wallTotalCount, |
||
| 92 | 'page' => $wallPage |
||
| 93 | ], |
||
| 94 | 'ratingOn' => (int)$cfg['rating'] === 1 |
||
| 95 | ]); |
||
| 98 |