Completed
Push — master ( ac80bb...589441 )
by Steve
02:27
created

VersionsCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4286
cc 1
eloc 5
nc 1
nop 0
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\Output\OutputInterface;
20
21
/**
22
 * Versions command
23
 *
24
 * @category  MageDownload
25
 * @package   MageDownload
26
 * @author    Steve Robbins <[email protected]>
27
 * @copyright 2015 Steve Robbins
28
 * @license   http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
29
 * @link      https://github.com/steverobbins/magedownload-cli
30
 */
31
class VersionsCommand extends AbstractCommand
32
{
33
    const NAME = 'versions';
34
35
    const API_ACTION = 'versions';
36
37
    /**
38
     * Configure command
39
     *
40
     * @return void
41
     */
42
    protected function configure()
43
    {
44
        $this
45
            ->setName(self::NAME)
46
            ->setDescription('List files available for download');
47
        parent::configure();
48
    }
49
50
    /**
51
     * Execute command
52
     *
53
     * @param InputInterface  $input
54
     * @param OutputInterface $output
55
     *
56
     * @return void
57
     */
58
    protected function execute(InputInterface $input, OutputInterface $output)
59
    {
60
        $info = new Info;
61
        return $this->render($info->sendCommand(
62
            self::API_ACTION,
63
            $this->getAccountId(),
64
            $this->getAccessToken()
65
        ));
66
    }
67
68
    /**
69
     * Render the versions action
70
     *
71
     * @param string $result
72
     *
73
     * @return void
74
     */
75
    protected function render($result)
76
    {
77
        $editions = preg_split('/\n{2}/', $result);
78
        foreach ($editions as $info) {
79
            $bits = preg_split('/\-{5,}/', $info);
80
            $versions = explode("\n", trim($bits[1]));
81
            usort($versions, 'version_compare');
82
            array_walk($versions, function (&$value) {
83
                $value = array($value);
84
            });
85
            $this->out(array(array(
86
                'type' => 'table',
87
                'data' => array(
88
                    array(trim($bits[0])),
89
                    $versions
90
                )
91
            )));
92
        }
93
    }
94
}
95