Skip to content

Commit 10bb904

Browse files
committed
Code refactor
1 parent d67ce9e commit 10bb904

File tree

10 files changed

+106
-76
lines changed

10 files changed

+106
-76
lines changed

Api/GetModuleVersionInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66

77
namespace Magefan\Community\Api;
88

9+
/**
10+
* Return module version by module name
11+
*
12+
* @api
13+
* @since 2.1.0
14+
*/
915
interface GetModuleVersionInterface
1016
{
1117
/**

Block/Adminhtml/System/Config/Form/ExtensionsInfo.php

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Framework\Data\Form\Element\AbstractElement;
1010
use Magento\Framework\Module\ModuleListInterface;
1111
use Magento\Backend\Block\Template\Context;
12+
use Magefan\Community\Api\GetModuleVersionInterface;
1213

1314
class ExtensionsInfo extends Field
1415
{
@@ -17,19 +18,27 @@ class ExtensionsInfo extends Field
1718
*/
1819
private $moduleList;
1920

21+
/**
22+
* @var GetModuleVersionInterface
23+
*/
24+
private $getModuleVersion;
25+
2026
/**
2127
* ExtensionsInfo constructor.
2228
* @param Context $context
2329
* @param ModuleListInterface $moduleList
30+
* @param GetModuleVersionInterface $getModuleVersion
2431
* @param array $data
2532
*/
2633
public function __construct(
2734
Context $context,
2835
ModuleListInterface $moduleList,
36+
GetModuleVersionInterface $getModuleVersion,
2937
array $data = []
3038
) {
3139
parent::__construct($context, $data);
3240
$this->moduleList = $moduleList;
41+
$this->getModuleVersion = $getModuleVersion;
3342
}
3443

3544
/**
@@ -44,64 +53,54 @@ public function render(AbstractElement $element)
4453
}
4554

4655
$lists = [
47-
'new_versions' => __('New Version Available'),
48-
'up_to_date' => __('Up-to-Date Magefan Extensions'),
49-
'available' => __('Available Magefan Extensions'),
56+
'need_update' => __('Extensions to Update'),
57+
'up_to_date' => __('Up-to-Date Extensions'),
58+
'new_extensions' => __('Available NEW Extensions'),
5059
];
5160

5261
$html = '';
53-
foreach ($lists as $key => $lable) {
54-
$html .= '<strong>' . $this->escapeHtml($lable) . '</strong>';
62+
foreach ($lists as $listKey => $lable) {
63+
$html .= '<h3>' . $this->escapeHtml($lable) . '</h3>';
5564
$html .= '<table class="data-grid">';
5665
$html .= '<thead>';
5766
$html .= '<tr>';
58-
$html .= '<th class="data-grid-th">' . $this->escapeHtml(__('Product Name')) . '</th>';
67+
$html .= '<th class="data-grid-th">' . $this->escapeHtml(__('Extension')) . '</th>';
5968
$html .= '<th class="data-grid-th">' . $this->escapeHtml(__('Version')) . '</th>';
6069
$html .= '<th class="data-grid-th">' . $this->escapeHtml(__('Change Log')) . '</th>';
6170
$html .= '<th class="data-grid-th">' . $this->escapeHtml(__('User Guide')) . '</th>';
6271
$html .= '</tr>';
6372
$html .= '</thead>';
6473
$html .= '<tbody class="magefan-section">';
6574

66-
foreach ($products as $key => $product) {
67-
$module = $this->moduleList->getOne('Magefan_' . $key);
68-
$continue = false;
69-
70-
switch ($key) {
71-
case 'new_versions':
72-
$version = $module['setup_version'];
73-
if (!$module) {
74-
$continue = true;
75-
}
76-
break;
77-
case 'up_to_date':
78-
$version = $module['setup_version'];
79-
if (!$module) {
80-
$continue = true;
81-
}
82-
if ($product->version != $module['setup_version']) {
83-
$continue = true;
84-
}
85-
break;
86-
case 'available':
87-
$version = $product->version;
88-
if ($module) {
89-
$continue = true;
90-
}
91-
break;
92-
default:
93-
$version = '';
94-
}
75+
foreach ($products as $productKey => $product) {
76+
77+
$moduleName = 'Magefan_' . $productKey;
78+
$module = $this->moduleList->getOne($moduleName);
9579

96-
if ($continue) {
80+
if ((!$module && $listKey != 'new_extensions') || ($module && $listKey == 'new_extensions')) {
81+
continue;
82+
}
83+
if ($listKey == 'up_to_date' && version_compare($this->getModuleVersion->execute($moduleName), $product->version) < 0) {
84+
continue;
85+
}
86+
if ($listKey == 'need_update' && version_compare($this->getModuleVersion->execute($moduleName), $product->version) >= 0) {
9787
continue;
9888
}
9989

90+
if ($listKey == 'need_update') {
91+
$version = $this->getModuleVersion->execute($moduleName) . ' -> ' . $product->version;
92+
} elseif ($listKey == 'new_extensions') {
93+
$version = $product->version;
94+
} else {
95+
$version = $this->getModuleVersion->execute($moduleName);
96+
}
97+
98+
10099
$html .= '<tr>';
101-
$html .= '<td><a href="' . $this->escapeHtml($product->product_url) . '">' . $this->escapeHtml($product->product_name) . '</a></td>';
102-
$html .= '<td>' . $this->escapeHtml($product->version) . '</td>';
103-
$html .= '<td><a href="' . $this->escapeHtml($product->change_log_url) . '">' . $this->escapeHtml(__('Change Log')) . '</a></td>';
104-
$html .= '<td><a href="' . $this->escapeHtml($product->documentation_url) . '">'. $this->escapeHtml(__('User Guide')). '</a></td>';
100+
$html .= '<td><a target="_blank" href="' . $this->escapeHtml($product->product_url) . '">' . $this->escapeHtml($product->product_name) . '</a></td>';
101+
$html .= '<td>' . $this->escapeHtml($version) . '</td>';
102+
$html .= '<td><a target="_blank" href="' . $this->escapeHtml($product->change_log_url) . '">' . $this->escapeHtml(__('Change Log')) . '</a></td>';
103+
$html .= '<td><a target="_blank" href="' . $this->escapeHtml($product->documentation_url) . '">'. $this->escapeHtml(__('User Guide')). '</a></td>';
105104
$html .= '</tr>';
106105
}
107106

Block/Adminhtml/System/Config/Form/Info.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
namespace Magefan\Community\Block\Adminhtml\System\Config\Form;
88

9-
use Magento\Store\Model\ScopeInterface;
109
use Magefan\Community\Api\GetModuleVersionInterface;
1110

1211
/**
@@ -22,19 +21,20 @@ class Info extends \Magento\Config\Block\System\Config\Form\Field
2221
/**
2322
* @var GetModuleVersionInterface
2423
*/
25-
private $getModuleVersion;
24+
protected $getModuleVersion;
2625

2726
/**
27+
* Info constructor.
2828
* @param \Magento\Framework\Module\ModuleListInterface $moduleList
2929
* @param \Magento\Backend\Block\Template\Context $context
30-
* @param GetModuleVersionInterface|null $getModuleVersion
3130
* @param array $data
31+
* @param GetModuleVersionInterface|null $getModuleVersion
3232
*/
3333
public function __construct(
3434
\Magento\Framework\Module\ModuleListInterface $moduleList,
3535
\Magento\Backend\Block\Template\Context $context,
36-
GetModuleVersionInterface $getModuleVersion = null,
37-
array $data = []
36+
array $data = [],
37+
GetModuleVersionInterface $getModuleVersion = null
3838
) {
3939
parent::__construct($context, $data);
4040
$this->moduleList = $moduleList;
@@ -53,9 +53,9 @@ public function render(\Magento\Framework\Data\Form\Element\AbstractElement $ele
5353
$useUrl = \Magefan\Community\Model\UrlChecker::showUrl($this->getUrl());
5454
$version = $this->getModuleVersion->execute($this->getModuleName());
5555
$html = '<div style="padding:10px;background-color:#f8f8f8;border:1px solid #ddd;margin-bottom:7px;">
56-
' . $this->getModuleTitle() . ' v' . $version . ' was developed by ';
56+
' . $this->escapeHtml($this->getModuleTitle()) . ' v' . $this->escapeHtml($version) . ' was developed by ';
5757
if ($useUrl) {
58-
$html .= '<a href="' . $this->getModuleUrl() . '" target="_blank">Magefan</a>';
58+
$html .= '<a href="' . $this->escapeHtml($this->getModuleUrl()) . '" target="_blank">Magefan</a>';
5959
} else {
6060
$html .= '<strong>Magefan</strong>';
6161
}

Block/Adminhtml/System/Config/Form/ProductKeyField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ public function render(\Magento\Framework\Data\Form\Element\AbstractElement $ele
3939
return '';
4040
}
4141
}
42-
}
42+
}

