ListCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 10
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * @copyright Bluz PHP Team
5
 * @link https://github.com/bluzphp/bluzman
6
 */
7
8
namespace Bluzman\Command\Module;
9
10
use Bluzman\Command\AbstractCommand;
11
use Symfony\Component\Console\Input\InputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Symfony\Component\Console\Output\OutputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Output\OutputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
/**
15
 * Class List command
16
 *
17
 * @package Bluzman\Command
18
 *
19
 * @author   Pavel Machekhin
20
 * @created  2013-03-28 14:03
21
 */
22
class ListCommand extends AbstractCommand
23
{
24
    /**
25
     * Command configuration
26 15
     */
27
    protected function configure()
28
    {
29
        $this
30 15
            // the name of the command (the part after "bin/bluzman")
31
            ->setName('module:list')
32 15
            // the short description shown while running "php bin/bluzman list"
33
            ->setDescription('List available modules')
34
            // the full command description shown when running the command with
35 15
            // the "--help" option
36
            ->setHelp('')
37 15
        ;
38
    }
39
40
    /**
41
     * @link https://developer.github.com/v3/#user-agent-required
42
     * @link https://developer.github.com/v3/search/#search-repositories
43
     * @param InputInterface $input
44
     * @param OutputInterface $output
45
     * @return int
46
     */
47
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        $curl = curl_init();
50
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
51
        curl_setopt(
52
            $curl,
53
            CURLOPT_URL,
54
            'https://api.github.com/search/repositories?q=topic:bluz-module+org:bluzphp'
55
        );
56
        curl_setopt(
57
            $curl,
58
            CURLOPT_HTTPHEADER,
59
            [
60
                'Accept: application/vnd.github.v3+json',
61
                'User-Agent: Bluzphp-Bluzman'
62
            ]
63
        );
64
        $repositories = curl_exec($curl);
65
        curl_close($curl);
66
67
        if (!$repositories) {
68
            $this->error('ERROR: Network problems, try again later');
69
            return 1;
70
        }
71
72
        $repositories = json_decode($repositories);
0 ignored issues
show
Bug introduced by
It seems like $repositories can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
        $repositories = json_decode(/** @scrutinizer ignore-type */ $repositories);
Loading history...
73
74
        if (!$repositories) {
75
            $this->error('ERROR: Invalid GitHub response');
76
            return 2;
77
        }
78
79
        if (!$repositories->total_count) {
80
            $this->error('ERROR: Not found any modules');
81
            return 3;
82
        }
83
84
        $this->write('List of modules (<info>installed</info>, <comment>available</comment>):');
85
86
87
        $repoData = [];
88
89
        foreach ($repositories->items as $repo) {
90
            $module = substr($repo->name, 7);
91
            $repoData[$module] = $repo->full_name;
92
        }
93
94
        ksort($repoData);
95
96
        foreach ($repoData as $module => $name) {
97
            if ($this->getApplication()->isModuleExists($module)) {
98
                $this->write(" - <info>$module</info> [$name]");
99
            } else {
100
                $this->write(" - <comment>$module</comment> [$name]");
101
            }
102
        }
103
104
        $this->write('You can install new module with <info>bluzman module:install name</info> command');
105
        return 0;
106
    }
107
}
108