Completed
Push — master ( a7df58...69cda4 )
by Toan
16s
created

ConfigCommand::chooseCacheModeQuestion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright © 2017 Toan Nguyen. All rights reserved.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Gojira\Command;
10
11
use Gojira\Api\Authentication\BasicAuthentication;
12
use Gojira\Framework\App\Configuration\AuthInterface;
13
use Gojira\Framework\App\Configuration\ConfigurationInterface;
14
use Gojira\Framework\App\Configuration\OptionsInterface;
15
use Gojira\Provider\Console\Command;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
/**
21
 * Config Command, check and set config before start using the application
22
 *
23
 * @package Gojira\Command
24
 * @author  Toan Nguyen <[email protected]>
25
 */
26
class ConfigCommand extends Command
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function configure()
32
    {
33
        $help = __(
34
            "Config Help:\n%1\n%2\n%3\n%4\n%5",
35
            ' - Jira URL: https://foo.atlassian.net/',
36
            ' - Username: user (for [email protected])',
37
            ' - Password: Your password',
38
            ' - Timezone: Choose your timezone',
39
            ' - Use cache: Choose cache mode'
40
        );
41
42
        $this
43
            ->setName('config')
44
            ->setDescription('Change configuration')
45
            ->setHelp($help)
46
            ->addOption('clear', 'c', InputOption::VALUE_NONE, 'Clear stored configuration');
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function execute(InputInterface $input, OutputInterface $output)
53
    {
54
        if ($input->getOption('clear')) {
55
            $result = $this->configuration->clearConfig();
56
            $output->writeln('<info>' . $result['msg'] . '</info>');
57
        }
58
59
        if (!$this->authentication->isAuth()) {
60
            $jiraUrl = $this->prompt->ask($input, $output, $this->prompt->getJiraUrlQuestion());
61
            $username = $this->prompt->ask($input, $output, $this->prompt->getJiraUsernameQuestion());
62
            $password = $this->prompt->ask($input, $output, $this->prompt->getJiraPasswordQuestion());
63
            $timezones = $this->prompt->ask($input, $output, $this->prompt->chooseTimezoneQuestion());
64
            $useCache = $this->prompt->ask($input, $output, $this->prompt->chooseCacheModeQuestion());
65
66
            if (isset($jiraUrl, $username, $password, $timezones, $useCache)) {
67
                $authenticate = new BasicAuthentication($username, $password);
68
                $encryptionKey = md5($this->random->getRandomString(OptionsInterface::KEY_RANDOM_STRING_SIZE));
69
                $authItems = [
70
                    AuthInterface::BASE_URI => $jiraUrl,
71
                    AuthInterface::USERNAME => $username,
72
                    AuthInterface::TOKEN_SECRET => $authenticate->getCredential(),
73
                    AuthInterface::CONSUMER_SECRET => uniqid(),
74
                    AuthInterface::BCRYPT_MODE => false
75
                ];
76
                $optionItems = array_merge(
77
                    $this->optionConfig->initDefaultOptionItems(),
78
                    [
79
                        OptionsInterface::TIMEZONE => $timezones,
80
                        OptionsInterface::IS_USE_CACHE => $useCache,
81
                        OptionsInterface::ENCRYPTION_KEY => $encryptionKey
82
                    ]
83
                );
84
                $pathItems = $this->pathConfig->initDefaultPaths();
85
86
                $this->authConfig->setData($authItems);
87
                $this->optionConfig->setData($optionItems);
88
                $this->pathConfig->setData($pathItems);
89
90
                $this->configuration->setData(ConfigurationInterface::PATHS, $this->pathConfig->getData());
91
                $this->configuration->setData(ConfigurationInterface::AUTH, $this->authConfig->getData());
92
                $this->configuration->setData(ConfigurationInterface::OPTIONS, $this->optionConfig->getData());
93
                $result = $this->configuration->saveConfig();
94
95
                $output->writeln(__('<info>%1</info>', $result['msg']));
96
            }
97
        } else {
98
            $output->writeln(__(
99
                'STATUS: [<comment>%1</comment>]',
100
                'Authorized'
101
            ));
102
103
            /**
104
             * @var Command $command
105
             */
106
            $command = $this->getApplication()->getService('console')->find('list');
107
            $command->run($input, $output);
108
        }
109
    }
110
}
111