Completed
Push — master ( 8e578c...cb817c )
by Petr
03:04
created

Wear::execute()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 25
rs 8.8571
c 1
b 0
f 0
cc 3
eloc 15
nc 3
nop 0
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
     * @return CommandResponse
18
     * @throws WrongSlot
19
     */
20
    public function execute() {
21
        $parameters = explode(':', $this->parameters);
22
        $itemId = $parameters[0];
23
        $slot = $parameters[1];
24
25
        $inventoryItemRepository = $this->container->get('kingdom.inventory_item_repository');
26
        $inventoryItem = $inventoryItemRepository->findOneByUserAndItemId($this->user, $itemId);
27
28
        if (!$inventoryItem->getItem()->fitsTo($slot)) {
29
            throw new WrongSlot($slot);
30
        }
31
32
        $wornItem = $inventoryItemRepository->findOneByUserAndSlot($this->user, $slot);
33
34
        if ($wornItem) {
35
            $wornItem->removeSlot();
36
            $inventoryItemRepository->flush($wornItem);
37
        }
38
39
        $inventoryItem->setSlot($slot);
40
41
        $inventoryItemRepository->flush($inventoryItem);
42
43
        return $this->result;
44
    }
45
}
46