ValidateIdCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 56.25%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 105
ccs 18
cts 32
cp 0.5625
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 28 4
A __construct() 0 5 1
A requestClientInfo() 0 18 4
1
<?php
2
/**
3
 * ValidateIdCommand.
4
 */
5
6
namespace Bmatovu\MtnMomo\Console;
7
8
use Bmatovu\MtnMomo\Traits\CommandUtilTrait;
9
use GuzzleHttp\ClientInterface;
10
use GuzzleHttp\Exception\ClientException;
11
use GuzzleHttp\Exception\ConnectException;
12
use GuzzleHttp\Exception\ServerException;
13
use Illuminate\Console\Command;
14
use Ramsey\Uuid\Uuid;
15
16
/**
17
 * Get client APP API credentials.
18
 */
19
class ValidateIdCommand extends Command
20
{
21
    use CommandUtilTrait;
22
23
    /**
24
     * Guzzle HTTP client instance.
25
     *
26
     * @var \GuzzleHttp\ClientInterface
27
     */
28
    protected $client;
29
30
    /**
31
     * The name and signature of the console command.
32
     *
33
     * @var string
34
     */
35
    protected $signature = 'mtn-momo:validate-id
36
                                {--id= : Client APP ID.}
37
                                {--product= : Product subscribed to.}';
38
39
    /**
40
     * The console command description.
41
     *
42
     * @var string
43
     */
44
    protected $description = "Validate client APP ID; 'apiuser'.";
45
46
    /**
47
     * Create a new command instance.
48
     *
49
     * @param \GuzzleHttp\ClientInterface $client
50
     */
51 25
    public function __construct(ClientInterface $client)
52
    {
53 25
        parent::__construct();
54
55 25
        $this->client = $client;
56
    }
57
58
    /**
59
     * Execute the console command.
60
     *
61
     * @throws \GuzzleHttp\Exception\GuzzleException
62
     *
63
     * @return void
64
     */
65 1
    public function handle()
66
    {
67 1
        $this->printLabels('Client APP ID -> Validation');
68
69 1
        $product = $this->option('product');
70
71 1
        if (! $product) {
72
            $product = $this->laravel['config']->get('mtn-momo.product');
73
        }
74
75 1
        $clientId = $this->option('id');
76
77 1
        if (! $clientId) {
78
            $clientId = $this->laravel['config']->get("mtn-momo.products.{$product}.id");
79
        }
80
81 1
        $clientId = $this->ask('Use client app ID?', $clientId);
82
83 1
        while (! Uuid::isValid($clientId)) {
84
            $this->info(' Invalid UUID (Format: 4). #IETF RFC4122');
85
            $clientId = $this->ask('MOMO_CLIENT_ID');
86
        }
87
88 1
        $validateIdUri = $this->laravel['config']->get('mtn-momo.api.validate_id_uri');
89
90 1
        $validateIdUri = str_replace('{clientId}', $clientId, $validateIdUri);
91
92 1
        $this->requestClientInfo($validateIdUri);
93
    }
94
95
    /**
96
     * Request client credentials.
97
     *
98
     * @link https://momodeveloper.mtn.com/docs/services/sandbox-provisioning-api/operations/get-v1_0-apiuser Documenation.
99
     *
100
     * @param string $clientIdStatusUri
101
     *
102
     * @throws \GuzzleHttp\Exception\GuzzleException
103
     *
104
     * @return void
105
     */
106 1
    protected function requestClientInfo($clientIdStatusUri)
107
    {
108
        try {
109 1
            $response = $this->client->request('GET', $clientIdStatusUri, []);
110
111 1
            $this->line("\r\nStatus: <fg=green>".$response->getStatusCode().' '.$response->getReasonPhrase().'</>');
112
113 1
            $this->line("\r\nBody: <fg=green>".$response->getBody()."</>\r\n");
114
        } catch (ConnectException $ex) {
115
            $this->line("\r\n<fg=red>".$ex->getMessage().'</>');
116
        } catch (ClientException $ex) {
117
            $response = $ex->getResponse();
118
            $this->line("\r\nStatus: <fg=yellow>".$response->getStatusCode().' '.$response->getReasonPhrase().'</>');
119
            $this->line("\r\nBody: <fg=yellow>".$response->getBody()."</>\r\n");
120
        } catch (ServerException $ex) {
121
            $response = $ex->getResponse();
122
            $this->line("\r\nStatus: <fg=red>".$response->getStatusCode().' '.$response->getReasonPhrase().'</>');
123
            $this->line("\r\nBody: <fg=red>".$response->getBody()."</>\r\n");
124
        }
125
    }
126
}
127