Failed Conditions
Pull Request — master (#15)
by Steve
03:42
created

FilesCommand::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 2
eloc 11
nc 2
nop 1
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\Info;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Input\InputOption;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
/**
23
 * Files 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 FilesCommand extends AbstractCommand
33
{
34
    const NAME = 'files';
35
36
    const API_ACTION_FILES  = 'files';
37
    const API_ACTION_FILTER = 'filter';
38
39
    const OPTION_FILTER_TYPE    = 'filter-type';
40
    const OPTION_FILTER_VERSION = 'filter-version';
41
42
    protected $typeFilters = array(
43
        'ce-full',
44
        'ce-patch',
45
        'ee-full',
46
        'ee-patch',
47
        'other',
48
    );
49
50
    /**
51
     * Configure command
52
     *
53
     * @return void
54
     */
55
    protected function configure()
56
    {
57
        $this
58
            ->setName(self::NAME)
59
            ->setDescription('List files available for download')
60
            ->addOption(
61
                self::OPTION_FILTER_VERSION,
62
                null,
63
                InputOption::VALUE_REQUIRED,
64
                'Version to filter by (1.9.2.1, 1.9.*, etc)'
65
            )
66
            ->addOption(
67
                self::OPTION_FILTER_TYPE,
68
                null,
69
                InputOption::VALUE_REQUIRED,
70
                'Type to filter by (' . implode(', ', $this->typeFilters) . ')'
71
            );
72
        parent::configure();
73
    }
74
75
    /**
76
     * Execute command
77
     *
78
     * @param InputInterface  $input
79
     * @param OutputInterface $output
80
     *
81
     * @return void
82
     */
83
    protected function execute(InputInterface $input, OutputInterface $output)
84
    {
85
        $info    = new Info;
86
        $action  = self::API_ACTION_FILES;
87
        $filters = $this->getFilters();
88
        if ($filters) {
89
            $action = self::API_ACTION_FILTER;
90
        }
91
        return $this->render($info->sendCommand(
1 ignored issue
show
Bug introduced by
It seems like $info->sendCommand($acti...getAccessToken(), true) targeting MageDownload\Info::sendCommand() can also be of type string; however, MageDownload\Command\FilesCommand::render() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
92
            $action . $filters,
93
            $this->getAccountId(),
94
            $this->getAccessToken(),
95
            true
96
        ));
97
    }
98
99
    /**
100
     * Get any applied filters
101
     *
102
     * @return string
103
     */
104
    protected function getFilters()
105
    {
106
        $filters = array();
107
        if ($this->input->getOption(self::OPTION_FILTER_VERSION)) {
108
            $filters['version'] = $this->input->getOption(self::OPTION_FILTER_VERSION);
109
        }
110
        if ($this->input->getOption(self::OPTION_FILTER_TYPE)) {
111
            $filters['type'] = $this->input->getOption(self::OPTION_FILTER_TYPE);
112
            if (!in_array($filters['type'], $this->typeFilters)) {
113
                throw new \InvalidArgumentException(
114
                    "Invalid filter type.  Must be one of: \n    " . implode("\n    ", $this->typeFilters)
115
                );
116
            }
117
        }
118
        if (!count($filters)) {
119
            return;
120
        }
121
        $result = '/';
122
        foreach ($filters as $type => $value) {
123
            $result .= $type . '/' . $value;
124
        }
125
        return $result;
126
    }
127
128
    /**
129
     * Render the files action
130
     *
131
     * @param array $result
132
     *
133
     * @return void
134
     */
135
    protected function render(array $result)
136
    {
137
        if (count($result) == 1) {
138
            return $this->out(trim($result[0]));
139
        }
140
        $headers = array_keys($result[0]);
141
        $rows = array_map('array_values', $result);
142
        usort($rows, array($this, 'sortFiles'));
143
        $this->out(array(array(
144
            'type' => 'table',
145
            'data' => array(
146
                $headers,
147
                $rows
148
            )
149
        )));
150
    }
151
152
    /**
153
     * Sort files by type and name
154
     *
155
     * @param string[] $a
156
     * @param string[] $b
157
     *
158
     * @return integer
159
     */
160
    protected function sortFiles($a, $b)
161
    {
162
        foreach (array_keys($a) as $key) {
163
            $test = strcmp($a[$key], $b[$key]);
164
            if ($test) {
165
                return $test;
166
            }
167
        }
168
    }
169
}
170