Model/AdminNotificationFeed.php

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Magefan\Community\Model;
88

9-
use Magento\Framework\Config\ConfigOptionsListConstants;
9+
use Magefan\Community\Api\GetModuleVersionInterface;
1010

1111
/**
1212
* Class AdminNotificationFeed
@@ -40,22 +40,28 @@ class AdminNotificationFeed extends \Magento\AdminNotification\Model\Feed
4040
protected $config;
4141

4242
/**
43+
* @var GetModuleVersionInterface
44+
*/
45+
private $getModuleVersion;
46+
47+
/**
48+
* AdminNotificationFeed constructor.
4349
* @param \Magento\Framework\Model\Context $context
4450
* @param \Magento\Framework\Registry $registry
4551
* @param \Magento\Backend\App\ConfigInterface $backendConfig
46-
* @param InboxFactory $inboxFactory
52+
* @param \Magento\AdminNotification\Model\InboxFactory $inboxFactory
4753
* @param \Magento\Backend\Model\Auth\Session $backendAuthSession
4854
* @param \Magento\Framework\Module\ModuleListInterface $moduleList
49-
* @param \Magento\Framework\Module\Manager $moduleManager,
55+
* @param \Magento\Framework\Module\Manager $moduleManager
5056
* @param \Magento\Framework\HTTP\Adapter\CurlFactory $curlFactory
5157
* @param \Magento\Framework\App\DeploymentConfig $deploymentConfig
5258
* @param \Magento\Framework\App\ProductMetadataInterface $productMetadata
5359
* @param \Magento\Framework\UrlInterface $urlBuilder
60+
* @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
61+
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
5462
* @param Config $config
55-
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
56-
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
63+
* @param GetModuleVersionInterface $getModuleVersion
5764
* @param array $data
58-
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
5965
*/
6066
public function __construct(
6167
\Magento\Framework\Model\Context $context,
@@ -71,14 +77,16 @@ public function __construct(
7177
\Magento\Framework\UrlInterface $urlBuilder,
7278
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
7379
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
74-
Config $config = null,
80+
Config $config,
81+
GetModuleVersionInterface $getModuleVersion,
7582
array $data = []
7683
) {
7784
parent::__construct($context, $registry, $backendConfig, $inboxFactory, $curlFactory, $deploymentConfig, $productMetadata, $urlBuilder, $resource, $resourceCollection, $data);
7885
$this->_backendAuthSession = $backendAuthSession;
7986
$this->_moduleList = $moduleList;
8087
$this->_moduleManager = $moduleManager;
81-
$this->config = $config ?: \Magento\Framework\App\ObjectManager::getInstance()->get(Config::class);
88+
$this->config = $config;
89+
$this->getModuleVersion = $getModuleVersion;
8290
}
8391

8492
/**
@@ -96,9 +104,9 @@ public function getFeedUrl()
96104
$domain = isset($urlInfo['host']) ? $urlInfo['host'] : '';
97105
$url = $this->_feedUrl . 'domain/' . urlencode($domain);
98106
$modulesParams = [];
99-
foreach ($this->getMagefanModules() as $key => $module) {
100-
$key = str_replace('Magefan_', '', $key);
101-
$modulesParams[] = $key . ',' . $module['setup_version'];
107+
foreach ($this->getMagefanModules() as $moduleName => $module) {
108+
$key = str_replace('Magefan_', '', $moduleName);
109+
$modulesParams[] = $key . ',' . $this->getModuleVersion->execute($moduleName);
102110
}
103111
if (count($modulesParams)) {
104112
$url .= '/modules/'.base64_encode(implode(';', $modulesParams));
@@ -113,7 +121,6 @@ public function getFeedUrl()
113121
if (count($notificationsParams)) {
114122
$url .= '/notifications/' . base64_encode(implode(';', $notificationsParams));
115123
}
116-
117124
return $url;
118125
}
119126

@@ -199,7 +206,10 @@ public function getFeedData()
199206
}
200207

201208
if (!$getNotification) {
202-
return new \SimpleXMLElement('<?xml version="1.0" encoding="utf-8" ?>');
209+
return new \SimpleXMLElement('<?xml version="1.0" encoding="utf-8" ?>
210+
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
211+
<channel></channel>
212+
</rss>');
203213
}
204214

205215
return parent::getFeedData();

Model/GetModuleVersion.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class GetModuleVersion implements GetModuleVersionInterface
3636
*/
3737
private $moduleList;
3838

39+
/**
40+
* @var array
41+
*/
42+
private $versions = [];
43+
3944
/**
4045
* GetModuleVersion constructor.
4146
* @param SerializerInterface $serializer
@@ -56,19 +61,28 @@ public function __construct(
5661
}
5762

5863
/**
59-
* @inheritDoc
64+
* @param string $moduleName
65+
* @return string
6066
*/
6167
public function execute(string $moduleName): string
6268
{
63-
$fileDir = $this->moduleReader->getModuleDir('', $moduleName) . '/composer.json';
64-
$data = $this->file->read($fileDir);
65-
$data = $this->serializer->unserialize($data);
66-
67-
if (empty($data['version'])) {
69+
if (!isset($this->versions[$moduleName])) {
6870
$module = $this->moduleList->getOne($moduleName);
69-
return $module['setup_version'] ? $module['setup_version'] : '0.0.0';
71+
if (!$module) {
72+
$this->versions[$moduleName] = '';
73+
} else {
74+
$fileDir = $this->moduleReader->getModuleDir('', $moduleName) . '/composer.json';
75+
$data = $this->file->read($fileDir);
76+
$data = $this->serializer->unserialize($data);
77+
78+
if (empty($data['version'])) {
79+
return !empty($module['setup_version']) ? $module['setup_version'] : '';
80+
}
81+
82+
$this->versions[$moduleName] = !empty($data['version']) ? $data['version'] : '';
83+
}
7084
}
7185

72-
return $data['version'];
86+
return $this->versions[$moduleName];
7387
}
7488
}

Model/UrlChecker.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class UrlChecker
1414
/**
1515
* @return bool
1616
*/
17-
static public function showUrl($url)
17+
public static function showUrl($url)
1818
{
1919
$url = (string)$url;
2020
$info = parse_url($url);
@@ -31,4 +31,4 @@ static public function showUrl($url)
3131
return (false === strpos($url, strrev('otnegam')))
3232
&& !is_numeric($part);
3333
}
34-
}
34+
}

