CreateUserCommand::createMoney()   A
last analyzed

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.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
namespace Rottenwood\KingdomBundle\Command\Console;
4
5
use Rottenwood\KingdomBundle\Entity\Infrastructure\HumanRepository;
6
use Rottenwood\KingdomBundle\Entity\Money;
7
use Rottenwood\KingdomBundle\Entity\Human as User;
8
use Rottenwood\UserBundle\Loggers\RegistrationLogger;
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
/** {@inheritDoc} */
16
class CreateUserCommand extends ContainerAwareCommand
17
{
18
19
    protected function configure()
20
    {
21
        $this->setName('kingdom:create:user');
22
        $this->setDescription('Создание персонажа');
23
24
        $this->addArgument(
25
            'username',
26
            InputArgument::REQUIRED,
27
            'имя игрока'
28
        );
29
30
        $this->addArgument(
31
            'password',
32
            InputArgument::REQUIRED,
33
            'пароль'
34
        );
35
36
        $this->addArgument(
37
            'email',
38
            InputArgument::REQUIRED,
39
            'e-mail'
40
        );
41
42
        $this->addArgument(
43
            'gender',
44
            InputArgument::OPTIONAL,
45
            'gender'
46
        );
47
    }
48
49
    protected function execute(InputInterface $input, OutputInterface $output)
50
    {
51
        $username = $input->getArgument('username');
52
        $password = $input->getArgument('password');
53
        $email = $input->getArgument('email');
54
        $gender = $input->getArgument('gender');
55
56
        $output->write('Создание персонажа ... ');
57
58
        $container = $this->getContainer();
59
60
        $userManager = $container->get('fos_user.user_manager');
61
        $humanRepository = $container->get('kingdom.human_repository');
62
        $userService = $container->get('kingdom.user_service');
63
64
        /** @var User $user */
65
        $user = $userManager->createUser();
66
        $user->setUsername($username);
67
        $user->setEmail($email);
68
        $userManager->updateCanonicalFields($user);
69
70
        $cyrillicName = $userService->transliterate($user->getUsernameCanonical());
71
72
        if ($this->checkUserExist($humanRepository, $username, $cyrillicName, $email)) {
73
            $output->writeln('персонаж уже существует.');
74
        } else {
75
            $user->setPlainPassword($password);
76
            $userManager->updatePassword($user);
77
78
            $user->setEnabled(true);
79
            $user->setAvatar($userService->pickAvatar());
80
            $user->setName($cyrillicName);
81
            $user->setRoom($userService->getStartRoom());
82
83
            // мужской пол по умолчанию
84
            if ($gender === User::GENDER_FEMALE) {
85
                $user->setGender(User::GENDER_FEMALE);
86
            }
87
88
            $this->createMoney($user, $container);
89
90
            $humanRepository->persist($user);
91
            $humanRepository->flush($user);
92
93
            $output->writeln('персонаж создан!');
94
95
            $output->writeln('====================================');
96
            $output->writeln('Логин: ' . $username);
97
            $output->writeln('Пароль: ' . $password);
98
            $output->writeln('E-mail: ' . $email);
99
            $output->writeln('====================================');
100
101
            /** @var RegistrationLogger $logger */
102
            $logger = $container->get('user.logger.registration');
103
104
            $logger->logRegistration($user);
105
        }
106
    }
107
108
    /**
109
     * @param HumanRepository $humanRepository
110
     * @param string          $username
111
     * @param string          $cyrillicName
112
     * @param string          $email
113
     * @return bool
114
     */
115
    private function checkUserExist(HumanRepository $humanRepository, $username, $cyrillicName, $email): bool
116
    {
117
        $user = $humanRepository->findByUsername($username);
118
119
        if (!$user) {
120
            $user = $humanRepository->findByName($cyrillicName);
121
        }
122
123
        if (!$user) {
124
            $user = $humanRepository->findByEmail($email);
125
        }
126
127
        return (bool) $user;
128
    }
129
130
    /**
131
     * Создание денег игрока
132
     * @param User               $user
133
     * @param ContainerInterface $container
134
     * @return void
135
     */
136
    private function createMoney(User $user, ContainerInterface $container)
137
    {
138
        $money = new Money($user);
139
        $moneyRepository = $container->get('kingdom.money_repository');
140
        $moneyRepository->persist($money);
141
    }
142
}
143