Completed
Push — master ( 42248e...0d72b5 )
by Alireza
02:36
created

HookSetupCommand::getHomeDir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
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)
44
    {
45
        $authFile = $this->getHomeDir() . '/.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
                touch($authFile);
80
                chmod($authFile,777);
81
                file_put_contents($authFile,json_encode([
82
                    'api' => $api,
83
                    'username' => $username
84
                ]));
85
            }
86
87
        } else {
88
89
            $auth = file_get_contents($authFile);
90
91
            $auth = json_decode($auth, true);
92
93
            $command->line("");
94
95
            $command->line("Packagist API : ". $auth['api']);
96
97
            $command->line('Packagist Username : '. $auth['username']);
98
99
            $command->note(
100
                'If you want change your details type `josh hook:setup --clear` '
101
            );
102
103
            $api = $auth['api'];
104
105
            $username = $auth['username'];
106
        }
107
108
        $this->sendRequest($command,$api,$username);
109
    }
110
111
    /**
112
     * Send Request to packagist
113
     *
114
     * @param Style $command
115
     * @param $api
116
     * @param $username
117
     */
118
    public function sendRequest(Style $command, $api, $username)
119
    {
120
        $package = $command->addInput('Enter your package-url');
121
122
        if(empty(trim($package))) {
123
            $command->error("Package-url is required !");
124
            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...
125
        }
126
127
        $command->info("Sending request ...");
128
129
        $data = [ 'repository' => [ 'url' => $package ] ];
130
131
        $client = new Guzzle([
132
            'base_uri' => 'https://packagist.org/',
133
            'headers' => [
134
                'Accept' => 'application/json'
135
            ]
136
        ]);
137
138
        $result = $client->post('/api/update-package?username=' . $username .
139
            '&apiToken=' . $api,[ 'json' => $data ]);
140
141
        $result = json_decode($result->getBody()->getContents(), true);
142
143
        if($result['status'] !== 'success') {
144
            $command->error("Message : ".$result['message']." | Failed ");
145
            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...
146
        }
147
148
        $command->info("Success");
149
    }
150
151
    public function getHomeDir()
0 ignored issues
show
Coding Style introduced by
getHomeDir 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...
152
    {
153
        if(empty($_SERVER['HOME'])){
154
            return posix_getpwuid(posix_getuid());
155
        }
156
157
        return $_SERVER['HOME'];
158
    }
159
160
}