7
7
8
8
namespace TrueLayer \Connect \Block \Adminhtml \System \Config \Button ;
9
9
10
- use TrueLayer \Connect \Api \Config \RepositoryInterface as ConfigRepository ;
11
10
use Magento \Backend \Block \Template \Context ;
12
11
use Magento \Config \Block \System \Config \Form \Field ;
13
12
use Magento \Framework \Data \Form \Element \AbstractElement ;
13
+ use Magento \Framework \HTTP \Client \Curl ;
14
+ use TrueLayer \Connect \Api \Config \RepositoryInterface as ConfigRepository ;
15
+ use TrueLayer \Connect \Api \Log \LogServiceInterface ;
14
16
15
17
/**
16
18
* Version check class
@@ -22,10 +24,6 @@ class VersionCheck extends Field
22
24
* @var string
23
25
*/
24
26
protected $ _template = 'TrueLayer_Connect::system/config/button/version.phtml ' ;
25
- /**
26
- * @var ConfigRepository
27
- */
28
- private $ configRepository ;
29
27
30
28
/**
31
29
* VersionCheck constructor.
@@ -35,10 +33,11 @@ class VersionCheck extends Field
35
33
*/
36
34
public function __construct (
37
35
Context $ context ,
38
- ConfigRepository $ configRepository ,
36
+ private ConfigRepository $ configRepository ,
37
+ private Curl $ curl ,
38
+ private LogServiceInterface $ logger ,
39
39
array $ data = []
40
40
) {
41
- $ this ->configRepository = $ configRepository ;
42
41
parent ::__construct ($ context , $ data );
43
42
}
44
43
@@ -66,6 +65,56 @@ public function _getElementHtml(AbstractElement $element): string
66
65
*/
67
66
public function getVersion (): string
68
67
{
69
- return 'v ' . $ this ->configRepository ->getExtensionVersion ();
68
+ return $ this ->configRepository ->getExtensionVersion ();
69
+ }
70
+
71
+ public function getLatestVersion ()
72
+ {
73
+ $ curlVersion = $ this ->getCurlVersion ();
74
+ $ this ->curl ->addHeader ('Accept ' , 'application/vnd.github+json ' );
75
+ $ this ->curl ->addHeader ('User-Agent ' , 'curl/ ' .$ curlVersion );
76
+ $ this ->curl ->setOption (CURLOPT_RETURNTRANSFER , true );
77
+ $ this ->curl ->get ('https://api.github.com/repos/TrueLayer/magento2/releases ' );
78
+ $ responseStatus = $ this ->curl ->getStatus ();
79
+ if ($ responseStatus !== 200 ) {
80
+ $ this ->logger ->error ('Plugin version check failed, could not retrieve releases from github api ' , [
81
+ 'response_status ' => $ responseStatus ,
82
+ 'response_body ' => $ this ->curl ->getBody ()
83
+ ]);
84
+ return false ;
85
+ }
86
+ $ response = $ this ->curl ->getBody ();
87
+ try {
88
+ $ releases = json_decode ($ response , true , JSON_THROW_ON_ERROR );
89
+ } catch (\Exception $ e ) {
90
+ $ this ->logger ->error ('Plugin version check failed, json_decode error ' , [
91
+ 'response_body ' => $ response ,
92
+ 'json_exception ' => $ e ->getMessage ()
93
+ ]);
94
+ return false ;
95
+ }
96
+ foreach ($ releases as $ release ) {
97
+ if (!$ release ['draft ' ] && !$ release ['prerelease ' ]) {
98
+ $ latestRelease = $ release ;
99
+ break ;
100
+ }
101
+ }
102
+ if (!isset ($ latestRelease )) {
103
+ $ this ->logger ->error ('Plugin version check failed, no valid release in github api response ' );
104
+ return false ;
105
+ }
106
+ $ latestVersion = ltrim ($ latestRelease ['name ' ], 'v ' );
107
+ return $ latestVersion ;
108
+ }
109
+
110
+ private function getCurlVersion ()
111
+ {
112
+ $ curlVersion = curl_version ();
113
+ if (is_array ($ curlVersion ) && array_key_exists ('version ' , $ curlVersion )) {
114
+ $ curlVersion = $ curlVersion ['version ' ];
115
+ } else {
116
+ $ curlVersion = 'unknown ' ;
117
+ }
118
+ return $ curlVersion ;
70
119
}
71
120
}
0 commit comments