Skip to content

Commit

Permalink
Add module update notification
Browse files Browse the repository at this point in the history
* Add update notice to module config page.
* Create admin notification once for each update.
* Change module version 3.1.11.
  • Loading branch information
JerrySmidt committed Aug 8, 2023
1 parent 1d12b38 commit 996a48c
Show file tree
Hide file tree
Showing 22 changed files with 595 additions and 41 deletions.
34 changes: 34 additions & 0 deletions Api/Data/UpdateNotificationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Flekto\Postcode\Api\Data;

/**
* @api
*/
interface UpdateNotificationInterface
{
public const VERSION = 'version';
public const NOTIFIED = 'notified';

/**
* @return string
*/
public function getVersion();

/**
* @param string $version
* @return $this
*/
public function setVersion(string $version);

/**
* @return bool
*/
public function getNotified();

/**
* @param bool $notified
* @return $this
*/
public function setNotified(bool $notified);
}
42 changes: 42 additions & 0 deletions Api/UpdateNotificationRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Flekto\Postcode\Api;

use Flekto\Postcode\Api\Data\UpdateNotificationInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;

/**
* Update notification CRUD interface.
* @api
*/
interface UpdateNotificationRepositoryInterface
{
/**
* @param UpdateNotificationInterface $notification
* @return UpdateNotificationInterface
* @throws LocalizedException
*/
public function save(UpdateNotificationInterface $notification): UpdateNotificationInterface;

/**
* @param string $version
* @return UpdateNotificationInterface
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function getByVersion(string $version): UpdateNotificationInterface;

/**
* @param string $version
* @throws LocalizedException
*/
public function setVersionNotified(string $version): void;

/**
* @param string $version
* @return bool
* @throws LocalizedException
*/
public function isVersionNotified(string $version): bool;
}
95 changes: 74 additions & 21 deletions Block/System/Config/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

use Flekto\Postcode\Helper\StoreConfigHelper;
use Flekto\Postcode\Helper\ApiClientHelper;
use Flekto\Postcode\Helper\Data as DataHelper;
use Flekto\Postcode\Model\UpdateNotification\UpdateNotifier;
use Magento\Backend\Block\Template;
use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Framework\Data\Form\Element\Renderer\RendererInterface;
use Magento\Framework\App\Config\ConfigResource\ConfigInterface;
use Magento\Framework\App\Cache\TypeListInterface as CacheTypeList;
use Magento\Framework\App\Cache\Frontend\Pool as CacheFrontendPool;
use Magento\Framework\Serialize\SerializerInterface;

