3636namespace Jefferson49 \Webtrees \Module \OAuth2Client ;
3737
3838use Fig \Http \Message \RequestMethodInterface ;
39- use Fig \Http \Message \StatusCodeInterface ;
4039use Fisharebest \Localization \Translation ;
4140use Fisharebest \Webtrees \Auth ;
4241use Fisharebest \Webtrees \FlashMessages ;
6463use Fisharebest \Webtrees \Validator ;
6564use Fisharebest \Webtrees \Tree ;
6665use Fisharebest \Webtrees \View ;
66+ use Jefferson49 \Webtrees \Exceptions \GithubCommunicationError ;
6767use Jefferson49 \Webtrees \Helpers \Functions ;
68+ use Jefferson49 \Webtrees \Helpers \GithubService ;
6869use Jefferson49 \Webtrees \Internationalization \MoreI18N ;
6970use Jefferson49 \Webtrees \Log \CustomModuleLogInterface ;
7071use Jefferson49 \Webtrees \Module \OAuth2Client \Factories \AuthorizationProviderFactory ;
7172use Jefferson49 \Webtrees \Module \OAuth2Client \LoginWithAuthorizationProviderAction ;
7273use Jefferson49 \Webtrees \Module \OAuth2Client \RequestHandlers \RegisterWithProviderAction ;
73- use GuzzleHttp \Client ;
7474use Illuminate \Support \Collection ;
75- use GuzzleHttp \Exception \GuzzleException ;
7675use Psr \Http \Message \ResponseInterface ;
7776use Psr \Http \Message \ServerRequestInterface ;
7877use RuntimeException ;
7978
80- use function substr ;
81-
8279
8380class OAuth2Client extends AbstractModule implements
8481 ModuleCustomInterface,
@@ -105,13 +102,9 @@ class OAuth2Client extends AbstractModule implements
105102 public const REDIRECT_ROUTE = '/OAuth2Client ' ;
106103 public const REGISTER_PROVIDER_ROUTE = '/register-with-provider-action{/tree} ' ;
107104
108- //Github repository
105+ //Github
109106 public const GITHUB_REPO = 'Jefferson49/webtrees-oauth2-client ' ;
110107
111- //Github API URL to get the information about the latest releases
112- public const GITHUB_API_LATEST_VERSION = 'https://api.github.com/repos/ ' . self ::GITHUB_REPO . '/releases/latest ' ;
113- public const GITHUB_API_TAG_NAME_PREFIX = '"tag_name":"v ' ;
114-
115108 //Author of custom module
116109 public const CUSTOM_AUTHOR = 'Markus Hemprich ' ;
117110
@@ -292,43 +285,18 @@ public function customModuleVersion(): string
292285 */
293286 public function customModuleLatestVersion (): string
294287 {
295- // No update URL provided.
296- if (self ::GITHUB_API_LATEST_VERSION === '' ) {
297- return $ this ->customModuleVersion ();
298- }
299288 return Registry::cache ()->file ()->remember (
300289 $ this ->name () . '-latest-version ' ,
301290 function (): string {
302- try {
303- $ client = new Client (
304- [
305- 'timeout ' => 3 ,
306- ]
307- );
308-
309- $ response = $ client ->get (self ::GITHUB_API_LATEST_VERSION );
310291
311- if ($ response ->getStatusCode () === StatusCodeInterface::STATUS_OK ) {
312- $ content = $ response ->getBody ()->getContents ();
313- preg_match_all ('/ ' . self ::GITHUB_API_TAG_NAME_PREFIX . '\d+\.\d+\.\d+/ ' , $ content , $ matches , PREG_OFFSET_CAPTURE );
314-
315- if (!empty ($ matches [0 ]))
316- {
317- $ version = $ matches [0 ][0 ][0 ];
318- $ version = substr ($ version , strlen (self ::GITHUB_API_TAG_NAME_PREFIX ));
319- }
320- else
321- {
322- $ version = $ this ->customModuleVersion ();
323- }
324-
325- return $ version ;
326- }
327- } catch (GuzzleException $ ex ) {
328- // Can't connect to the server?
292+ try {
293+ //Get latest release from GitHub
294+ return GithubService::getLatestReleaseTag (self ::GITHUB_REPO );
295+ }
296+ catch (GithubCommunicationError $ ex ) {
297+ // Can't connect to GitHub?
298+ return $ this ->customModuleVersion ();
329299 }
330-
331- return $ this ->customModuleVersion ();
332300 },
333301 86400
334302 );
0 commit comments