Skip to content

Commit

Permalink
Merge pull request #183 from bold-commerce/Q1-472
Browse files Browse the repository at this point in the history
A merchant should be shown a notification in m2 when there's a new version of the module
  • Loading branch information
nmalevanec authored Nov 22, 2023
2 parents 07bae2f + c19d7cf commit 0252a2c
Show file tree
Hide file tree
Showing 24 changed files with 737 additions and 12 deletions.
56 changes: 56 additions & 0 deletions Block/System/Config/Form/Field/LatestVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Bold\Checkout\Block\System\Config\Form\Field;

use Bold\Checkout\Block\System\Config\Form\Field;
use Bold\Checkout\Model\ModuleInfo\LatestModuleVersionProvider;
use Bold\Checkout\Model\ModuleInfo\ModuleComposerVersionProvider;
use Magento\Backend\Block\Template\Context;
use Magento\Framework\Data\Form\Element\AbstractElement;

/**
* Module latest available version field.
*/
class LatestVersion extends Field
{
protected $unsetScope = true;

/**
* @var string
*/
private $moduleName;

/**
* @var LatestModuleVersionProvider
*/
private $latestModuleVersionProvider;

/**
* @param Context $context
* @param ModuleComposerVersionProvider $moduleVersionProvider
* @param array $data
*/
public function __construct(
Context $context,
LatestModuleVersionProvider $latestModuleVersionProvider,
string $moduleName = '',
array $data = []
) {
parent::__construct($context, $data);
$this->moduleName = $moduleName;
$this->latestModuleVersionProvider = $latestModuleVersionProvider;
}

/**
* @inheritDoc
*/
protected function _renderValue(AbstractElement $element)
{
$version = $this->latestModuleVersionProvider->getVersion($this->moduleName);
$element->setText($version);

return parent::_renderValue($element);
}
}
24 changes: 16 additions & 8 deletions Block/System/Config/Form/Field/Version.php
Original file line number Diff line number Diff line change
@@ -1,45 +1,53 @@
<?php

declare(strict_types=1);

namespace Bold\Checkout\Block\System\Config\Form\Field;

use Bold\Checkout\Block\System\Config\Form\Field;
use Bold\Checkout\Model\ModuleVersionProvider;
use Bold\Checkout\Model\ModuleInfo\ModuleComposerVersionProvider;
use Magento\Backend\Block\Template\Context;
use Magento\Framework\Data\Form\Element\AbstractElement;

/**
* Bold Integration version field.
* Bold Integration module version field.
*/
class Version extends Field
{
protected $unsetScope = true;

/**
* @var ModuleVersionProvider
* @var ModuleComposerVersionProvider
*/
private $moduleVersionProvider;

/**
* @var string
*/
private $moduleName;

/**
* @param Context $context
* @param ModuleVersionProvider $moduleVersionProvider
* @param ModuleComposerVersionProvider $moduleVersionProvider
* @param array $data
*/
public function __construct(
Context $context,
ModuleVersionProvider $moduleVersionProvider,
array $data = []
Context $context,
ModuleComposerVersionProvider $moduleVersionProvider,
string $moduleName = '',
array $data = []
) {
parent::__construct($context, $data);
$this->moduleVersionProvider = $moduleVersionProvider;
$this->moduleName = $moduleName;
}

/**
* @inheritDoc
*/
protected function _renderValue(AbstractElement $element)
{
$version = $this->moduleVersionProvider->getVersion('Bold_Checkout');
$version = $this->moduleVersionProvider->getVersion($this->moduleName);
$element->setText($version);

return parent::_renderValue($element);
Expand Down
56 changes: 56 additions & 0 deletions Cron/GetLatestModuleVersions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Bold\Checkout\Cron;

use Bold\Checkout\Model\Config;
use Bold\Checkout\Model\ModuleInfo\InstalledModulesProvider;
use Magento\Framework\MessageQueue\PublisherInterface;

/**
* Cron job to check module updates availability.
*/
class GetLatestModuleVersions
{
/**
* @var PublisherInterface
*/
private $publisher;

/**
* @var InstalledModulesProvider
*/
private $installedModulesProvider;

/**
* @var Config
*/
private $config;

/**
* @param InstalledModulesProvider $installedModulesProvider
* @param PublisherInterface $publisher
* @param Config $config
*/
public function __construct(
InstalledModulesProvider $installedModulesProvider,
PublisherInterface $publisher,
Config $config
) {
$this->installedModulesProvider = $installedModulesProvider;
$this->publisher = $publisher;
$this->config = $config;
}

/**
* Add a queue job.
*
* @return void
*/
public function execute(): void
{
if ($this->config->isUpdatesCheckEnabled()) {
$moduleList = array_unique(array_values($this->installedModulesProvider->getModuleList()));
$this->publisher->publish('bold.checkout.module.check', $moduleList);
}
}
}
11 changes: 11 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Config implements ConfigInterface
private const PATH_INTEGRATION_IDENTITY_URL = 'checkout/bold_checkout_base/integration_identity_url';
private const PATH_LIFE_ELEMENTS = 'checkout/bold_checkout_life_elements/life_elements';
private const PATH_VALIDATE_COUPON_CODES = 'checkout/bold_checkout_advanced/validate_coupon_codes';
private const PATH_UPDATE_CHECK = 'checkout/bold_checkout_advanced/updates_check';

public const INTEGRATION_PATHS = [
self::PATH_INTEGRATION_EMAIL,
Expand Down Expand Up @@ -396,4 +397,14 @@ public function getValidateCouponCodes(int $websiteId): bool
$websiteId
);
}

/**
* @inheritDoc
*/
public function isUpdatesCheckEnabled(): bool
{
return $this->scopeConfig->isSetFlag(
self::PATH_UPDATE_CHECK
);
}
}
7 changes: 7 additions & 0 deletions Model/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,11 @@ public function getLifeElements(int $websiteId): array;
* @return bool
*/
public function getValidateCouponCodes(int $websiteId): bool;

