2
2
3
3
namespace DevTools \Command ;
4
4
5
+ use DevTools \ValueObjects \OAuthClient ;
5
6
use DevTools \ValueObjects \OAuthTokenSet ;
6
7
use GuzzleHttp \Client ;
7
8
use GuzzleHttp \HandlerStack ;
@@ -59,11 +60,10 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
59
60
protected function execute (InputInterface $ input , OutputInterface $ output ): int
60
61
{
61
62
$ this ->checkRedirectUriValue ();
62
- $ clientId = $ this ->askClientId ();
63
- $ clientSecret = $ this ->askClientSecret ();
64
- $ authorisationCode = $ this ->getAuthorisationCode ($ clientId );
65
- $ tokenSet = $ this ->fetchTokens ($ clientId , $ clientSecret , $ authorisationCode );
66
- $ this ->writeTokenSetToFile ($ tokenSet );
63
+ $ client = $ this ->askClientDetails ();
64
+ $ authorisationCode = $ this ->getAuthorisationCode ($ client );
65
+ $ tokenSet = $ this ->fetchTokens ($ client , $ authorisationCode );
66
+ $ this ->writeToFile ($ client , $ tokenSet );
67
67
68
68
return 0 ;
69
69
}
@@ -78,24 +78,29 @@ private function checkRedirectUriValue(): void
78
78
}
79
79
}
80
80
81
- private function askClientId (): string
81
+ private function askClientDetails (): OAuthClient
82
82
{
83
- $ question = new Question ('What is the Client ID (Can be found in the Exact App Center)? ' );
84
- return $ this ->questionHelper ->ask ($ this ->input , $ this ->output , $ question );
85
- }
83
+ $ clientId = $ this ->questionHelper ->ask (
84
+ $ this ->input ,
85
+ $ this ->output ,
86
+ new Question ('What is the Client ID (Can be found in the Exact App Center)? ' )
87
+ );
86
88
87
- private function askClientSecret (): string
88
- {
89
- $ question = new Question ('What is the Client secret (Can be found in the Exact App Center)? ' );
90
- return $ this ->questionHelper ->ask ($ this ->input , $ this ->output , $ question );
89
+ $ clientSecret = $ this ->questionHelper ->ask (
90
+ $ this ->input ,
91
+ $ this ->output ,
92
+ new Question ('What is the Client secret (Can be found in the Exact App Center)? ' )
93
+ );
94
+
95
+ return new OAuthClient ($ clientId , $ clientSecret );
91
96
}
92
97
93
- private function getAuthorisationCode (string $ clientId ): string
98
+ private function getAuthorisationCode (OAuthClient $ client ): string
94
99
{
95
100
$ this ->startHttpListening ();
96
101
97
102
$ url = self ::$ baseUrl . self ::$ authUrl . '? ' . http_build_query ([
98
- 'client_id ' => $ clientId ,
103
+ 'client_id ' => $ client -> getId () ,
99
104
'redirect_uri ' => self ::$ redirectUri ,
100
105
'response_type ' => 'code '
101
106
]);
@@ -115,7 +120,7 @@ private function getAuthorisationCode(string $clientId): string
115
120
$ i ++;
116
121
} else {
117
122
$ code = file_get_contents ($ filename );
118
- if ($ code === null ) {
123
+ if ($ code === false ) {
119
124
$ this ->output ->writeln ('Unable to read code from file, halting! ' );
120
125
$ this ->stopHttpListening ();
121
126
exit (1 );
@@ -143,7 +148,7 @@ private function stopHttpListening(): void
143
148
shell_exec (sprintf ('kill %d 2>&1 ' , $ this ->pid ));
144
149
}
145
150
146
- private function fetchTokens (string $ clientId , string $ clientSecret , string $ authorisationCode ): OAuthTokenSet
151
+ private function fetchTokens (OAuthClient $ client , string $ authorisationCode ): OAuthTokenSet
147
152
{
148
153
$ this ->output ->writeln ('Requesting access and refresh tokens. ' );
149
154
@@ -152,8 +157,8 @@ private function fetchTokens(string $clientId, string $clientSecret, string $aut
152
157
'form_params ' => [
153
158
'redirect_uri ' => self ::$ redirectUri ,
154
159
'grant_type ' => 'authorization_code ' ,
155
- 'client_id ' => $ clientId ,
156
- 'client_secret ' => $ clientSecret ,
160
+ 'client_id ' => $ client -> getId () ,
161
+ 'client_secret ' => $ client -> getSecret () ,
157
162
'code ' => $ authorisationCode
158
163
]
159
164
];
@@ -177,12 +182,18 @@ private function getFullDestinationPath(string $destination): string
177
182
return getcwd () . DIRECTORY_SEPARATOR . $ destination ;
178
183
}
179
184
180
- private function writeTokenSetToFile ( OAuthTokenSet $ tokenSet ): void
185
+ private function writeToFile ( OAuthClient $ client , OAuthTokenSet $ tokenSet ): void
181
186
{
182
- $ path = $ this ->getFullDestinationPath ($ this ->input ->getOption ('output ' ));
187
+ $ output = $ this ->input ->getOption ('output ' );
188
+
189
+ if (! is_string ($ output )) {
190
+ throw new \InvalidArgumentException ('Output parameter should be a string ' );
191
+ }
192
+
193
+ $ path = $ this ->getFullDestinationPath ($ output );
183
194
$ fileName = $ path . DIRECTORY_SEPARATOR . 'oauth.json ' ;
184
195
185
- file_put_contents ($ fileName , json_encode ($ tokenSet , JSON_PRETTY_PRINT ));
196
+ file_put_contents ($ fileName , json_encode (array_merge ( $ client -> jsonSerialize (), $ tokenSet-> jsonSerialize ()) , JSON_PRETTY_PRINT ));
186
197
187
198
$ this ->output ->writeln ('Access and refresh tokens are available in: ' . $ fileName );
188
199
}
0 commit comments