Passed
Push — master ( 80db42...d275a8 )
by Anton
03:59 queued 23s
created

ListCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/bluzman
5
 */
6
7
namespace Bluzman\Command\Module;
8
9
use Bluzman\Command\AbstractCommand;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
/**
14
 * Class List command
15
 *
16
 * @package Bluzman\Command
17
 *
18
 * @author   Pavel Machekhin
19
 * @created  2013-03-28 14:03
20
 */
21
class ListCommand extends AbstractCommand
22
{
23
    /**
24
     * Command configuration
25
     */
26 14
    protected function configure()
27
    {
28
        $this
29
            // the name of the command (the part after "bin/bluzman")
30 14
            ->setName('module:list')
31
            // the short description shown while running "php bin/bluzman list"
32 14
            ->setDescription('List available modules')
33
            // the full command description shown when running the command with
34
            // the "--help" option
35 14
            ->setHelp('')
36
        ;
37 14
    }
38
39
    /**
40
     * @link https://developer.github.com/v3/#user-agent-required
41
     * @link https://developer.github.com/v3/search/#search-repositories
42
     * @param InputInterface $input
43
     * @param OutputInterface $output
44
     * @return void
45
     */
46
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48
        $curl = curl_init();
49
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
50
        curl_setopt(
51
            $curl,
52
            CURLOPT_URL,
53
            'https://api.github.com/search/repositories?q=topic:bluz-module+org:bluzphp'
54
        );
55
        curl_setopt(
56
            $curl,
57
            CURLOPT_HTTPHEADER,
58
            [
59
                'Accept: application/vnd.github.v3+json',
60
                'User-Agent: Bluzphp-Bluzman'
61
            ]
62
        );
63
        $repositories = curl_exec($curl);
64
        curl_close($curl);
65
66
        if (!$repositories) {
67
            $this->error('ERROR: Network problems, try again later');
68
            return;
69
        }
70
71
        $repositories = json_decode($repositories);
72
73
        if (!$repositories) {
74
            $this->error('ERROR: Invalid GitHub response');
75
            return;
76
        }
77
78
        if (!$repositories->total_count) {
79
            $this->error('ERROR: Not found any modules');
80
            return;
81
        }
82
83
        $this->write('List of modules (<info>installed</info>, <comment>available</comment>):');
84
85
86
        $repoData = [];
87
88
        foreach ($repositories->items as $repo) {
89
            $module = substr($repo->name, 7);
90
            $repoData[$module] = $repo->full_name;
91
        }
92
93
        ksort($repoData);
94
95
        foreach ($repoData as $module => $name) {
96
            if ($this->getApplication()->isModuleExists($module)) {
97
                $this->write(" - <info>$module</info> [$name]");
98
            } else {
99
                $this->write(" - <comment>$module</comment> [$name]");
100
            }
101
        }
102
103
        $this->write('You can install new module with <info>bluzman module:install name</info> command');
104
    }
105
}
106