/**
* Check if module update check is available.
*
* @return bool
*/
public function isUpdatesCheckEnabled(): bool;
}
10 changes: 7 additions & 3 deletions Model/GetVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@
namespace Bold\Checkout\Model;

use Bold\Checkout\Api\GetVersionInterface;
use Bold\Checkout\Model\ModuleInfo\ModuleComposerVersionProvider;

/**
* Get module version.
*/
class GetVersion implements GetVersionInterface
{
/**
* @var ModuleVersionProvider
* @var ModuleComposerVersionProvider
*/
private $moduleVersionProvider;

/**
* @param ModuleVersionProvider $moduleVersionProvider
* @param ModuleComposerVersionProvider $moduleVersionProvider
*/
public function __construct(ModuleVersionProvider $moduleVersionProvider) {
public function __construct(ModuleComposerVersionProvider $moduleVersionProvider) {
$this->moduleVersionProvider = $moduleVersionProvider;
}

Expand Down
82 changes: 82 additions & 0 deletions Model/ModuleInfo/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace Bold\Checkout\Model\ModuleInfo;

use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Config\Storage\WriterInterface;

/**
* Get composer latest module version.
*/
class Config
{
private const PATH_MODULE_VERSION = 'checkout/bold_checkout/latest_version_%s';

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var WriterInterface
*/
private $writer;

/**
* @var TypeListInterface
*/
private $cacheTypeList;

/**
* @param ScopeConfigInterface $scopeConfig
* @param WriterInterface $writer
* @param TypeListInterface $cacheTypeList
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
WriterInterface $writer,
TypeListInterface $cacheTypeList
) {
$this->scopeConfig = $scopeConfig;
$this->writer = $writer;
$this->cacheTypeList = $cacheTypeList;
}

/**
* Get composer latest module version.
*
* @param string $module
* @return string
*/
public function getLatestModuleVersion(string $module): string
{
return $this->scopeConfig->getValue($this->getModuleVersionPath($module)) ?? '0.0.0';
}

/**
* Set composer latest module version.
*
* @param string $module
* @param string $version
*/
public function setLatestModuleVersion(string $module, string $version): void
{
$this->writer->save($this->getModuleVersionPath($module), $version);
$this->cacheTypeList->cleanType('config');
$this->scopeConfig->clean();
}

/**
* Generate config path for module version data.
*
* @param string $module
* @return string
*/
private function getModuleVersionPath(string $module): string
{
return sprintf(self::PATH_MODULE_VERSION, strtolower($module));
}
}
35 changes: 35 additions & 0 deletions Model/ModuleInfo/InstalledModulesProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Bold\Checkout\Model\ModuleInfo;

/**
* Get all installed Bold modules.
*/
class InstalledModulesProvider
{
/**
* @var array
*/
private $moduleList;

/**
* @param array $moduleList
*/
public function __construct(
array $moduleList = []
) {
$this->moduleList = $moduleList;
}

/**
* Get installed modules list.
*
* @return string[]
*/
public function getModuleList(): array
{
return $this->moduleList;
}
}
Loading

0 comments on commit 0252a2c

Please sign in to comment.