diff --git a/Block/Adminhtml/System/Config/Button/VersionCheck.php b/Block/Adminhtml/System/Config/Button/VersionCheck.php index 5914a82..6331349 100644 --- a/Block/Adminhtml/System/Config/Button/VersionCheck.php +++ b/Block/Adminhtml/System/Config/Button/VersionCheck.php @@ -7,10 +7,12 @@ namespace TrueLayer\Connect\Block\Adminhtml\System\Config\Button; -use TrueLayer\Connect\Api\Config\RepositoryInterface as ConfigRepository; use Magento\Backend\Block\Template\Context; use Magento\Config\Block\System\Config\Form\Field; use Magento\Framework\Data\Form\Element\AbstractElement; +use Magento\Framework\HTTP\Client\Curl; +use TrueLayer\Connect\Api\Config\RepositoryInterface as ConfigRepository; +use TrueLayer\Connect\Api\Log\LogServiceInterface; /** * Version check class @@ -22,10 +24,6 @@ class VersionCheck extends Field * @var string */ protected $_template = 'TrueLayer_Connect::system/config/button/version.phtml'; - /** - * @var ConfigRepository - */ - private $configRepository; /** * VersionCheck constructor. @@ -35,10 +33,11 @@ class VersionCheck extends Field */ public function __construct( Context $context, - ConfigRepository $configRepository, + private ConfigRepository $configRepository, + private Curl $curl, + private LogServiceInterface $logger, array $data = [] ) { - $this->configRepository = $configRepository; parent::__construct($context, $data); } @@ -66,6 +65,56 @@ public function _getElementHtml(AbstractElement $element): string */ public function getVersion(): string { - return 'v' . $this->configRepository->getExtensionVersion(); + return $this->configRepository->getExtensionVersion(); + } + + public function getLatestVersion() + { + $curlVersion = $this->getCurlVersion(); + $this->curl->addHeader('Accept', 'application/vnd.github+json'); + $this->curl->addHeader('User-Agent', 'curl/'.$curlVersion); + $this->curl->setOption(CURLOPT_RETURNTRANSFER, true); + $this->curl->get('https://api.github.com/repos/TrueLayer/magento2/releases'); + $responseStatus = $this->curl->getStatus(); + if ($responseStatus !== 200) { + $this->logger->error('Plugin version check failed, could not retrieve releases from github api', [ + 'response_status' => $responseStatus, + 'response_body' => $this->curl->getBody() + ]); + return false; + } + $response = $this->curl->getBody(); + try { + $releases = json_decode($response, true, JSON_THROW_ON_ERROR); + } catch (\Exception $e) { + $this->logger->error('Plugin version check failed, json_decode error', [ + 'response_body' => $response, + 'json_exception' => $e->getMessage() + ]); + return false; + } + foreach ($releases as $release) { + if (!$release['draft'] && !$release['prerelease']) { + $latestRelease = $release; + break; + } + } + if (!isset($latestRelease)) { + $this->logger->error('Plugin version check failed, no valid release in github api response'); + return false; + } + $latestVersion = ltrim($latestRelease['name'], 'v'); + return $latestVersion; + } + + private function getCurlVersion() + { + $curlVersion = curl_version(); + if (is_array($curlVersion) && array_key_exists('version', $curlVersion)) { + $curlVersion = $curlVersion['version']; + } else { + $curlVersion = 'unknown'; + } + return $curlVersion; } } diff --git a/view/adminhtml/templates/system/config/button/version.phtml b/view/adminhtml/templates/system/config/button/version.phtml index 9f4a213..5399fb8 100644 --- a/view/adminhtml/templates/system/config/button/version.phtml +++ b/view/adminhtml/templates/system/config/button/version.phtml @@ -10,7 +10,29 @@ use TrueLayer\Connect\Block\Adminhtml\System\Config\Button\VersionCheck; * @see \TrueLayer\Connect\Block\Adminhtml\System\Config\Button\VersionCheck * @var VersionCheck $block */ +$currentVersion = $block->getVersion(); +$latestVersion = $block->getLatestVersion(); +$upToDate = !$latestVersion || version_compare($currentVersion, $latestVersion, '>='); ?> -