Skip to content

Commit

Permalink
Merge pull request #84 from JerrySmidt/master
Browse files Browse the repository at this point in the history
3.1.11
  • Loading branch information
JerrySmidt authored Aug 16, 2023
2 parents e864bf8 + 494e62f commit 2e02ff3
Show file tree
Hide file tree
Showing 30 changed files with 703 additions and 45 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;
}
115 changes: 112 additions & 3 deletions Block/System/Config/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,76 @@
namespace Flekto\Postcode\Block\System\Config;

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
{
public const CACHE_ID = 'postcode-eu-status';
public const CACHE_LIFETIME_SECONDS = 3600;

protected $_template = 'Flekto_Postcode::system/config/status.phtml';
protected $_scopeConfig;
protected $_storeConfigHelper;
protected $_apiClientHelper;
protected $_resourceConfig;
protected $_cacheTypeList;
protected $_cacheFrontendPool;
protected $_serializer;
protected $_dataHelper;
protected $_updateNotifier;

private $_cachedData;

public array $accountInfo;
public array $moduleInfo;

/**
* @param Template\Context $context
* @param StoreConfigHelper $storeConfigHelper
* @param ApiClientHelper $apiClientHelper
* @param ConfigInterface $resourceConfig
* @param CacheTypeList $cacheTypeList
* @param CacheFrontendPool $cacheFrontendPool
* @param SerializerInterface $serializer
* @param DataHelper $dataHelper
* @param UpdateNotifier $updateNotifier
* @param array $data
*/
public function __construct(
Template\Context $context,
StoreConfigHelper $storeConfigHelper,
ApiClientHelper $apiClientHelper,
ConfigInterface $resourceConfig,
CacheTypeList $cacheTypeList,
CacheFrontendPool $cacheFrontendPool,
SerializerInterface $serializer,
DataHelper $dataHelper,
UpdateNotifier $updateNotifier,
array $data = []
) {
$this->_scopeConfig = $context->getScopeConfig();
$this->_storeConfigHelper = $storeConfigHelper;
$this->_apiClientHelper = $apiClientHelper;
$this->_resourceConfig = $resourceConfig;
$this->_cacheTypeList = $cacheTypeList;
$this->_cacheFrontendPool = $cacheFrontendPool;
$this->_serializer = $serializer;
$this->_dataHelper = $dataHelper;
$this->_updateNotifier = $updateNotifier;

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

$this->_notifyUpdate();

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

Expand All @@ -57,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 @@ -88,4 +150,51 @@ public function getApiStatusDescription(): string
throw new Status\Exception(__('Invalid account status value.'));
}
}

/**
* 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.
*
* @return array
*/
private function _getAccountInfo(): array
{
$status = $this->_storeConfigHelper->getValue(StoreConfigHelper::PATH['account_status']);
if ($status === \Flekto\Postcode\Helper\ApiClientHelper::API_ACCOUNT_STATUS_ACTIVE) {
return $this->_apiClientHelper->getApiClient()->accountInfo();
}

return [];
}

/**
* Set a notification if an update is available.
*/
private function _notifyUpdate(): void
{
$moduleInfo = $this->getModuleInfo();
if ($moduleInfo['has_update'] ?? false) {
$this->_updateNotifier->notifyVersion($moduleInfo['latest_version']);
}
}
}
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 2e02ff3

Please sign in to comment.