33namespace App \Commands \Authentication ;
44
55use App \Console \BaseCommand ;
6+ use App \Console \Output \JsonOutput ;
7+ use App \Credentials \ApiKey ;
68use App \Credentials \ApiKeyResolver ;
79use App \Credentials \Exceptions \CredentialException ;
810use App \Quickpay \AuthenticatedQuickpayFactory ;
1113
1214class AuthCommand extends BaseCommand
1315{
14- protected $ signature = 'auth ' ;
16+ protected $ signature = 'auth
17+ {--check : Exit non-zero when no active credential is configured}
18+ {--json : Write machine-readable JSON} ' ;
1519
1620 protected $ description = 'Show the active Quickpay authentication status ' ;
1721
@@ -23,15 +27,19 @@ public function handle(ApiKeyResolver $credentials, AuthenticatedQuickpayFactory
2327 return $ this ->failure ($ exception ->getMessage ());
2428 }
2529
26- $ this -> line ( ' Credential source: ' .( $ apiKey ?->source() ?? ' none ' ));
27- $ this ->line ( ' API base URL: ' .QuickpayClient:: BASE_URL );
28- $ this -> line ( ' API version: ' .QuickpayClient:: API_VERSION );
30+ if (! $ this -> option ( ' json ' )) {
31+ $ this ->writeHumanStatusHeader ( $ apiKey );
32+ }
2933
3034 if ($ apiKey === null ) {
31- $ this ->line ('Scope: not authenticated ' );
32- $ this ->comment ('Run quickpay login to store a merchant API key. ' );
35+ if ($ this ->option ('json ' )) {
36+ $ this ->writeJsonStatus (null , null );
37+ } else {
38+ $ this ->line ('Scope: not authenticated ' );
39+ $ this ->comment ('Run quickpay login to store a merchant API key. ' );
40+ }
3341
34- return self ::SUCCESS ;
42+ return $ this -> option ( ' check ' ) ? self :: FAILURE : self ::SUCCESS ;
3543 }
3644
3745 try {
@@ -57,8 +65,30 @@ public function handle(ApiKeyResolver $credentials, AuthenticatedQuickpayFactory
5765 return $ this ->failure ('Quickpay returned an invalid scope. ' );
5866 }
5967
60- $ this ->line ("Scope: {$ scope }" );
68+ if ($ this ->option ('json ' )) {
69+ $ this ->writeJsonStatus ($ apiKey , $ scope );
70+ } else {
71+ $ this ->line ("Scope: {$ scope }" );
72+ }
6173
6274 return self ::SUCCESS ;
6375 }
76+
77+ private function writeHumanStatusHeader (?ApiKey $ apiKey ): void
78+ {
79+ $ this ->line ('Credential source: ' .($ apiKey ?->source() ?? 'none ' ));
80+ $ this ->line ('API base URL: ' .QuickpayClient::BASE_URL );
81+ $ this ->line ('API version: ' .QuickpayClient::API_VERSION );
82+ }
83+
84+ private function writeJsonStatus (?ApiKey $ apiKey , ?string $ scope ): void
85+ {
86+ $ this ->getOutput ()->write (JsonOutput ::value ([
87+ 'authenticated ' => $ scope !== null ,
88+ 'credential_source ' => $ apiKey ?->source(),
89+ 'api_base_url ' => QuickpayClient::BASE_URL ,
90+ 'api_version ' => QuickpayClient::API_VERSION ,
91+ 'scope ' => $ scope ,
92+ ], '' ));
93+ }
6494}
0 commit comments