3
3
namespace Algolia \AlgoliaSearch \Model ;
4
4
5
5
use Algolia \AlgoliaSearch \Helper \ConfigHelper ;
6
+ use GuzzleHttp \Client ;
6
7
use Magento \Framework \App \CacheInterface ;
8
+ use Magento \Framework \Serialize \SerializerInterface ;
9
+ use Psr \Log \LoggerInterface ;
10
+ use Psr \Log \LogLevel ;
7
11
8
12
class ExtensionNotification
9
13
{
10
14
const CHECK_FREQUENCY = 86400 ; // one day
11
15
12
- const REPOSITORY_URL = 'https://api.github.com /repos/algolia/algoliasearch-magento-2/releases/latest ' ;
16
+ const REPOSITORY_URI = '/repos/algolia/algoliasearch-magento-2/releases/latest ' ;
13
17
14
- const CURL_OPT = [
15
- 'http ' => [
16
- 'method ' => 'GET ' ,
17
- 'header ' => [
18
- 'User-Agent: PHP ' ,
19
- ],
20
- 'timeout ' => 10 ,
21
- ],
22
- ];
18
+ const APPLICATION_JSON_HEADER = ['Content-Type ' => 'application/json ' ];
23
19
24
20
/** @var CacheInterface */
25
21
protected $ cacheManager ;
26
22
27
23
/** @var ConfigHelper */
28
24
private $ configHelper ;
29
25
26
+ /** @var LoggerInterface */
27
+ private $ logger ;
28
+
29
+ /** @var SerializerInterface */
30
+ private $ serializer ;
31
+
32
+ /** @var Client */
33
+ private $ guzzleClient ;
34
+
30
35
private $ repoData = null ;
31
36
32
37
/**
33
38
* @param CacheInterface $cacheManager
34
39
* @param ConfigHelper $configHelper
40
+ * @param LoggerInterface $logger
41
+ * @param SerializerInterface $serializer
35
42
*/
36
43
public function __construct (
37
44
CacheInterface $ cacheManager ,
38
- ConfigHelper $ configHelper
45
+ ConfigHelper $ configHelper ,
46
+ LoggerInterface $ logger ,
47
+ SerializerInterface $ serializer
39
48
) {
40
49
$ this ->cacheManager = $ cacheManager ;
41
50
$ this ->configHelper = $ configHelper ;
51
+ $ this ->logger = $ logger ;
52
+ $ this ->serializer = $ serializer ;
53
+ $ this ->guzzleClient = new Client ([
54
+ 'base_uri ' => 'https://api.github.com ' ,
55
+ 'timeout ' => 10.0 ,
56
+ ]);
42
57
}
43
58
44
59
/**
@@ -65,10 +80,13 @@ public function checkVersion()
65
80
*/
66
81
private function getLastCheck ()
67
82
{
68
- $ notificationData = json_decode (
69
- $ this ->cacheManager ->load ('algoliasearch_notification_lastcheck ' ),
70
- true
71
- );
83
+ $ notificationData = null ;
84
+ $ cachedLastCheck = $ this ->cacheManager ->load ('algoliasearch_notification_lastcheck ' );
85
+
86
+ if ($ cachedLastCheck !== false ) {
87
+ $ notificationData = $ this ->serializer ->unserialize ($ cachedLastCheck , true );
88
+ }
89
+
72
90
if ($ notificationData === null || !is_array ($ notificationData )) {
73
91
$ notificationData = [
74
92
'time ' => 0 ,
@@ -90,7 +108,10 @@ private function getLastCheck()
90
108
*/
91
109
private function setLastCheck ($ newExtensionData )
92
110
{
93
- $ this ->cacheManager ->save (json_encode ($ newExtensionData ), 'algoliasearch_notification_lastcheck ' );
111
+ $ this ->cacheManager ->save (
112
+ $ this ->serializer ->serialize ($ newExtensionData ),
113
+ 'algoliasearch_notification_lastcheck '
114
+ );
94
115
95
116
return $ this ;
96
117
}
@@ -99,21 +120,25 @@ private function checkExtensionVersion()
99
120
{
100
121
$ newVersion = null ;
101
122
try {
102
- $ versionFromRepository = $ this ->getLatestVersionFromRepository ()->name ;
123
+ $ versionFromRepository = $ this ->getLatestVersionFromRepository ();
124
+ $ versionName = $ versionFromRepository ['name ' ];
125
+ $ versionUrl = $ versionFromRepository ['html_url ' ];
103
126
} catch (\Exception $ e ) {
127
+ $ this ->logger ->log (LogLevel::INFO , $ e ->getMessage ());
128
+
104
129
return $ newVersion ;
105
130
}
106
131
107
132
$ newVersion = [
108
133
'time ' => time (),
109
134
'is_new ' => false ,
110
- 'version ' => $ versionFromRepository ,
111
- 'url ' => $ this -> getLatestVersionFromRepository ()-> html_url ,
135
+ 'version ' => $ versionName ,
136
+ 'url ' => $ versionUrl ,
112
137
];
113
138
114
139
$ versionFromDb = $ this ->configHelper ->getExtensionVersion ();
115
140
// If the db version is older than the repo one, mark it as new and return it
116
- if (version_compare ($ versionFromDb , $ versionFromRepository , '< ' )) {
141
+ if (version_compare ($ versionFromDb , $ versionName , '< ' )) {
117
142
$ newVersion ['is_new ' ] = true ;
118
143
$ this ->setLastCheck ($ newVersion );
119
144
@@ -128,12 +153,19 @@ private function checkExtensionVersion()
128
153
private function getLatestVersionFromRepository ()
129
154
{
130
155
if ($ this ->repoData === null ) {
131
- $ json = file_get_contents (
132
- self ::REPOSITORY_URL ,
133
- false ,
134
- stream_context_create (self ::CURL_OPT )
156
+ $ response = $ this ->guzzleClient ->request (
157
+ 'GET ' ,
158
+ self ::REPOSITORY_URI ,
159
+ [
160
+ 'headers ' => self ::APPLICATION_JSON_HEADER ,
161
+ ]
135
162
);
136
- $ this ->repoData = json_decode ($ json );
163
+
164
+ if ($ response ->getStatusCode () != 200 ) {
165
+ throw new \Exception ($ response ->getReasonPhrase ());
166
+ }
167
+
168
+ $ this ->repoData = $ this ->serializer ->unserialize ($ response ->getBody ()->getContents ());
137
169
}
138
170
139
171
return $ this ->repoData ;
0 commit comments