| Conditions | 5 |
| Paths | 12 |
| Total Lines | 60 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 71 | public function execute(){ |
||
| 72 | |||
| 73 | // (1) generate a conv id |
||
| 74 | $ids = array(); |
||
| 75 | foreach($this->requestData['user_to_invite'] as $userToInvite){ |
||
| 76 | $ids[] = $userToInvite['id']; |
||
| 77 | } |
||
| 78 | // always add our selve to the array for the conv id |
||
| 79 | $ids[] = $this->requestData['user']['id']; |
||
| 80 | |||
| 81 | // (2) check if conv id exists |
||
| 82 | |||
| 83 | if($id = $this->conversationMapper->existsByUsers($ids)){ |
||
| 84 | $id = $id['conv_id']; |
||
| 85 | } else { |
||
| 86 | $id = $this->generateConvId(); |
||
| 87 | |||
| 88 | // (3) Create the conv |
||
| 89 | $conversation = new Conversation(); |
||
| 90 | $conversation->setConversationId($id); |
||
| 91 | $this->conversationMapper->insert($conversation); |
||
| 92 | } |
||
| 93 | |||
| 94 | // (5) invite the user_to_invite since we just created the conv |
||
| 95 | // foreach user to invite |
||
| 96 | $requestData = array(); |
||
| 97 | $requestData['conv_id'] = $id; |
||
| 98 | $requestData['user'] = $this->requestData['user']; |
||
| 99 | foreach($this->requestData['user_to_invite'] as $userToInvite){ |
||
| 100 | if($userToInvite['id'] !== $this->requestData['user']['id']){ |
||
| 101 | $requestData['user_to_invite'] = $userToInvite; |
||
| 102 | $this->invite->setRequestData($requestData); |
||
| 103 | $this->invite->execute(); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | // (4) join the just created conv |
||
| 108 | $this->requestData['conv_id'] = $id; |
||
| 109 | $this->join->setRequestData($this->requestData); |
||
| 110 | $this->join->execute(); |
||
| 111 | |||
| 112 | // Fetch users in conv |
||
| 113 | $this->getUsers->setRequestData(array("conv_id" => $this->requestData['conv_id'])); |
||
| 114 | $users = $this->getUsers->execute(); |
||
| 115 | $users = $users['users']; |
||
| 116 | |||
| 117 | // Fetch messages in conv |
||
| 118 | $this->messages->setRequestData(array( |
||
| 119 | "conv_id" => $this->requestData['conv_id'], |
||
| 120 | 'user' => $this->requestData['user'] |
||
| 121 | )); |
||
| 122 | $messages = $this->messages->execute(); |
||
| 123 | $messages = $messages['messages']; |
||
| 124 | |||
| 125 | return array( |
||
| 126 | "conv_id" => $id, |
||
| 127 | "users" => $users, |
||
| 128 | "messages" => $messages |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | |||
| 141 |