PublishLogin::handle()   B
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 45
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 29
nc 16
nop 0
dl 0
loc 45
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Acacha\ForgePublish\Commands;
4
5
use Acacha\ForgePublish\Commands\Traits\InteractsWithEnvironment;
6
use Acacha\ForgePublish\Commands\Traits\PossibleEmails;
7
use Acacha\ForgePublish\Commands\Traits\ShowsErrorResponse;
8
use Acacha\ForgePublish\Commands\Traits\SkipsIfEnvVariableIsAlreadyInstalled;
9
use Acacha\ForgePublish\Commands\Traits\DiesIfNoEnvFileExists;
10
use GuzzleHttp\Client;
11
use Illuminate\Console\Command;
12
13
/**
14
 * Class PublishLogin.
15
 *
16
 * @package Acacha\ForgePublish\Commands
17
 */
18
class PublishLogin extends Command
19
{
20
    use ShowsErrorResponse, DiesIfNoEnvFileExists, InteractsWithEnvironment, PossibleEmails;
21
22
    /**
23
     * The name and signature of the console command.
24
     *
25
     * @var string
26
     */
27
    protected $signature = 'publish:login {email?}';
28
29
    /**
30
     * The console command description.
31
     *
32
     * @var string
33
     */
34
    protected $description = 'Login to acacha forge';
35
36
    /**
37
     * Guzzle Http client
38
     *
39
     * @var Client
40
     */
41
    protected $http;
42
43
    /**
44
     * Endpoint api url.
45
     *
46
     * @var String
47
     */
48
    protected $url;
49
50
    /**
51
     * PublishLogin constructor.
52
     *
53
     * @param Client $client
54
     */
55
    public function __construct(Client $client)
56
    {
57
        parent::__construct();
58
        $this->http = $client;
59
    }
60
61
    /**
62
     * Execute the console command.
63
     *
64
     */
65
    public function handle()
66
    {
67
        $this->checkIfCommandHaveToBeSkipped();
68
69
        $emails = $this->getPossibleEmails();
70
        $email = $this->argument('email') ?
71
            $this->argument('email') :
72
            $this->anticipate('Email?', $emails, $current_value = fp_env('ACACHA_FORGE_EMAIL'));
73
74
        if ($email != fp_env('ACACHA_FORGE_EMAIL')) {
75
            $this->addValueToEnv('ACACHA_FORGE_EMAIL', $email);
76
        }
77
78
        $password = $this->secret('Password?');
79
80
        $this->url = config('forge-publish.url') . config('forge-publish.token_uri');
81
        $response = '';
82
        try {
83
            $response = $this->http->post($this->url, [
84
                'form_params' => [
85
                    'client_id' => config('forge-publish.client_id'),
86
                    'client_secret' => config('forge-publish.client_secret'),
87
                    'grant_type' => 'password',
88
                    'username' => $email,
89
                    'password' => $password,
90
                    'scope' => '*',
91
                ]
92
            ]);
93
        } catch (\Exception $e) {
94
            $this->showErrorAndDie($e);
95
        }
96
97
        $body = json_decode((string) $response->getBody());
98
99
        if (!isset($body->access_token)) {
100
            $this->error("The URL $this->url doesn't return an access_token!");
101
            die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method handle() 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...
102
        }
103
104
        $access_token = $body->access_token;
105
106
        $this->addValueToEnv('ACACHA_FORGE_ACCESS_TOKEN', $access_token);
107
108
        $this->info('The access token has been added to file .env with key ACACHA_FORGE_ACCESS_TOKEN');
109
    }
110
111
    /**
112
     * Check if command have to be skipped.
113
     */
114
    protected function checkIfCommandHaveToBeSkipped()
115
    {
116
        $this->dieIfNoEnvFileIsFound();
117
    }
118
}
119