class Status extends Template implements RendererInterface
{
Expand All @@ -23,8 +26,14 @@ class Status extends Template implements RendererInterface
protected $_resourceConfig;
protected $_cacheTypeList;
protected $_cacheFrontendPool;
protected $_serializer;
protected $_dataHelper;
protected $_updateNotifier;

public array $accountInfo = [];
private $_cachedData;

public array $accountInfo;
public array $moduleInfo;

/**
* @param Template\Context $context
Expand All @@ -33,6 +42,9 @@ class Status extends Template implements RendererInterface
* @param ConfigInterface $resourceConfig
* @param CacheTypeList $cacheTypeList
* @param CacheFrontendPool $cacheFrontendPool
* @param SerializerInterface $serializer
* @param DataHelper $dataHelper
* @param UpdateNotifier $updateNotifier
* @param array $data
*/
public function __construct(
Expand All @@ -42,6 +54,9 @@ public function __construct(
ConfigInterface $resourceConfig,
CacheTypeList $cacheTypeList,
CacheFrontendPool $cacheFrontendPool,
SerializerInterface $serializer,
DataHelper $dataHelper,
UpdateNotifier $updateNotifier,
array $data = []
) {
$this->_scopeConfig = $context->getScopeConfig();
Expand All @@ -50,8 +65,14 @@ public function __construct(
$this->_resourceConfig = $resourceConfig;
$this->_cacheTypeList = $cacheTypeList;
$this->_cacheFrontendPool = $cacheFrontendPool;
$cachedData = $this->_getCachedData();
$this->accountInfo = $cachedData['accountInfo'];
$this->_serializer = $serializer;
$this->_dataHelper = $dataHelper;
$this->_updateNotifier = $updateNotifier;

$this->_cachedData = $this->_getCachedData();

$this->_notifyUpdate();

parent::__construct($context, $data);
}

Expand All @@ -78,15 +99,35 @@ public function render(AbstractElement $element): string
public function getConfig(): array
{
return [
'enabled' => $this->_storeConfigHelper->isSetFlag(StoreConfigHelper::PATH['enabled']),
'module_version' => $this->_storeConfigHelper->getValue(StoreConfigHelper::PATH['module_version']),
'enabled' => $this->_storeConfigHelper->isEnabled(),
'module_version' => $this->_storeConfigHelper->getModuleVersion(),
'supported_countries' => $this->_storeConfigHelper->getSupportedCountries(),
'account_name' => $this->_storeConfigHelper->getValue(StoreConfigHelper::PATH['account_name']),
'account_status' => $this->_storeConfigHelper->getValue(StoreConfigHelper::PATH['account_status']), // Defaults to "new", see etc/config.xml.
'has_credentials' => $this->_storeConfigHelper->hasCredentials(),
];
}

/**
* Get cached account info.
*
* @return array
*/
public function getAccountInfo(): array
{
return $this->_cachedData['accountInfo'] ?? [];
}

/**
* Get cached module info.
*
* @return array
*/
public function getModuleInfo(): array
{
return $this->_cachedData['moduleInfo'] ?? [];
}

/**
* Get short description of API status.
*
Expand All @@ -110,6 +151,27 @@ public function getApiStatusDescription(): string
}
}

/**
* Get cached data.
*
* @return array
*/
private function _getCachedData(): array
{
$cache = $this->_cacheFrontendPool->get(\Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER);
$cachedData = $cache->load(self::CACHE_ID);

if ($cachedData === false) {
$data = [];
$data['accountInfo'] = $this->_getAccountInfo();
$data['moduleInfo'] = $this->_dataHelper->getModuleInfo();
$cache->save($this->_serializer->serialize($data), self::CACHE_ID, [], self::CACHE_LIFETIME_SECONDS);
return $data;
}

return $this->_serializer->unserialize($cachedData);
}

/**
* Get Postcode.eu API account info.
*
Expand All @@ -118,30 +180,21 @@ public function getApiStatusDescription(): string
private function _getAccountInfo(): array
{
$status = $this->_storeConfigHelper->getValue(StoreConfigHelper::PATH['account_status']);
if ($status === \Flekto\Postcode\Helper\ApiClientHelper::API_ACCOUNT_STATUS_ACTIVE)
if ($status === \Flekto\Postcode\Helper\ApiClientHelper::API_ACCOUNT_STATUS_ACTIVE) {
return $this->_apiClientHelper->getApiClient()->accountInfo();
}

return [];
}

/**
* Get cached data.
*
* @return array
* Set a notification if an update is available.
*/
private function _getCachedData(): array
private function _notifyUpdate(): void
{
$cache = $this->_cacheFrontendPool->get(\Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER);
$cachedData = $cache->load(self::CACHE_ID);

if ($cachedData === false)
{
$data = [];
$data['accountInfo'] = $this->_getAccountInfo();
$cache->save(serialize($data), self::CACHE_ID, [], self::CACHE_LIFETIME_SECONDS);
return $data;
$moduleInfo = $this->getModuleInfo();
if ($moduleInfo['has_update'] ?? false) {
$this->_updateNotifier->notifyVersion($moduleInfo['latest_version']);
}

return unserialize($cachedData);
}
}
49 changes: 49 additions & 0 deletions Cron/NotifyModuleUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Flekto\Postcode\Cron;

use Psr\Log\LoggerInterface;
use Flekto\Postcode\Helper\Data as DataHelper;
use Flekto\Postcode\Model\UpdateNotification\UpdateNotifier;

class NotifyModuleUpdate
{
protected $_logger;
protected $_dataHelper;
protected $_updateNotifier;

/**
* Constructor
*
* @access public
* @param LoggerInterface $logger
* @param DataHelper $dataHelper
* @param UpdateNotifier $updateNotifier
* @return void
*/
public function __construct(
LoggerInterface $logger,
DataHelper $dataHelper,
UpdateNotifier $updateNotifier,
) {
$this->_logger = $logger;
$this->_dataHelper = $dataHelper;
$this->_updateNotifier = $updateNotifier;
}

/**
* Run cron job.
*
* @access public
* @return void
*/
public function execute(): void
{
$moduleInfo = $this->_dataHelper->getModuleInfo();
if (($moduleInfo['has_update'] ?? false)
&& $this->_updateNotifier->notifyVersion($moduleInfo['latest_version'])
) {
$this->_logger->info(__('Added notification for Postcode.eu Address API %1 update.', $moduleInfo['latest_version']));
}
}
}
2 changes: 1 addition & 1 deletion Cron/UpdateApiData.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class UpdateApiData
protected $_storeConfigHelper;

/**
* __construct function.
* Constructor
*
* @access public
* @param LoggerInterface $logger
Expand Down
Loading

0 comments on commit 996a48c

Please sign in to comment.