Completed
Push — master ( 0a71d8...c173f4 )
by Michał
49:45 queued 34:02
created

SetupCommand::setupLocale()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
rs 8.8571
cc 2
eloc 16
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\InstallerBundle\Command;
13
14
use Sylius\Component\Core\Model\ChannelInterface;
15
use Sylius\Component\Currency\Model\CurrencyInterface;
16
use Sylius\Component\Locale\Model\LocaleInterface;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Intl\Intl;
20
use Symfony\Component\Validator\Constraints\Currency;
21
use Symfony\Component\Validator\Constraints\Email;
22
use Symfony\Component\Validator\Constraints\NotBlank;
23
24
/**
25
 * @author Paweł Jędrzejewski <[email protected]>
26
 */
27
class SetupCommand extends AbstractInstallCommand
28
{
29
    /**
30
     * @var CurrencyInterface
31
     */
32
    private $currency;
33
34
    /**
35
     * @var LocaleInterface
36
     */
37
    private $locale;
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected function configure()
43
    {
44
        $this
45
            ->setName('sylius:install:setup')
46
            ->setDescription('Sylius configuration setup.')
47
            ->setHelp(<<<EOT
48
The <info>%command.name%</info> command allows user to configure basic Sylius data.
49
EOT
50
            )
51
        ;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    protected function execute(InputInterface $input, OutputInterface $output)
58
    {
59
        $this->setupCurrency($input, $output);
60
        $this->setupLocale($input, $output);
61
        $this->setupChannel();
62
        $this->setupAdministratorUser($input, $output);
63
    }
64
65
    /**
66
     * @param InputInterface $input
67
     * @param OutputInterface $output
68
     *
69
     * @return int
70
     */
71
    protected function setupAdministratorUser(InputInterface $input, OutputInterface $output)
72
    {
73
        $output->writeln('Create your administrator account.');
74
75
        $userManager = $this->get('sylius.manager.user');
76
        $userRepository = $this->get('sylius.repository.user');
77
        $userFactory = $this->get('sylius.factory.user');
78
        $customerFactory = $this->get('sylius.factory.customer');
79
80
        $rbacInitializer = $this->get('sylius.rbac.initializer');
81
        $rbacInitializer->initialize();
82
83
        $user = $userFactory->createNew();
84
        $customer = $customerFactory->createNew();
85
        $user->setCustomer($customer);
86
87
        if ($input->getOption('no-interaction')) {
88
            $exists = null !== $userRepository->findOneByEmail('[email protected]');
89
90
            if ($exists) {
91
                return 0;
92
            }
93
94
            $customer->setFirstname('Sylius');
95
            $customer->setLastname('Admin');
96
            $user->setEmail('[email protected]');
97
            $user->setPlainPassword('sylius');
98
        } else {
99
            $customer->setFirstname($this->ask($output, 'Your firstname:', [new NotBlank()]));
100
            $customer->setLastname($this->ask($output, 'Lastname:', [new NotBlank()]));
101
102
            do {
103
                $email = $this->ask($output, 'E-Mail:', [new NotBlank(), new Email()]);
104
                $exists = null !== $userRepository->findOneByEmail($email);
105
106
                if ($exists) {
107
                    $output->writeln('<error>E-Mail is already in use!</error>');
108
                }
109
            } while ($exists);
110
111
            $user->setEmail($email);
112
            $user->setPlainPassword($this->getAdministratorPassword($output));
113
        }
114
115
        $user->setEnabled(true);
116
        $user->addAuthorizationRole($this->get('sylius.repository.role')->findOneBy(['code' => 'administrator']));
117
118
        $userManager->persist($user);
119
        $userManager->flush();
120
        $output->writeln('Administrator account successfully registered.');
121
    }
122
123
    /**
124
     * @param InputInterface  $input
125
     * @param OutputInterface $output
126
     */
127
    protected function setupLocale(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Unused Code introduced by
The parameter $input is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
128
    {
129
        $localeRepository = $this->get('sylius.repository.locale');
130
        $localeManager = $this->get('sylius.manager.locale');
131
        $localeFactory = $this->get('sylius.factory.locale');
132
133
        $code = trim($this->getContainer()->getParameter('sylius.locale'));
134
        $name = Intl::getLanguageBundle()->getLanguageName($code);
135
        $output->writeln(sprintf('Adding <info>%s</info> locale.', $name));
136
137
        if (null !== $existingLocale = $localeRepository->findOneBy(['code' => $code])) {
138
            $this->locale = $existingLocale;
139
140
            $localeManager->flush();
141
142
            return;
143
        }
144
145
        $locale = $localeFactory->createNew();
146
        $locale->setCode($code);
147
148
        $localeManager->persist($locale);
149
        $localeManager->flush();
150
151
        $this->locale = $locale;
152
    }
153
154
    /**
155
     * @param InputInterface  $input
156
     * @param OutputInterface $output
157
     */
158
    protected function setupCurrency(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Unused Code introduced by
The parameter $input is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
159
    {
160
        $currencyRepository = $this->get('sylius.repository.currency');
161
        $currencyManager = $this->get('sylius.manager.currency');
162
        $currencyFactory = $this->get('sylius.factory.currency');
163
164
        $code = trim($this->getContainer()->getParameter('sylius.currency'));
165
        $name = Intl::getCurrencyBundle()->getCurrencyName($code);
166
        $output->writeln(sprintf('Adding <info>%s</info> currency.', $name));
167
168
        if (null !== $existingCurrency = $currencyRepository->findOneBy(['code' => $code])) {
169
            $this->currency = $existingCurrency;
170
            $existingCurrency->setBase(true);
171
172
            $currencyManager->flush();
173
174
            return;
175
        }
176
177
        $currency = $currencyFactory->createNew();
178
179
        $currency->setExchangeRate(1);
180
        $currency->setBase(true);
181
        $currency->setCode($code);
182
183
        $this->currency = $currency;
184
185
        $currencyManager->persist($currency);
186
        $currencyManager->flush();
187
188
        $this->currency = $currency;
189
    }
190
191
    protected function setupChannel()
192
    {
193
        $channelRepository = $this->get('sylius.repository.channel');
194
        $channelManager = $this->get('sylius.manager.channel');
195
        $channelFactory = $this->get('sylius.factory.channel');
196
197
        $channel = $channelRepository->findOneByCode('default');
198
199
        if (null !== $channel) {
200
            return;
201
        }
202
203
        /** @var ChannelInterface $channel */
204
        $channel = $channelFactory->createNew();
205
        $channel->setCode('default');
206
        $channel->setName('Default');
207
208
        $channel->addCurrency($this->currency);
209
        $channel->addLocale($this->locale);
210
211
        $channelManager->persist($channel);
212
        $channelManager->flush();
213
    }
214
215
    /**
216
     * @param OutputInterface $output
217
     *
218
     * @return mixed
219
     */
220
    private function getAdministratorPassword(OutputInterface $output)
221
    {
222
        do {
223
            $password = $this->askHidden($output, 'Choose password:', [new NotBlank()]);
224
            $repeatedPassword = $this->askHidden($output, 'Confirm password:', [new NotBlank()]);
225
226
            if ($repeatedPassword !== $password) {
227
                $output->writeln('<error>Passwords do not match!</error>');
228
            }
229
        } while ($repeatedPassword !== $password);
230
231
        return $password;
232
    }
233
}
234