Wear   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 34
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B execute() 0 26 3
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\Exception\WrongSlot;
8
9
/**
10
 * Одеть предмет
11
 * Список слотов доступен в статическом методе Item::getAllSlotNames()
12
 * Применение в js: Kingdom.Websocket.command('wear', ['idПредмета', 'названиеСлота'])
13
 */
14
class Wear extends AbstractGameCommand
15
{
16
17
    /**
18
     * @return CommandResponse
19
     * @throws WrongSlot
20
     */
21
    public function execute(): CommandResponse
22
    {
23
        $parameters = explode(':', $this->parameters);
24
        $itemId = $parameters[0];
25
        $slot = $parameters[1];
26
27
        $inventoryItemRepository = $this->container->get('kingdom.inventory_item_repository');
28
        $inventoryItem = $inventoryItemRepository->findOneByUserAndItemId($this->user, $itemId);
29
30
        if (!$inventoryItem->getItem()->fitsTo($slot)) {
31
            throw new WrongSlot($slot);
32
        }
33
34
        $wornItem = $inventoryItemRepository->findOneByUserAndSlot($this->user, $slot);
35
36
        if ($wornItem) {
37
            $wornItem->removeSlot();
38
            $inventoryItemRepository->flush($wornItem);
39
        }
40
41
        $inventoryItem->setSlot($slot);
42
43
        $inventoryItemRepository->flush($inventoryItem);
44
45
        return $this->result;
46
    }
47
}
48