ComposeMap::execute()   B
last analyzed

Complexity

Conditions 8
Paths 15

Size

Total Lines 59
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 7.132
c 0
b 0
f 0
cc 8
eloc 35
nc 15
nop 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace Rottenwood\KingdomBundle\Command\Game;
4
5
use Rottenwood\KingdomBundle\Command\Infrastructure\AbstractGameCommand;
6
use Rottenwood\KingdomBundle\Command\Infrastructure\CommandResponse;
7
use Rottenwood\KingdomBundle\Entity\Room;
8
9
/**
10
 * Отрисовка карты
11
 * Применение в js: Kingdom.Websocket.command('composeMap')
12
 */
13
class ComposeMap extends AbstractGameCommand
14
{
15
16
    /**
17
     * Количество комнат от центра в каждую сторону
18
     * @var int
19
     */
20
    private $mapRadius = 2;
21
22
    /**
23
     * @return CommandResponse
24
     */
25
    public function execute(): CommandResponse
26
    {
27
        $currentRoom = $this->user->getRoom();
28
        $currentX = $currentRoom->getX();
29
        $currentY = $currentRoom->getY();
30
31
        $roomRepository = $this->container->get('kingdom.room_repository');
32
        $resourceRepository = $this->container->get('kingdom.room_resource_repository');
33
34
        $map = [];
35
36
        for ($y = $currentY - $this->mapRadius, $relativeY = 1, $relativeX = 1;
0 ignored issues
show
Comprehensibility Bug introduced by
Loop incrementor ($relativeX) jumbling with inner loop
Loading history...
37
             $y <= $currentY + $this->mapRadius;
38
             $y++, $relativeY++, $relativeX = 1) {
39
40
            for ($x = $currentX - $this->mapRadius;
41
                 $x <= $currentX + $this->mapRadius;
42
                 $x++, $relativeX++) {
43
44
                // пропуск центра карты, комнаты где находится персонаж
45
                if ($relativeY == 3 && $relativeX == 3) {
46
                    continue;
47
                }
48
49
                $room = $roomRepository->findOneByXandY($x, $y);
50
51
                if ($room) {
52
                    $map[] = $this->addRoom($room, $relativeX, $relativeY);
53
                }
54
            }
55
        }
56
57
        $result = [
58
            'name'        => $currentRoom->getName(),
59
            'description' => $currentRoom->getDescription(),
60
            'x'           => $currentX,
61
            'y'           => $currentY,
62
        ];
63
64
        $roomResources = $resourceRepository->findByRoom($currentRoom);
65
66
        foreach ($roomResources as $roomResource) {
67
            $resourceQuantity = $roomResource->getQuantity();
68
69
            if ($resourceQuantity > 0) {
70
                $result['resources'][] = [
71
                    'id'       => $roomResource->getItem()->getId(),
72
                    'name'     => $roomResource->getItem()->getName(),
73
                    'name4'    => $roomResource->getItem()->getName4(),
74
                    'quantity' => $resourceQuantity,
75
                ];
76
            }
77
        }
78
79
        $this->result->setData($result);
80
        $this->result->setMapData($map);
81
82
        return $this->result;
83
    }
84
85
    /**
86
     * @param Room $room
87
     * @param int  $x
88
     * @param int  $y
89
     * @return array
90
     */
91
    private function addRoom(Room $room, int $x, int $y): array
92
    {
93
        return [
94
            'x'   => $x,
95
            'y'   => $y,
96
            'pic' => $room->getType()->getPicture(),
97
        ];
98
    }
99
}
100