Skip to content

Commit

Permalink
Merge pull request #186 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 ve…
  • Loading branch information
nmalevanec authored Nov 23, 2023
2 parents 8bf69d0 + df47d0e commit 7b25317
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 161 deletions.
79 changes: 0 additions & 79 deletions Block/System/Config/Form/Field/LatestVersion.php

This file was deleted.

55 changes: 0 additions & 55 deletions Block/System/Config/Form/Field/Version.php

This file was deleted.

77 changes: 77 additions & 0 deletions Block/System/Config/Form/Field/Versions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?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\InstalledModulesProvider;
use Bold\Checkout\Model\ModuleInfo\LatestModuleVersionProvider;
use Bold\Checkout\Model\ModuleInfo\ModuleComposerNameProvider;
use Bold\Checkout\Model\ModuleInfo\ModuleComposerVersionProvider;
use Magento\Backend\Block\Template\Context;
use Magento\Framework\Data\Form\Element\AbstractElement;

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

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

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

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

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

/**
* @inheritDoc
*/
public function render(AbstractElement $element)
{
$result = '';
foreach ($this->installedModulesProvider->getModuleList() as $module) {
$element->setLabel(__('%1 version', $module));
$element->setHtmlId('checkout_bold_module_version_' . strtolower($module));
$currentVersion = $this->moduleVersionProvider->getVersion($module);
$latestVersion = $this->latestModuleVersionProvider->getVersion($module);
$text = $currentVersion;
if (version_compare($latestVersion, $currentVersion, '>')) {
$text .= ' (' . __('update available') . ')';
}
$element->setText($text);
$result .= parent::render($element);
}

return $result;
}
}
18 changes: 10 additions & 8 deletions Model/ModuleInfo/LatestModuleVersionUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem\Driver\File;
use Magento\Framework\Module\Dir\Reader;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\Shell;

/**
Expand Down Expand Up @@ -50,7 +47,8 @@ public function __construct(
Shell $shell,
DirectoryList $directoryList,
ModuleComposerNameProvider $composerNameProvider
) {
)
{
$this->config = $config;
$this->shell = $shell;
$this->directoryList = $directoryList;
Expand All @@ -64,14 +62,12 @@ public function __construct(
* @return void
* @throws LocalizedException
*/
public function update(string $moduleName): string
public function update(string $moduleName): void
{
$latestVersion = $this->getLatestModuleVersion($moduleName);
if ($latestVersion) {
$this->config->setLatestModuleVersion($moduleName, $latestVersion);
}

return $latestVersion;
}

/**
Expand All @@ -86,7 +82,13 @@ private function getLatestModuleVersion(string $moduleName): ?string
$moduleName = $this->composerNameProvider->getName($moduleName);
$rootDirectory = $this->directoryList->getRoot();
$command = sprintf(self::COMMAND_TEMPLATE, $moduleName, $rootDirectory);
$result = $this->shell->execute($command);
try {
file_put_contents('/var/www/html/var/log/composer.log', $command . PHP_EOL);
$result = $this->shell->execute($command);
} catch (\Exception $exception) {
// Composer or repository are unavailable.
return null;
}

return preg_match(self::LATEST_PATTERN, $result, $matches)
? $matches[1]
Expand Down
3 changes: 2 additions & 1 deletion Model/Queue/Handler/Module/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public function handle(array $moduleNames): void

foreach ($moduleNames as $moduleName) {
$lastCheckedVersion = $this->config->getLatestModuleVersion($moduleName);
$latestVersion = $this->latestModuleVersionUpdater->update($moduleName);
$this->latestModuleVersionUpdater->update($moduleName);
$latestVersion = $this->config->getLatestModuleVersion($moduleName);
$currentVersion = $this->moduleVersionProvider->getVersion($moduleName);
$composerName = $this->moduleComposerNameProvider->getName($moduleName);
if (
Expand Down
10 changes: 0 additions & 10 deletions etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,4 @@
<argument name="client" xsi:type="object">Bold\Checkout\Model\Http\BoldClient</argument>
</arguments>
</type>
<virtualType name="Bold\Checkout\Block\System\Config\Form\Field\LatestVersion\Checkout" type="Bold\Checkout\Block\System\Config\Form\Field\LatestVersion">
<arguments>
<argument name="moduleName" xsi:type="string">Bold_Checkout</argument>
</arguments>
</virtualType>
<virtualType name="Bold\Checkout\Block\System\Config\Form\Field\Version\Checkout" type="Bold\Checkout\Block\System\Config\Form\Field\Version">
<arguments>
<argument name="moduleName" xsi:type="string">Bold_Checkout</argument>
</arguments>
</virtualType>
</config>
15 changes: 7 additions & 8 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,6 @@
<comment><![CDATA[In case the status is 'Not Found', please re-save configuration.
Otherwise, if the status is not 'Active', open the 'System' -> 'Integrations' grid, find the Integration named '{{integrationName}}' and Activate or Reauthorize it.]]></comment>
</field>
<field id="version" translate="label" type="note" sortOrder="1000" showInDefault="1" showInWebsite="1">
<label>Module Version</label>
<frontend_model>Bold\Checkout\Block\System\Config\Form\Field\Version\Checkout</frontend_model>
</field>
<field id="latest_version" translate="label" type="note" sortOrder="1010" showInDefault="1" showInWebsite="1">
<label>Latest Available Module Version</label>
<frontend_model>Bold\Checkout\Block\System\Config\Form\Field\LatestVersion\Checkout</frontend_model>
</field>
</group>
<group id="bold_checkout_life_elements" translate="label" sortOrder="105" showInDefault="1" showInWebsite="1">
<label>Bold Checkout Integration (LiFE) Elements</label>
Expand Down Expand Up @@ -151,6 +143,13 @@
<comment><![CDATA[Check for Bold modules new version availability.]]></comment>
</field>
</group>
<group id="bold_module_version" translate="label" sortOrder="1000" showInDefault="1" showInWebsite="1">
<label>Bold Commerce Module Versions</label>
<field id="version" translate="label" type="note" sortOrder="1000" showInDefault="1" showInWebsite="1">
<label>Module Version</label>
<frontend_model>Bold\Checkout\Block\System\Config\Form\Field\Versions</frontend_model>
</field>
</group>
</section>
</system>
</config>

0 comments on commit 7b25317

Please sign in to comment.