Completed
Push — master ( 4f2dbb...42248e )
by Alireza
02:01
created

HookSetupCommand::sendRequest()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 18
nc 3
nop 3
1
<?php
2
3
/**
4
 * Packagist hook setup
5
 *
6
 * @author Alireza Josheghani <[email protected]>
7
 * @since  17 Nov 2016
8
 */
9
10
namespace Josh\Console\Commands;
11
12
use GuzzleHttp\Client as Guzzle;
13
use Josh\Console\ConsoleStyle as Style;
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Console\Question\ConfirmationQuestion;
19
20
21
class HookSetupCommand extends Command
22
{
23
24
    /**
25
     * configure command
26
     *
27
     * @return void
28
     */
29
    public function configure()
30
    {
31
        $this->setName('hook:setup')
32
            ->setDescription('Setup hook of your packagist package');
33
34
        $this->addOption('clear' , 'c', InputOption::VALUE_NONE, 'Clear default details');
35
    }
36
37
    /**
38
     * execute command
39
     *
40
     * @param InputInterface $input
41
     * @param OutputInterface $output
42
     */
43
    protected function execute(InputInterface $input , OutputInterface $output)
0 ignored issues
show
Coding Style introduced by
execute uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
44
    {
45
        $authFile = $_SERVER['HOME'] . '/.Josh/auth.json';
46
47
        if($input->getOption('clear')){
48
            unlink($authFile);
49
        }
50
51
        $command = new Style($input, $output);
52
53
        if(! file_exists($authFile)){
54
55
            $username = $command->addInput('Enter your packagist username');
56
57
            if(empty($username)) {
58
                $command->error("Packagist username is required !");
59
                exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method execute() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
60
            }
61
62
            $api = $command->addInput('Enter your packagist API');
63
64
            if(empty($api)) {
65
                $command->error("Packagist API is required !");
66
                exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method execute() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
67
            }
68
69
            $helper = $this->getHelper('question');
70
71
            $question = new ConfirmationQuestion(
72
                'Are you wanna to save this details of default ? [ Yes or No ] :',
73
                ['yes' , 'no']
0 ignored issues
show
Documentation introduced by
array('yes', 'no') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
            );
75
76
            $answer = $helper->ask($input, $output, $question);
77
78
            if($answer){
79
                file_put_contents($authFile,json_encode([
80
                    'api' => $api,
81
                    'username' => $username
82
                ]));
83
            }
84
85
        } else {
86
87
            $auth = file_get_contents($authFile);
88
89
            $auth = json_decode($auth, true);
90
91
            $command->line("");
92
93
            $command->line("Packagist API : ". $auth['api']);
94
95
            $command->line('Packagist Username : '. $auth['username']);
96
97
            $command->note(
98
                'If you want change your details type `josh hook:setup --clear` '
99
            );
100
101
            $api = $auth['api'];
102
103
            $username = $auth['username'];
104
        }
105
106
        $this->sendRequest($command,$api,$username);
107
    }
108
109
    /**
110
     * Send Request to packagist
111
     *
112
     * @param Style $command
113
     * @param $api
114
     * @param $username
115
     */
116
    public function sendRequest(Style $command, $api, $username)
117
    {
118
        $package = $command->addInput('Enter your package-url');
119
120
        if(empty(trim($package))) {
121
            $command->error("Package-url is required !");
122
            exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method sendRequest() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
123
        }
124
125
        $command->info("Sending request ...");
126
127
        $data = [ 'repository' => [ 'url' => $package ] ];
128
129
        $client = new Guzzle([
130
            'base_uri' => 'https://packagist.org/',
131
            'headers' => [
132
                'Accept' => 'application/json'
133
            ]
134
        ]);
135
136
        $result = $client->post('/api/update-package?username=' . $username .
137
            '&apiToken=' . $api,[ 'json' => $data ]);
138
139
        $result = json_decode($result->getBody()->getContents(), true);
140
141
        if($result['status'] !== 'success') {
142
            $command->error("Message : ".$result['message']." | Failed ");
143
            exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method sendRequest() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
144
        }
145
146
        $command->info("Success");
147
    }
148
149
}