Completed
Push — master ( c4fab6...faae25 )
by Alireza
03:17
created

HookSetupCommand::getHomeDir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
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
class HookSetupCommand extends Command
21
{
22
    /**
23
     * configure command
24
     *
25
     * @return void
26
     */
27
    public function configure()
28
    {
29
        $this->setName('hook:setup')
30
            ->setDescription('Setup hook of your packagist package');
31
32
        $this->addOption('clear' , 'c', InputOption::VALUE_NONE, 'Clear default details');
33
    }
34
35
    /**
36
     * execute command
37
     *
38
     * @param InputInterface $input
39
     * @param OutputInterface $output
40
     */
41
    protected function execute(InputInterface $input , OutputInterface $output)
42
    {
43
        $authFile = package_path('/auth.json');
44
45
        if($input->getOption('clear')){
46
            unlink($authFile);
47
        }
48
49
        $command = new Style($input, $output);
50
51
        if(! file_exists($authFile)){
52
53
            $username = $command->addInput('Enter your packagist username');
54
55
            if(empty($username)) {
56
                $command->error("Packagist username is required !");
57
                exit;
58
            }
59
60
            $api = $command->addInput('Enter your packagist API');
61
62
            if(empty($api)) {
63
                $command->error("Packagist API is required !");
64
                exit;
65
            }
66
67
            $helper = $this->getHelper('question');
68
69
            $question = new ConfirmationQuestion(
70
                'Are you wanna to save this details ? [ Yes or No ]: ',
71
                false
72
            );
73
74
            $answer = $helper->ask($input, $output, $question);
75
76
            if($answer){
77
                mkdir(package_path(),0777);
78
                touch($authFile);
79
                chmod($authFile,0777);
80
                file_put_contents($authFile,json_encode([
81
                    'api' => $api,
82
                    'username' => $username
83
                ]));
84
            }
85
86
        } else {
87
88
            $auth = json_decode(file_get_contents($authFile));
89
90
            $command->line("");
91
            $command->line("Packagist API : ". $auth->api);
92
93
            $command->line('Packagist Username : '. $auth->username);
94
            $command->note(
95
                'If you want change your details type `josh hook:setup --clear` '
96
            );
97
98
            $api = $auth->api;
99
            $username = $auth->username;
100
        }
101
102
        $this->sendRequest($command,$api,$username);
103
    }
104
105
    /**
106
     * Send Request to packagist
107
     *
108
     * @param Style $command
109
     * @param $api
110
     * @param $username
111
     */
112
    public function sendRequest(Style $command, $api, $username)
113
    {
114
        $package = $command->addInput('Enter your package-url');
115
116
        if(empty(trim($package))) {
117
            $command->error("Package-url is required !");
118
            exit;
119
        }
120
121
        $command->info("Sending request ...");
122
123
        $data = [ 'repository' => [ 'url' => $package ] ];
124
125
        $client = new Guzzle([
126
            'base_uri' => 'https://packagist.org/',
127
            'headers' => [
128
                'Accept' => 'application/json'
129
            ]
130
        ]);
131
132
        $result = $client->post('/api/update-package?username=' . $username .
133
            '&apiToken=' . $api,[ 'json' => $data ]);
134
135
        $result = json_decode($result->getBody()->getContents(), true);
136
137
        if($result['status'] !== 'success') {
138
            $command->error("Message : ".$result['message']." | Failed ");
139
            exit;
140
        }
141
142
        $command->info("Success");
143
    }
144
}
145