Plugin/Magento/Backend/Model/Menu/BuilderPlugin.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ public function afterGetResult(Builder $subject, Menu $menu, $result)
120120

121121
unset($this->configSections['Magefan_Community']);
122122

123-
foreach ($this->magefanModules as $moduleName)
124-
{
123+
foreach ($this->magefanModules as $moduleName) {
125124
$section = $this->getConfigSections($moduleName);
126125
if ($section) {
127126
$item = $this->menuItemFactory->create([

etc/adminhtml/system.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
<label>Extensions &amp; Notifications</label>
1616
<tab>magefan</tab>
1717
<resource>Magefan_Community::config_section</resource>
18-
<group id="installed" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
19-
<label>Installed Extensions</label>
18+
<group id="magefan_extensions" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
19+
<label>Magefan Extensions</label>
2020
<attribute type="expanded">1</attribute>
2121
<field id="extensions_info" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
2222
<label>Extensions Info</label>

view/adminhtml/web/css/source/_module.less

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
background-size: auto 20px;
66
background-repeat: no-repeat;
77
padding-left: 25px;
8-
margin-left: 10px;
8+
margin-left: 15px;
99
}
1010

1111
.admin__menu .level-0[data-ui-id^=menu-magefan-community-elements] > a:before {
@@ -16,6 +16,8 @@
1616
margin-left: 25px;
1717
}
1818

19+
.admin__menu .level-0[data-ui-id^=menu-magefan-community-elements] .submenu-group-title span:after {display:none}
20+
1921
.config-nav .magefan-tab .admin__page-nav-title:before {
2022
content: '';
2123
background-image: url('Magefan_Community::images/logo-config-section.png');

0 commit comments

Comments
 (0)