AuthCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 41
rs 10
ccs 0
cts 16
cp 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 31 4
1
<?php
2
3
namespace Bmatovu\AirtelMoney\Commands;
4
5
use Bmatovu\AirtelMoney\Facades\Authentication;
6
use Bmatovu\AirtelMoney\Traits\CommandUtils;
7
use GuzzleHttp\Exception\RequestException;
8
use Illuminate\Console\Command;
9
use Illuminate\Console\ConfirmableTrait;
10
use Psr\Http\Message\ResponseInterface;
11
12
class AuthCommand extends Command
13
{
14
    use CommandUtils, ConfirmableTrait;
15
16
    protected $signature = "airtel-money:auth
17
                            {--no-write : Don't write credentials to .env file.}
18
                            {--f|force : Force the operation to run when in production.}";
19
20
    public $description = 'Setup authentication';
21
22
    public function handle(): int
23
    {
24
        if (! $this->confirmToProceed()) {
25
            return self::FAILURE;
26
        }
27
28
        $this->line('<options=bold>Client App Credentials</>');
29
30
        $this->writeConfig('client_id');
31
32
        $this->writeConfig('client_secret');
33
34
        try {
35
            $apiRes = Authentication::getToken();
36
37
            $this->line(json_encode($apiRes, JSON_PRETTY_PRINT));
38
        } catch (RequestException $ex) {
39
            $response = $ex->getResponse();
40
41
            if ($response instanceof ResponseInterface) {
42
                $this->error($response->getStatusCode().' '.$response->getReasonPhrase());
43
44
                $this->error($response->getBody());
45
46
                return self::FAILURE;
47
            }
48
49
            $this->error($ex->getMessage());
50
        }
51
52
        return self::SUCCESS;
53
    }
54
}
55