Completed
Push — master ( 1d29a1...e8393d )
by Petr
02:53
created

CreateRobotCommand::createMoney()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
namespace Rottenwood\KingdomBundle\Command\Console;
4
5
use Rottenwood\KingdomBundle\Entity\Money;
6
use Rottenwood\KingdomBundle\Entity\Robot;
7
use Rottenwood\KingdomBundle\Entity\Room;
8
use Rottenwood\KingdomBundle\Exception\RoomNotFound;
9
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
15
class CreateRobotCommand extends ContainerAwareCommand
16
{
17
18
    protected function configure()
19
    {
20
        $this->setName('kingdom:create:robot');
21
        $this->setDescription('Создание робота');
22
23
        $this->addArgument('name',
24
            InputArgument::REQUIRED,
25
            'имя робота'
26
        );
27
28
        $this->addArgument('roomId',
29
            InputArgument::REQUIRED,
30
            'id комнаты рождения'
31
        );
32
    }
33
34
    protected function execute(InputInterface $input, OutputInterface $output)
35
    {
36
        $name = $input->getArgument('name');
37
        $roomId = $input->getArgument('roomId');
38
39
        $output->write('Создание робота ... ');
40
41
        $container = $this->getContainer();
42
43
        $roomRepository = $container->get('kingdom.room_repository');
44
45
        /** @var Room $room */
46
        $room = $roomRepository->find($roomId);
47
48
        if (!$room) {
49
            throw new RoomNotFound();
50
        }
51
52
        $em = $roomRepository->getEntityManager();
53
        $userService = $container->get('kingdom.user_service');
54
55
        $cyrillicName = $userService->transliterate($name);
56
57
        $robot = new Robot();
58
        $robot->setName($cyrillicName);
59
        $robot->setRoom($room);
60
        $this->createMoney($robot, $container);
61
62
        $em->persist($robot);
63
        $em->flush($robot);
64
65
        $output->writeln('Робот создан!');
66
67
        $output->writeln('====================================');
68
        $output->writeln('Id: ' . $robot->getId());
69
        $output->writeln('Имя: ' . $robot->getName());
70
        $output->writeln(
71
            sprintf('Комната: [%d]%s %d/%d (%d)',
72
                $room->getId(),
73
                $room->getName(),
74
                $room->getX(),
75
                $room->getY(),
76
                $room->getZ()
77
            )
78
        );
79
        $output->writeln('====================================');
80
    }
81
82
    /**
83
     * Создание денег игрока
84
     * @param Robot              $robot
85
     * @param ContainerInterface $container
86
     */
87
    private function createMoney(Robot $robot, ContainerInterface $container)
88
    {
89
        $money = new Money($robot);
90
        $moneyRepository = $container->get('kingdom.money_repository');
91
        $moneyRepository->persist($money);
92
    }
93
}
94