| Conditions | 10 |
| Paths | 22 |
| Total Lines | 95 |
| Code Lines | 64 |
| 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 |
||
| 24 | public function execute(): CommandResponse |
||
| 25 | { |
||
| 26 | |||
| 27 | if ($this->user->isBusy()) { |
||
| 28 | $this->result->setWaitstate($this->user->getWaitstate()); |
||
| 29 | |||
| 30 | return $this->result; |
||
| 31 | } |
||
| 32 | |||
| 33 | $roomRepository = $this->container->get('kingdom.room_repository'); |
||
| 34 | $em = $roomRepository->getEntityManager(); |
||
| 35 | |||
| 36 | $userId = $this->user->getId(); |
||
| 37 | $userName = $this->user->getName(); |
||
| 38 | |||
| 39 | /** @var Room $currentRoom */ |
||
| 40 | $currentRoom = $this->user->getRoom(); |
||
| 41 | |||
| 42 | $x = $currentRoom->getX(); |
||
| 43 | $y = $currentRoom->getY(); |
||
| 44 | |||
| 45 | if ($this->parameters == 'north') { |
||
| 46 | $y++; |
||
| 47 | $directionTo = 'на север'; |
||
| 48 | $directionFrom = 'с юга'; |
||
| 49 | } elseif ($this->parameters == 'south') { |
||
| 50 | $y--; |
||
| 51 | $directionTo = 'на юг'; |
||
| 52 | $directionFrom = 'с севера'; |
||
| 53 | } elseif ($this->parameters == 'east') { |
||
| 54 | $x++; |
||
| 55 | $directionTo = 'на восток'; |
||
| 56 | $directionFrom = 'с запада'; |
||
| 57 | } elseif ($this->parameters == 'west') { |
||
| 58 | $x--; |
||
| 59 | $directionTo = 'на запад'; |
||
| 60 | $directionFrom = 'с востока'; |
||
| 61 | } else { |
||
| 62 | throw new InvalidCommandParameter; |
||
| 63 | } |
||
| 64 | |||
| 65 | $destinationRoom = $roomRepository->findOneByXandY($x, $y); |
||
| 66 | |||
| 67 | if (!$destinationRoom || !$this->userCanWalkToRoom($destinationRoom)) { |
||
| 68 | $this->result->addError('В эту сторону не пройти'); |
||
| 69 | } else { |
||
| 70 | $this->user->setRoom($destinationRoom); |
||
| 71 | $em->flush($this->user); |
||
| 72 | |||
| 73 | /** @var \Redis $redis */ |
||
| 74 | $redis = $this->container->get('snc_redis.default'); |
||
| 75 | $redis->hset(RedisClientInterface::ID_ROOM_HASH, $userId, $destinationRoom->getId()); |
||
| 76 | |||
| 77 | $logger = $this->container->get('kingdom.logger'); |
||
| 78 | $logString = sprintf( |
||
| 79 | '[%d]%s переместился %s в комнату [%d]%s [%d/%d/%d]', |
||
| 80 | $userId, |
||
| 81 | $userName, |
||
| 82 | $directionTo, |
||
| 83 | $destinationRoom->getId(), |
||
| 84 | $destinationRoom->getName(), |
||
| 85 | $x, |
||
| 86 | $y, |
||
| 87 | $destinationRoom->getZ() |
||
| 88 | ); |
||
| 89 | |||
| 90 | $userService = $this->container->get('kingdom.user_service'); |
||
| 91 | |||
| 92 | $resultData = [ |
||
| 93 | 'name' => $userName, |
||
| 94 | 'directionTo' => $directionTo, |
||
| 95 | 'directionFrom' => $directionFrom, |
||
| 96 | ]; |
||
| 97 | |||
| 98 | if ($usersInCurrentRoom = $userService->getOnlineUsersIdsInRoom($currentRoom, $userId)) { |
||
| 99 | $resultData['left'] = $userService->getSessionsByUserIds($usersInCurrentRoom); |
||
| 100 | } |
||
| 101 | |||
| 102 | if ($usersInDestinationRoom = $userService->getOnlineUsersIdsInRoom($destinationRoom, $userId)) { |
||
| 103 | $resultData['enter'] = $userService->getSessionsByUserIds($usersInDestinationRoom); |
||
| 104 | |||
| 105 | $logString .= sprintf( |
||
| 106 | ', встретил игроков: [%s]', |
||
| 107 | implode(',', $usersInDestinationRoom) |
||
| 108 | ); |
||
| 109 | } |
||
| 110 | |||
| 111 | $logger->info($logString); |
||
| 112 | $this->result->setData($resultData); |
||
| 113 | |||
| 114 | $userService->addWaitstate($this->user, $this->waitState); |
||
| 115 | } |
||
| 116 | |||
| 117 | return $this->result; |
||
| 118 | } |
||
| 119 | |||
| 130 |