Skip to content

Commit adebed9

Browse files
authored
Use Guzzle to for version checking (#949)
1 parent b03d660 commit adebed9

File tree

2 files changed

+58
-25
lines changed

2 files changed

+58
-25
lines changed

Model/ExtensionNotification.php

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,57 @@
33
namespace Algolia\AlgoliaSearch\Model;
44

55
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
6+
use GuzzleHttp\Client;
67
use Magento\Framework\App\CacheInterface;
8+
use Magento\Framework\Serialize\SerializerInterface;
9+
use Psr\Log\LoggerInterface;
10+
use Psr\Log\LogLevel;
711

812
class ExtensionNotification
913
{
1014
const CHECK_FREQUENCY = 86400; // one day
1115

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';
1317

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'];
2319

2420
/** @var CacheInterface */
2521
protected $cacheManager;
2622

2723
/** @var ConfigHelper */
2824
private $configHelper;
2925

26+
/** @var LoggerInterface */
27+
private $logger;
28+
29+
/** @var SerializerInterface */
30+
private $serializer;
31+
32+
/** @var Client */
33+
private $guzzleClient;
34+
3035
private $repoData = null;
3136

3237
/**
3338
* @param CacheInterface $cacheManager
3439
* @param ConfigHelper $configHelper
40+
* @param LoggerInterface $logger
41+
* @param SerializerInterface $serializer
3542
*/
3643
public function __construct(
3744
CacheInterface $cacheManager,
38-
ConfigHelper $configHelper
45+
ConfigHelper $configHelper,
46+
LoggerInterface $logger,
47+
SerializerInterface $serializer
3948
) {
4049
$this->cacheManager = $cacheManager;
4150
$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+
]);
4257
}
4358

4459
/**
@@ -65,10 +80,13 @@ public function checkVersion()
6580
*/
6681
private function getLastCheck()
6782
{
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+
7290
if ($notificationData === null || !is_array($notificationData)) {
7391
$notificationData = [
7492
'time' => 0,
@@ -90,7 +108,10 @@ private function getLastCheck()
90108
*/
91109
private function setLastCheck($newExtensionData)
92110
{
93-
$this->cacheManager->save(json_encode($newExtensionData), 'algoliasearch_notification_lastcheck');
111+
$this->cacheManager->save(
112+
$this->serializer->serialize($newExtensionData),
113+
'algoliasearch_notification_lastcheck'
114+
);
94115

95116
return $this;
96117
}
@@ -99,21 +120,25 @@ private function checkExtensionVersion()
99120
{
100121
$newVersion = null;
101122
try {
102-
$versionFromRepository = $this->getLatestVersionFromRepository()->name;
123+
$versionFromRepository = $this->getLatestVersionFromRepository();
124+
$versionName = $versionFromRepository['name'];
125+
$versionUrl = $versionFromRepository['html_url'];
103126
} catch (\Exception $e) {
127+
$this->logger->log(LogLevel::INFO, $e->getMessage());
128+
104129
return $newVersion;
105130
}
106131

107132
$newVersion = [
108133
'time' => time(),
109134
'is_new' => false,
110-
'version' => $versionFromRepository,
111-
'url' => $this->getLatestVersionFromRepository()->html_url,
135+
'version' => $versionName,
136+
'url' => $versionUrl,
112137
];
113138

114139
$versionFromDb = $this->configHelper->getExtensionVersion();
115140
// 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, '<')) {
117142
$newVersion['is_new'] = true;
118143
$this->setLastCheck($newVersion);
119144

@@ -128,12 +153,19 @@ private function checkExtensionVersion()
128153
private function getLatestVersionFromRepository()
129154
{
130155
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+
]
135162
);
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());
137169
}
138170

139171
return $this->repoData;

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"php": "~7.0|~7.1|~7.2|~7.3",
99
"magento/framework": "~101.0|~102.0",
1010
"algolia/algoliasearch-client-php": ">=1.27.0 <2.0",
11+
"guzzlehttp/guzzle": "^6.3.3",
1112
"ext-json": "*",
1213
"ext-PDO": "*",
1314
"ext-mbstring": "*",

0 commit comments

Comments
 (0)