ConfigureCommand   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 86
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
B execute() 0 37 6
A promptFor() 0 14 3
1
<?php
2
/**
3
 * Magedownload CLI
4
 *
5
 * PHP version 5
6
 *
7
 * @category  MageDownload
8
 * @package   MageDownload
9
 * @author    Steve Robbins <[email protected]>
10
 * @copyright 2015 Steve Robbins
11
 * @license   http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
12
 * @link      https://github.com/steverobbins/magedownload-cli
13
 */
14
15
namespace MageDownload\Command;
16
17
use MageDownload\Config;
18
use MageDownload\Download;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
/**
23
 * Download file command
24
 *
25
 * @category  MageDownload
26
 * @package   MageDownload
27
 * @author    Steve Robbins <[email protected]>
28
 * @copyright 2015 Steve Robbins
29
 * @license   http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
30
 * @link      https://github.com/steverobbins/magedownload-cli
31
 */
32
class ConfigureCommand extends AbstractCommand
33
{
34
    const NAME = 'configure';
35
36
    /**
37
     * Configure command
38
     *
39
     * @return void
40
     */
41
    protected function configure()
42
    {
43
        $this
44
            ->setName(self::NAME)
45
            ->setDescription('Configure your account ID and access token');
46
        parent::configure();
47
    }
48
49
    /**
50
     * Execute command
51
     *
52
     * @param InputInterface  $input
53
     * @param OutputInterface $output
54
     *
55
     * @return void
56
     */
57
    protected function execute(InputInterface $input, OutputInterface $output)
58
    {
59
        try {
60
            $currentId = $this->getAccountId();
61
        } catch (\InvalidArgumentException $e) {
62
            // Ignore this exception
63
            $currentId = false;
64
        }
65
        try {
66
            $currentToken = $this->getAccessToken();
67
        } catch (\InvalidArgumentException $e) {
68
            // Ignore this exception
69
            $currentToken = false;
70
        }
71
        if ($this->input->getOption('id')) {
72
            $newId = $this->input->getOption('id');
73
        } else {
74
            $newId = $this->promptFor('account id', $currentId);
75
        }
76
        if ($this->input->getOption('token')) {
77
            $newToken = $this->input->getOption('token');
78
        } else {
79
            $newToken = $this->promptFor('access token', $currentToken);
80
        }
81
        $config = new Config;
82
        $success = $config->saveConfig(array(
83
            'user' => array(
84
                'id'    => $newId,
85
                'token' => $newToken,
86
            )
87
        ));
88
        if ($success) {
89
            $this->out('<info>Configuration successfully updated</info>');
90
        } else {
91
            $this->out('<error>Failed to update configuration</error>');
92
        }
93
    }
94
95
    /**
96
     * Get the new value for config option
97
     *
98
     * @param string         $name
99
     * @param string|boolean $currentValue
100
     *
101
     * @return string
102
     */
103
    protected function promptFor($name, $currentValue)
104
    {
105
        $dialog   = $this->getHelper('dialog');
106
        $newValue = $dialog->ask(
107
            $this->output,
108
            sprintf('Please enter the %s%s: ', $name, $currentValue ? sprintf(' (%s)', $currentValue) : ''),
109
            $currentValue
110
        );
111
        if (!$newValue) {
112
            $this->output->writeln('<error>Value cannot be empty</error>');
113
            return $this->promptFor($name, $currentValue);
114
        }
115
        return $newValue;
116
    }
117
}
118