From 54e7500fca8408e83e0652b87da79f1102d4b14c Mon Sep 17 00:00:00 2001 From: Alexander Dite Date: Fri, 29 Sep 2017 11:34:48 +0200 Subject: [PATCH 01/62] [MAGEHACKMUC-17] add model to read modules system config values --- Model/Config.php | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Model/Config.php diff --git a/Model/Config.php b/Model/Config.php new file mode 100644 index 0000000..e8ca246 --- /dev/null +++ b/Model/Config.php @@ -0,0 +1,67 @@ +config = $config; + } + + /** + * @return bool + */ + public function isEnabled() + { + return $this->config->isSetFlag(self::XML_PATH_ENABLED); + } + + /** + * @return string|null + */ + public function getRemoteSystemUrl() + { + return $this->config->getValue(self::XML_PATH_REMOTE_SYSTEM_URL); + } + + /** + * @return string|null + */ + public function getRemoteSystemApiUser() + { + return $this->config->getValue(self::XML_PATH_API_USER); + } + + /** + * @return string|null + */ + public function getRemoteSystemApiPass() + { + return $this->config->getValue(self::XML_PATH_API_PASS); + } +} \ No newline at end of file From ad73bdcaa7b40fb2158f8acdb94850a85d060a04 Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Fri, 29 Sep 2017 11:36:34 +0200 Subject: [PATCH 02/62] [MAGEHACKMUC-17] First draft of webapi url /V1/systemConfig/all --- .../FetchLocalDataServiceInterface.php | 7 ++++++- Service/FetchLocalDataService.php | 2 +- etc/webapi.xml | 21 +++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 etc/webapi.xml diff --git a/Api/Service/FetchLocalDataServiceInterface.php b/Api/Service/FetchLocalDataServiceInterface.php index 10f7893..9d9c294 100644 --- a/Api/Service/FetchLocalDataServiceInterface.php +++ b/Api/Service/FetchLocalDataServiceInterface.php @@ -9,10 +9,15 @@ namespace Magenerds\SystemDiff\Api\Service; +/** + * Returns the locally configured config data. + * + * @api + */ interface FetchLocalDataServiceInterface { /** - * @return array + * @return mixed */ public function fetch(); } \ No newline at end of file diff --git a/Service/FetchLocalDataService.php b/Service/FetchLocalDataService.php index 7cb4c95..acb92f3 100644 --- a/Service/FetchLocalDataService.php +++ b/Service/FetchLocalDataService.php @@ -30,7 +30,7 @@ public function __construct(DataReaderPool $dataReaderPool) } /** - * @return array + * @return mixed */ public function fetch() { diff --git a/etc/webapi.xml b/etc/webapi.xml new file mode 100644 index 0000000..bcb0e13 --- /dev/null +++ b/etc/webapi.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + From 5390945ccd7507f28553c61fe4747a6df319b7a1 Mon Sep 17 00:00:00 2001 From: Julien Wiedner Date: Fri, 29 Sep 2017 11:58:19 +0200 Subject: [PATCH 03/62] [MAGEHACKMUC-17] Add system.xml and acl.xml --- etc/acl.xml | 22 ++++++++++++++++++++++ etc/adminhtml/system.xml | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 etc/acl.xml create mode 100644 etc/adminhtml/system.xml diff --git a/etc/acl.xml b/etc/acl.xml new file mode 100644 index 0000000..ae767f7 --- /dev/null +++ b/etc/acl.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml new file mode 100644 index 0000000..3001e2e --- /dev/null +++ b/etc/adminhtml/system.xml @@ -0,0 +1,37 @@ + + + + + + +
+ + magenerds + Magenerds_SystemDiff::config_system_diff + + + + + Flag for showing the differences in the backend + Magento\Config\Model\Config\Source\Yesno + + + + + + + Url of the system to compare with + + + + Username of the API of the remote system + + + + Password of the API of the remote system + + +
+
+
\ No newline at end of file From 0577ddacaae5dc376a97069171f2fc0af68f9a11 Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Fri, 29 Sep 2017 14:50:07 +0200 Subject: [PATCH 04/62] [MAGEHACKMUC-17] DataReaderPool di config fixed --- DataReader/DataReaderPool.php | 9 +++++---- etc/di.xml | 3 +-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/DataReader/DataReaderPool.php b/DataReader/DataReaderPool.php index 842f7fc..31c2194 100644 --- a/DataReader/DataReaderPool.php +++ b/DataReader/DataReaderPool.php @@ -23,15 +23,16 @@ class DataReaderPool implements \IteratorAggregate /** * @param TMapFactory $tmapFactory - * @param array $dataReader + * @param array $dataReaders */ public function __construct( TMapFactory $tmapFactory, - array $dataReader = [] - ) { + array $dataReaders = [] + ) + { $this->dataReader = $tmapFactory->create( [ - 'array' => $dataReader, + 'array' => $dataReaders, 'type' => DataReaderInterface::class ] ); diff --git a/etc/di.xml b/etc/di.xml index e67b203..ce0c4ee 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -33,8 +33,7 @@ - Magenerds\SystemDiff\DataReader\StoreConfigDataReader - Magenerds\SystemDiff\DataReader\CmsDataReader + Magenerds\SystemDiff\DataReader\StoreConfigDataReader From 25008f14984b8663be6d141c06ec079709ca2a36 Mon Sep 17 00:00:00 2001 From: Julien Wiedner Date: Fri, 29 Sep 2017 15:44:24 +0200 Subject: [PATCH 05/62] [MAGEHACKMUC-17] Add install script --- Setup/InstallSchema.php | 58 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Setup/InstallSchema.php diff --git a/Setup/InstallSchema.php b/Setup/InstallSchema.php new file mode 100644 index 0000000..9fa4f3d --- /dev/null +++ b/Setup/InstallSchema.php @@ -0,0 +1,58 @@ +startSetup(); + + $table = $setup->getConnection()->newTable( + $setup->getTable('magenerds_systemdiff_diff_config') + )->addColumn( + 'diff_value_id', + \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT, + null, + ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true] + )->addColumn( + 'path', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 255, + ['nullable' => false, 'primary' => false] + )->addColumn( + 'scope', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 8, + ['nullable' => false, 'primary' => false] + )->addColumn( + 'scope_id', + \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, + 8, + ['nullable' => false, 'primary' => false] + )->addColumn( + 'diff_value', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + null, + ['nullable' => false, 'primary' => false] + ); + $setup->getConnection()->createTable($table); + + $setup->endSetup(); + } +} \ No newline at end of file From 6cc9c02368d37655408afdc44eada7b1068ba8ce Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Fri, 29 Sep 2017 15:48:08 +0200 Subject: [PATCH 06/62] [MAGEHACKMUC-17] DataReaderPool di config fixed --- etc/di.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/etc/di.xml b/etc/di.xml index ce0c4ee..070778e 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -32,7 +32,7 @@ - + Magenerds\SystemDiff\DataReader\StoreConfigDataReader From 525bf1dd64ca9c5710a4404883f1517e02ead382 Mon Sep 17 00:00:00 2001 From: Alexander Dite Date: Fri, 29 Sep 2017 15:53:15 +0200 Subject: [PATCH 07/62] [MAGEHACKMUC-17] add model,resourceModel and collection for the diff_config table --- Model/DiffConfig.php | 28 +++++++++++++++++ Model/ResourceModel/DiffConfig.php | 30 +++++++++++++++++++ Model/ResourceModel/DiffConfig/Collection.php | 30 +++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 Model/DiffConfig.php create mode 100644 Model/ResourceModel/DiffConfig.php create mode 100644 Model/ResourceModel/DiffConfig/Collection.php diff --git a/Model/DiffConfig.php b/Model/DiffConfig.php new file mode 100644 index 0000000..3b97dd9 --- /dev/null +++ b/Model/DiffConfig.php @@ -0,0 +1,28 @@ +_init(\Magenerds\SystemDiff\Model\ResourceModel\DiffConfig::class); + } + +} \ No newline at end of file diff --git a/Model/ResourceModel/DiffConfig.php b/Model/ResourceModel/DiffConfig.php new file mode 100644 index 0000000..288043a --- /dev/null +++ b/Model/ResourceModel/DiffConfig.php @@ -0,0 +1,30 @@ +_init('magenerds_systemdiff_diff_config', 'diff_value_id'); + } +} \ No newline at end of file diff --git a/Model/ResourceModel/DiffConfig/Collection.php b/Model/ResourceModel/DiffConfig/Collection.php new file mode 100644 index 0000000..789414c --- /dev/null +++ b/Model/ResourceModel/DiffConfig/Collection.php @@ -0,0 +1,30 @@ +_init(\Magenerds\SystemDiff\Model\DiffConfig::class, + \Magenerds\SystemDiff\Model\ResourceModel\DiffConfig::class + ); + } + +} \ No newline at end of file From a654d5f94ee39992cb359dc174cf2bf5188548e3 Mon Sep 17 00:00:00 2001 From: Florian Sydekum Date: Fri, 29 Sep 2017 15:56:14 +0200 Subject: [PATCH 08/62] [MAGEHACKMUC-17] Refactored store config reader to work with Magento 2.2.0 --- DataReader/StoreConfigDataReader.php | 37 +++++++------------------ etc/di.xml | 40 +++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 30 deletions(-) diff --git a/DataReader/StoreConfigDataReader.php b/DataReader/StoreConfigDataReader.php index 9d5e5b3..b1322af 100644 --- a/DataReader/StoreConfigDataReader.php +++ b/DataReader/StoreConfigDataReader.php @@ -9,49 +9,32 @@ namespace Magenerds\SystemDiff\DataReader; +use Magento\Framework\App\Config\ConfigSourceInterface; + class StoreConfigDataReader implements DataReaderInterface { /** - * @var \Magento\Store\Model\Config\Reader\DefaultReader - */ - private $defaultReader; - /** - * @var \Magento\Store\Model\Config\Reader\Website - */ - private $websiteReader; - /** - * @var \Magento\Store\Model\Config\Reader\Store + * @var ConfigSourceInterface */ - private $storeReader; + private $configSource; /** * StoreConfigDataReader constructor. - * @param \Magento\Store\Model\Config\Reader\DefaultReader $defaultReader - * @param \Magento\Store\Model\Config\Reader\Website $websiteReader - * @param \Magento\Store\Model\Config\Reader\Store $storeReader + * @param ConfigSourceInterface $configSource */ - public function __construct - ( - \Magento\Store\Model\Config\Reader\DefaultReader $defaultReader, - \Magento\Store\Model\Config\Reader\Website $websiteReader, - \Magento\Store\Model\Config\Reader\Store $storeReader + public function __construct( + ConfigSourceInterface $configSource ){ - $this->defaultReader = $defaultReader; - $this->websiteReader = $websiteReader; - $this->storeReader = $storeReader; + $this->configSource = $configSource; } /** * Reads the store configuration from database. * - * @return array + * @return [] */ public function read() { - return [ - 'default' => $this->defaultReader->read(), - 'websites' => $this->websiteReader->read(), - 'stores' => $this->storeReader->read() - ]; + return $this->configSource->get(); } } \ No newline at end of file diff --git a/etc/di.xml b/etc/di.xml index e67b203..1c71d2d 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -32,9 +32,8 @@ - + Magenerds\SystemDiff\DataReader\StoreConfigDataReader - Magenerds\SystemDiff\DataReader\CmsDataReader @@ -46,4 +45,39 @@ - + + + + + + Magento\Config\App\Config\Source\ModularConfigSource + 10 + + + Magento\Config\App\Config\Source\RuntimeConfigSource + 100 + + + systemConfigInitialDataProvider + 1000 + + + + + + + + + Magento\Config\App\Config\Source\DumpConfigSourceAggregated::RULE_TYPE_INCLUDE + Magento\Config\App\Config\Source\DumpConfigSourceAggregated::RULE_TYPE_EXCLUDE + Magento\Config\App\Config\Source\DumpConfigSourceAggregated::RULE_TYPE_EXCLUDE + + + + + + + appDumpConfigSystemSource + + + \ No newline at end of file From 10f34202169e888d92f6b7617c2e46804f1f64df Mon Sep 17 00:00:00 2001 From: Florian Sydekum Date: Fri, 29 Sep 2017 16:38:52 +0200 Subject: [PATCH 09/62] [MAGEHACKMUC-17] Minor fixes for TMap and di --- Differ/DifferPool.php | 16 ++++++++-------- etc/di.xml | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Differ/DifferPool.php b/Differ/DifferPool.php index 0097846..f1f8abc 100644 --- a/Differ/DifferPool.php +++ b/Differ/DifferPool.php @@ -19,19 +19,19 @@ class DifferPool implements \IteratorAggregate /** * @var DifferInterface[] | TMap */ - private $differ; + private $differs; /** * @param TMapFactory $tmapFactory - * @param array $differ + * @param array $differs */ public function __construct( TMapFactory $tmapFactory, - array $differ = [] + array $differs = [] ) { - $this->differ = $tmapFactory->create( + $this->differs = $tmapFactory->create( [ - 'array' => $differ, + 'array' => $differs, 'type' => DifferInterface::class ] ); @@ -46,11 +46,11 @@ public function __construct( */ public function get($differCode) { - if (!isset($this->differ[$differCode])) { + if (!isset($this->differs[$differCode])) { throw new NotFoundException(__('Differ %1 does not exist.', $differCode)); } - return $this->differ[$differCode]; + return $this->differs[$differCode]; } /** @@ -62,6 +62,6 @@ public function get($differCode) */ public function getIterator() { - return $this->differ; + return $this->differs; } } \ No newline at end of file diff --git a/etc/di.xml b/etc/di.xml index 83f2cd4..9caf9bc 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -24,16 +24,16 @@ - - Magenerds\SystemDiff\Differ\StoreConfigDiffer + + Magenerds\SystemDiff\Differ\StoreConfigDiffer - - Magenerds\SystemDiff\DataReader\StoreConfigDataReader + + Magenerds\SystemDiff\DataReader\StoreConfigDataReader From f18bb01c830956711dc806b274b872dcd84ba54d Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Fri, 29 Sep 2017 17:12:01 +0200 Subject: [PATCH 10/62] [MAGEHACKMUC-17] Changed config option to only use access token --- Model/Config.php | 15 +++------------ etc/adminhtml/system.xml | 10 +++------- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/Model/Config.php b/Model/Config.php index e8ca246..261d782 100644 --- a/Model/Config.php +++ b/Model/Config.php @@ -19,8 +19,7 @@ class Config { const XML_PATH_ENABLED = 'system_diff/general/enabled'; const XML_PATH_REMOTE_SYSTEM_URL = 'system_diff/connection/remote_system_url'; - const XML_PATH_API_USER = 'system_diff/connection/api_user'; - const XML_PATH_API_PASS = 'system_diff/connection/api_pw'; + const XML_PATH_ACCESS_TOKEN = 'system_diff/connection/access_token'; /** * @var \Magento\Framework\App\Config\ScopeConfigInterface @@ -52,16 +51,8 @@ public function getRemoteSystemUrl() /** * @return string|null */ - public function getRemoteSystemApiUser() + public function getRemoteSystemAccessToken() { - return $this->config->getValue(self::XML_PATH_API_USER); - } - - /** - * @return string|null - */ - public function getRemoteSystemApiPass() - { - return $this->config->getValue(self::XML_PATH_API_PASS); + return $this->config->getValue(self::XML_PATH_ACCESS_TOKEN); } } \ No newline at end of file diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 3001e2e..442c913 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -23,13 +23,9 @@ Url of the system to compare with - - - Username of the API of the remote system - - - - Password of the API of the remote system + + + Access Token of the integration user on the remote system. This user has to have the checkmark on Stores > Settings > Configuration > SystemDiff Section From 54b0818294543052f987786941c19d70a2ed0006 Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Fri, 29 Sep 2017 17:20:50 +0200 Subject: [PATCH 11/62] [MAGEHACKMUC-17] Updated API definition --- Api/Service/FetchLocalDataServiceInterface.php | 4 +++- etc/webapi.xml | 6 ++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Api/Service/FetchLocalDataServiceInterface.php b/Api/Service/FetchLocalDataServiceInterface.php index 9d9c294..52e77b2 100644 --- a/Api/Service/FetchLocalDataServiceInterface.php +++ b/Api/Service/FetchLocalDataServiceInterface.php @@ -10,13 +10,15 @@ namespace Magenerds\SystemDiff\Api\Service; /** - * Returns the locally configured config data. + * Return locally configured config data. * * @api */ interface FetchLocalDataServiceInterface { /** + * Returns locally configured config data. + * * @return mixed */ public function fetch(); diff --git a/etc/webapi.xml b/etc/webapi.xml index bcb0e13..d510632 100644 --- a/etc/webapi.xml +++ b/etc/webapi.xml @@ -10,12 +10,10 @@ --> - - + - - + From 9c42144c50890871c395b1d185df4bc34d52dd6e Mon Sep 17 00:00:00 2001 From: Julien Wiedner Date: Fri, 29 Sep 2017 17:23:32 +0200 Subject: [PATCH 12/62] [MAGEHACKMUC-17] Change README according to new variables --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a63a47e..8edd685 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ sets. In order to integrate your own data reader and differ, add the following t - + Namespace\Module\Differ\YourOwnDiffer @@ -27,7 +27,7 @@ sets. In order to integrate your own data reader and differ, add the following t - + Namespace\Module\DataReader\YourOwnReader From f47a69a0e1c69f2b7c54d8785fd208adc42efc0a Mon Sep 17 00:00:00 2001 From: Julien Wiedner Date: Sat, 30 Sep 2017 10:21:41 +0200 Subject: [PATCH 13/62] [MAGEHACKMUC-17] Change to correct license --- Setup/InstallSchema.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Setup/InstallSchema.php b/Setup/InstallSchema.php index 9fa4f3d..5acbe7f 100644 --- a/Setup/InstallSchema.php +++ b/Setup/InstallSchema.php @@ -1,8 +1,10 @@ Date: Sat, 30 Sep 2017 10:36:28 +0200 Subject: [PATCH 14/62] [MAGEHACKMUC-17] add interfaces and stubs for services: * SaveDiffToTableService * PerformSystemDiffService --- .../PerformSystemDiffServiceInterface.php | 22 +++++++++++++++ .../SaveDiffToTableServiceInterface.php | 23 +++++++++++++++ Service/PerformSystemDiffService.php | 28 +++++++++++++++++++ Service/SaveDiffToTableService.php | 28 +++++++++++++++++++ etc/di.xml | 6 ++++ 5 files changed, 107 insertions(+) create mode 100644 Api/Service/PerformSystemDiffServiceInterface.php create mode 100644 Api/Service/SaveDiffToTableServiceInterface.php create mode 100644 Service/PerformSystemDiffService.php create mode 100644 Service/SaveDiffToTableService.php diff --git a/Api/Service/PerformSystemDiffServiceInterface.php b/Api/Service/PerformSystemDiffServiceInterface.php new file mode 100644 index 0000000..483b354 --- /dev/null +++ b/Api/Service/PerformSystemDiffServiceInterface.php @@ -0,0 +1,22 @@ + + + + + From 3094f52f3ef91e7d0f5e1d399a9118b3a7b6cad8 Mon Sep 17 00:00:00 2001 From: Florian Sydekum Date: Sat, 30 Sep 2017 10:38:48 +0200 Subject: [PATCH 15/62] [MAGEHACKMUC-17] Changed implementation to fetch store configuration, including sensitive data --- etc/di.xml | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/etc/di.xml b/etc/di.xml index 9caf9bc..43ea885 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -46,13 +46,9 @@ - + - - Magento\Config\App\Config\Source\ModularConfigSource - 10 - Magento\Config\App\Config\Source\RuntimeConfigSource 100 @@ -61,23 +57,17 @@ systemConfigInitialDataProvider 1000 - - - - - - - - Magento\Config\App\Config\Source\DumpConfigSourceAggregated::RULE_TYPE_INCLUDE - Magento\Config\App\Config\Source\DumpConfigSourceAggregated::RULE_TYPE_EXCLUDE - Magento\Config\App\Config\Source\DumpConfigSourceAggregated::RULE_TYPE_EXCLUDE + + Magento\Config\App\Config\Source\EnvironmentConfigSource + 2000 + - appDumpConfigSystemSource + configShowSourceAggregated \ No newline at end of file From 5dd1c9c16ceece40164acb2c1e07eac662d37726 Mon Sep 17 00:00:00 2001 From: Alexander Dite Date: Sat, 30 Sep 2017 10:57:50 +0200 Subject: [PATCH 16/62] [MAGEHACKMUC-17] implement PerformSystemDiffService --- Service/PerformSystemDiffService.php | 45 +++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/Service/PerformSystemDiffService.php b/Service/PerformSystemDiffService.php index bcedb93..1f241d1 100644 --- a/Service/PerformSystemDiffService.php +++ b/Service/PerformSystemDiffService.php @@ -9,7 +9,10 @@ namespace Magenerds\SystemDiff\Service; +use Magenerds\SystemDiff\Api\Service\FetchLocalDataServiceInterface; +use Magenerds\SystemDiff\Api\Service\FetchRemoteDataServiceInterface; use Magenerds\SystemDiff\Api\Service\PerformSystemDiffServiceInterface; +use Magenerds\SystemDiff\Api\Service\SaveDiffToTableServiceInterface; /** * Class PerformSystemDiffService @@ -17,12 +20,52 @@ */ class PerformSystemDiffService implements PerformSystemDiffServiceInterface { + /** + * @var \Magenerds\SystemDiff\Api\Service\FetchLocalDataServiceInterface + */ + protected $fetchLocalDataService; + /** + * @var \Magenerds\SystemDiff\Api\Service\FetchRemoteDataServiceInterface + */ + protected $fetchRemoteDataService; + /** + * @var \Magenerds\SystemDiff\Service\DiffDataService + */ + protected $diffDataService; + /** + * @var \Magenerds\SystemDiff\Api\Service\SaveDiffToTableServiceInterface + */ + private $saveDiffToTableService; + + /** + * PerformSystemDiffService constructor. + * @param \Magenerds\SystemDiff\Api\Service\FetchLocalDataServiceInterface $fetchLocalDataService + * @param \Magenerds\SystemDiff\Api\Service\FetchRemoteDataServiceInterface $fetchRemoteDataService + * @param \Magenerds\SystemDiff\Service\DiffDataService $diffDataService + * @param \Magenerds\SystemDiff\Api\Service\SaveDiffToTableServiceInterface $saveDiffToTableService + */ + public function __construct( + FetchLocalDataServiceInterface $fetchLocalDataService, + FetchRemoteDataServiceInterface $fetchRemoteDataService, + DiffDataService $diffDataService, + SaveDiffToTableServiceInterface $saveDiffToTableService + ) { + $this->fetchLocalDataService = $fetchLocalDataService; + $this->fetchRemoteDataService = $fetchRemoteDataService; + $this->diffDataService = $diffDataService; + $this->saveDiffToTableService = $saveDiffToTableService; + } /** * @return bool */ public function performDiff() { - // TODO: Implement performDiff() method. + $localData = $this->fetchLocalDataService->fetch(); + $remoteData = $this->fetchRemoteDataService->fetch(); + + $diffData = $this->diffDataService->diffData($localData, $remoteData); + + $this->saveDiffToTableService->saveData($diffData); } } \ No newline at end of file From 8871cd14bd830d085e5f859abded96c9a9ae8694 Mon Sep 17 00:00:00 2001 From: Florian Sydekum Date: Sat, 30 Sep 2017 11:04:59 +0200 Subject: [PATCH 17/62] [MAGEHACKMUC-17] Refactored store configuration differ in order to diff the data format provided --- Differ/StoreConfigDiffer.php | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/Differ/StoreConfigDiffer.php b/Differ/StoreConfigDiffer.php index d3a9344..bcb662f 100644 --- a/Differ/StoreConfigDiffer.php +++ b/Differ/StoreConfigDiffer.php @@ -11,6 +11,11 @@ class StoreConfigDiffer extends AbstractDiffer { + /** + * Holds the data reader code for the store configuration + */ + const DATA_READER_CODE = 'storeConfig'; + /** * Diffs two data sets of two systems. * @@ -33,7 +38,7 @@ public function diff(array $localData, array $remoteData) $remoteConfig['websites'] = $this->flattenArray($remoteData['websites'], ''); $remoteConfig['stores'] = $this->flattenArray($remoteData['stores'], ''); - $diff = array(); + $diff = []; $diff['default'] = $this->diffArrays($localConfig['default'], $remoteConfig['default']); $diff['websites'] = $this->diffArrays($localConfig['websites'], $remoteConfig['websites']); $diff['stores'] = $this->diffArrays($localConfig['stores'], $remoteConfig['stores']); @@ -44,11 +49,18 @@ public function diff(array $localData, array $remoteData) /** * Validates the given array if all necessary array keys exist. Otherwise an empty array is added. * - * @param array $array - * @return array + * @param [] $array + * @return [] */ protected function validateArray(array $array) { + // if data reader code does not exist, no data is provided + if (!array_key_exists(self::DATA_READER_CODE, $array)) { + $array = []; + } else { + $array = $array[self::DATA_READER_CODE]; + } + if (!array_key_exists('default', $array)) { $array['default'] = []; } @@ -73,10 +85,10 @@ protected function validateArray(array $array) */ protected function flattenArray($arr, $path) { - $result = array(); + $result = []; if(!is_array($arr)){ - return array(ltrim($path, '/') => $arr); + return [ltrim($path, '/') => $arr]; } foreach($arr as $key => $value){ From 7216314ada0f7b3f595caffdd29e32bfe662d5c9 Mon Sep 17 00:00:00 2001 From: Florian Sydekum Date: Sat, 30 Sep 2017 11:13:55 +0200 Subject: [PATCH 18/62] [MAGEHACKMUC-17] Minor fixes to doc blocks --- Differ/StoreConfigDiffer.php | 4 ++-- Service/PerformSystemDiffService.php | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Differ/StoreConfigDiffer.php b/Differ/StoreConfigDiffer.php index bcb662f..f820535 100644 --- a/Differ/StoreConfigDiffer.php +++ b/Differ/StoreConfigDiffer.php @@ -21,7 +21,7 @@ class StoreConfigDiffer extends AbstractDiffer * * @param array $localData * @param array $remoteData - * @return array + * @return [] */ public function diff(array $localData, array $remoteData) { @@ -81,7 +81,7 @@ protected function validateArray(array $array) * * @param $arr * @param $path - * @return array + * @return [] */ protected function flattenArray($arr, $path) { diff --git a/Service/PerformSystemDiffService.php b/Service/PerformSystemDiffService.php index 1f241d1..c930ec2 100644 --- a/Service/PerformSystemDiffService.php +++ b/Service/PerformSystemDiffService.php @@ -21,28 +21,31 @@ class PerformSystemDiffService implements PerformSystemDiffServiceInterface { /** - * @var \Magenerds\SystemDiff\Api\Service\FetchLocalDataServiceInterface + * @var FetchLocalDataServiceInterface */ protected $fetchLocalDataService; + /** - * @var \Magenerds\SystemDiff\Api\Service\FetchRemoteDataServiceInterface + * @var FetchRemoteDataServiceInterface */ protected $fetchRemoteDataService; + /** - * @var \Magenerds\SystemDiff\Service\DiffDataService + * @var DiffDataService */ protected $diffDataService; + /** - * @var \Magenerds\SystemDiff\Api\Service\SaveDiffToTableServiceInterface + * @var SaveDiffToTableServiceInterface */ private $saveDiffToTableService; /** * PerformSystemDiffService constructor. - * @param \Magenerds\SystemDiff\Api\Service\FetchLocalDataServiceInterface $fetchLocalDataService - * @param \Magenerds\SystemDiff\Api\Service\FetchRemoteDataServiceInterface $fetchRemoteDataService - * @param \Magenerds\SystemDiff\Service\DiffDataService $diffDataService - * @param \Magenerds\SystemDiff\Api\Service\SaveDiffToTableServiceInterface $saveDiffToTableService + * @param FetchLocalDataServiceInterface $fetchLocalDataService + * @param FetchRemoteDataServiceInterface $fetchRemoteDataService + * @param DiffDataService $diffDataService + * @param SaveDiffToTableServiceInterface $saveDiffToTableService */ public function __construct( FetchLocalDataServiceInterface $fetchLocalDataService, From 02d9ec4fc0878a2a93df28f769877f734fe62d65 Mon Sep 17 00:00:00 2001 From: Florian Sydekum Date: Sat, 30 Sep 2017 11:33:59 +0200 Subject: [PATCH 19/62] [MAGEHACKMUC-17] Added license headers --- Cron/Diff.php | 0 etc/acl.xml | 7 +++++-- etc/adminhtml/system.xml | 9 +++++++++ etc/crontab.xml | 0 4 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 Cron/Diff.php create mode 100644 etc/crontab.xml diff --git a/Cron/Diff.php b/Cron/Diff.php new file mode 100644 index 0000000..e69de29 diff --git a/etc/acl.xml b/etc/acl.xml index ae767f7..1eebff5 100644 --- a/etc/acl.xml +++ b/etc/acl.xml @@ -1,8 +1,11 @@ diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 3001e2e..6640451 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -1,4 +1,13 @@ + diff --git a/etc/crontab.xml b/etc/crontab.xml new file mode 100644 index 0000000..e69de29 From a1460b45616424f126d6d3b107ecbc5f42dfcdc3 Mon Sep 17 00:00:00 2001 From: Julien Wiedner Date: Sat, 30 Sep 2017 11:45:52 +0200 Subject: [PATCH 20/62] [MAGEHACKMUC-17] Add new field to installscript --- Setup/InstallSchema.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Setup/InstallSchema.php b/Setup/InstallSchema.php index 5acbe7f..b1b7648 100644 --- a/Setup/InstallSchema.php +++ b/Setup/InstallSchema.php @@ -48,7 +48,12 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con 8, ['nullable' => false, 'primary' => false] )->addColumn( - 'diff_value', + 'diff_value_local', + \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + null, + ['nullable' => false, 'primary' => false] + )->addColumn( + 'diff_value_remote', \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, null, ['nullable' => false, 'primary' => false] From 22168debb45d3ffc35c6a93ba93701f0c765b072 Mon Sep 17 00:00:00 2001 From: Florian Sydekum Date: Sat, 30 Sep 2017 11:46:32 +0200 Subject: [PATCH 21/62] [MAGEHACKMUC-17] Added cron job for regular diff as well as refacted console command to reuse service class --- Console/Command/ExecuteCommand.php | 72 +++++++--------------------- Cron/Diff.php | 43 +++++++++++++++++ Service/PerformSystemDiffService.php | 2 +- etc/crontab.xml | 18 +++++++ 4 files changed, 78 insertions(+), 57 deletions(-) diff --git a/Console/Command/ExecuteCommand.php b/Console/Command/ExecuteCommand.php index fae2ba9..6af0fa6 100644 --- a/Console/Command/ExecuteCommand.php +++ b/Console/Command/ExecuteCommand.php @@ -9,59 +9,30 @@ namespace Magenerds\SystemDiff\Console\Command; -use Magenerds\SystemDiff\Api\Service\DiffDataServiceInterface; -use Magenerds\SystemDiff\Api\Service\FetchLocalDataServiceInterface; -use Magenerds\SystemDiff\Api\Service\FetchRemoteDataServiceInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputOption; +use Magenerds\SystemDiff\Service\PerformSystemDiffService; class ExecuteCommand extends Command { /** - * Dry run argument + * @var PerformSystemDiffService */ - const DRY_RUN = 'dry-run'; + private $performSystemDiffService; /** - * Name argument - */ - const NAME_ARGUMENT = 'name'; - - /** - * @var FetchLocalDataServiceInterface - */ - private $fetchLocalDataService; - - /** - * @var FetchRemoteDataServiceInterface - */ - private $fetchRemoteDataService; - - /** - * @var DiffDataServiceInterface - */ - private $diffDataService; - - /** - * FetchDataCommand constructor. - * - * @param FetchLocalDataServiceInterface $fetchLocalDataService - * @param FetchRemoteDataServiceInterface $fetchRemoteDataService - * @param DiffDataServiceInterface $diffDataService + * ExecuteCommand constructor. + * @param null $name + * @param PerformSystemDiffService $performSystemDiffService */ public function __construct( - FetchLocalDataServiceInterface $fetchLocalDataService, - FetchRemoteDataServiceInterface $fetchRemoteDataService, - DiffDataServiceInterface $diffDataService - ) { - parent::__construct(); + PerformSystemDiffService $performSystemDiffService, + $name = null + ){ + parent::__construct($name); - $this->fetchLocalDataService = $fetchLocalDataService; - $this->fetchRemoteDataService = $fetchRemoteDataService; - $this->diffDataService = $diffDataService; + $this->performSystemDiffService = $performSystemDiffService; } /** @@ -71,14 +42,6 @@ public function configure() { $this->setName('system-diff:execute'); $this->setDescription('system-diff:execute'); - $this->setDefinition([ - new InputOption( - self::DRY_RUN, - '--dry-run', - InputOption::VALUE_NONE, - 'Dry run' - ) - ]); parent::configure(); } @@ -90,13 +53,10 @@ public function configure() */ public function execute(InputInterface $input, OutputInterface $output) { - $remoteData = $this->fetchRemoteDataService->fetch(); - $localData = $this->fetchLocalDataService->fetch(); - - $difference = $this->diffDataService->diffData($remoteData, $localData); - - if ($input->getOption(self::DRY_RUN)) { - $output->writeln(var_export($difference, true)); + try { + $this->performSystemDiffService->performDiff(); + } catch (\Exception $e) { + $output->writeln(sprintf('An error occurred during diff: %s', $e->getMessage())); } } -} +} \ No newline at end of file diff --git a/Cron/Diff.php b/Cron/Diff.php index e69de29..26376fe 100644 --- a/Cron/Diff.php +++ b/Cron/Diff.php @@ -0,0 +1,43 @@ +performSystemDiffService = $performSystemDiffService; + } + + /** + * @return $this + */ + public function execute() + { + $this->performSystemDiffService->performDiff(); + return $this; + } +} \ No newline at end of file diff --git a/Service/PerformSystemDiffService.php b/Service/PerformSystemDiffService.php index c930ec2..46e53e2 100644 --- a/Service/PerformSystemDiffService.php +++ b/Service/PerformSystemDiffService.php @@ -60,7 +60,7 @@ public function __construct( } /** - * @return bool + * Initiates the diff */ public function performDiff() { diff --git a/etc/crontab.xml b/etc/crontab.xml index e69de29..47cb61f 100644 --- a/etc/crontab.xml +++ b/etc/crontab.xml @@ -0,0 +1,18 @@ + + + + + + 0 * * * * + + + From 0ed3c078f54a2a4b1d782a89a33716e98428e85c Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Sat, 30 Sep 2017 11:49:43 +0200 Subject: [PATCH 22/62] [MAGEHACKMUC-17] Config as helper class --- {Model => Helper}/Config.php | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) rename {Model => Helper}/Config.php (51%) diff --git a/Model/Config.php b/Helper/Config.php similarity index 51% rename from Model/Config.php rename to Helper/Config.php index 261d782..3cddf74 100644 --- a/Model/Config.php +++ b/Helper/Config.php @@ -7,29 +7,32 @@ * http://opensource.org/licenses/osl-3.0.php */ -namespace Magenerds\SystemDiff\Model; +namespace Magenerds\SystemDiff\Helper; +use Magenerds\SystemDiff\Remote\ClientInterface; +use Magento\Framework\App\Config\ScopeConfigInterface; /** * Class to read module-specific configuration * * Class Config - * @package Magenerds\SystemDiff\Model + * @package Magenerds\SystemDiff\Helper */ class Config { const XML_PATH_ENABLED = 'system_diff/general/enabled'; const XML_PATH_REMOTE_SYSTEM_URL = 'system_diff/connection/remote_system_url'; const XML_PATH_ACCESS_TOKEN = 'system_diff/connection/access_token'; + const XML_PATH_API_TYPE = 'system_diff/connection/api_type'; /** - * @var \Magento\Framework\App\Config\ScopeConfigInterface + * @var ScopeConfigInterface */ - protected $config; + protected $scopeConfig; public function __construct( - \Magento\Framework\App\Config\ScopeConfigInterface $config + ScopeConfigInterface $scopeConfig ) { - $this->config = $config; + $this->scopeConfig = $scopeConfig; } /** @@ -37,7 +40,7 @@ public function __construct( */ public function isEnabled() { - return $this->config->isSetFlag(self::XML_PATH_ENABLED); + return $this->scopeConfig->isSetFlag(self::XML_PATH_ENABLED); } /** @@ -45,7 +48,17 @@ public function isEnabled() */ public function getRemoteSystemUrl() { - return $this->config->getValue(self::XML_PATH_REMOTE_SYSTEM_URL); + return $this->scopeConfig->getValue(self::XML_PATH_REMOTE_SYSTEM_URL); + } + + /** + * Returns the class/type name to use as Client. + * + * @return string|null + */ + public function getApiType() + { + return $this->scopeConfig->getValue(self::XML_PATH_API_TYPE); } /** @@ -53,6 +66,6 @@ public function getRemoteSystemUrl() */ public function getRemoteSystemAccessToken() { - return $this->config->getValue(self::XML_PATH_ACCESS_TOKEN); + return $this->scopeConfig->getValue(self::XML_PATH_ACCESS_TOKEN); } } \ No newline at end of file From 14fd82f1e237f24d4bf1d0eabb29859e6f24c5a6 Mon Sep 17 00:00:00 2001 From: Florian Sydekum Date: Sat, 30 Sep 2017 11:55:04 +0200 Subject: [PATCH 23/62] [MAGEHACKMUC-17] Minor enhancements to doc blocks and integration of enabled flag --- Helper/Config.php | 9 +++++++- Model/ResourceModel/DiffConfig.php | 1 - Model/ResourceModel/DiffConfig/Collection.php | 1 - Service/PerformSystemDiffService.php | 21 ++++++++++++++----- etc/crontab.xml | 2 +- 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/Helper/Config.php b/Helper/Config.php index 3cddf74..051c551 100644 --- a/Helper/Config.php +++ b/Helper/Config.php @@ -8,7 +8,7 @@ */ namespace Magenerds\SystemDiff\Helper; -use Magenerds\SystemDiff\Remote\ClientInterface; + use Magento\Framework\App\Config\ScopeConfigInterface; /** @@ -19,6 +19,9 @@ */ class Config { + /** + * Holds the configuration paths for this module + */ const XML_PATH_ENABLED = 'system_diff/general/enabled'; const XML_PATH_REMOTE_SYSTEM_URL = 'system_diff/connection/remote_system_url'; const XML_PATH_ACCESS_TOKEN = 'system_diff/connection/access_token'; @@ -29,6 +32,10 @@ class Config */ protected $scopeConfig; + /** + * Config constructor. + * @param ScopeConfigInterface $scopeConfig + */ public function __construct( ScopeConfigInterface $scopeConfig ) { diff --git a/Model/ResourceModel/DiffConfig.php b/Model/ResourceModel/DiffConfig.php index 288043a..4a64c0e 100644 --- a/Model/ResourceModel/DiffConfig.php +++ b/Model/ResourceModel/DiffConfig.php @@ -17,7 +17,6 @@ */ class DiffConfig extends AbstractDb { - /** * Resource initialization * diff --git a/Model/ResourceModel/DiffConfig/Collection.php b/Model/ResourceModel/DiffConfig/Collection.php index 789414c..2992d79 100644 --- a/Model/ResourceModel/DiffConfig/Collection.php +++ b/Model/ResourceModel/DiffConfig/Collection.php @@ -26,5 +26,4 @@ protected function _construct() \Magenerds\SystemDiff\Model\ResourceModel\DiffConfig::class ); } - } \ No newline at end of file diff --git a/Service/PerformSystemDiffService.php b/Service/PerformSystemDiffService.php index 46e53e2..1425f3c 100644 --- a/Service/PerformSystemDiffService.php +++ b/Service/PerformSystemDiffService.php @@ -13,6 +13,7 @@ use Magenerds\SystemDiff\Api\Service\FetchRemoteDataServiceInterface; use Magenerds\SystemDiff\Api\Service\PerformSystemDiffServiceInterface; use Magenerds\SystemDiff\Api\Service\SaveDiffToTableServiceInterface; +use Magenerds\SystemDiff\Helper\Config; /** * Class PerformSystemDiffService @@ -40,23 +41,31 @@ class PerformSystemDiffService implements PerformSystemDiffServiceInterface */ private $saveDiffToTableService; + /** + * @var Config + */ + private $config; + /** * PerformSystemDiffService constructor. * @param FetchLocalDataServiceInterface $fetchLocalDataService * @param FetchRemoteDataServiceInterface $fetchRemoteDataService * @param DiffDataService $diffDataService * @param SaveDiffToTableServiceInterface $saveDiffToTableService + * @param Config $config */ public function __construct( FetchLocalDataServiceInterface $fetchLocalDataService, FetchRemoteDataServiceInterface $fetchRemoteDataService, DiffDataService $diffDataService, - SaveDiffToTableServiceInterface $saveDiffToTableService + SaveDiffToTableServiceInterface $saveDiffToTableService, + Config $config ) { $this->fetchLocalDataService = $fetchLocalDataService; $this->fetchRemoteDataService = $fetchRemoteDataService; $this->diffDataService = $diffDataService; $this->saveDiffToTableService = $saveDiffToTableService; + $this->config = $config; } /** @@ -64,11 +73,13 @@ public function __construct( */ public function performDiff() { - $localData = $this->fetchLocalDataService->fetch(); - $remoteData = $this->fetchRemoteDataService->fetch(); + if ($this->config->isEnabled()) { + $localData = $this->fetchLocalDataService->fetch(); + $remoteData = $this->fetchRemoteDataService->fetch(); - $diffData = $this->diffDataService->diffData($localData, $remoteData); + $diffData = $this->diffDataService->diffData($localData, $remoteData); - $this->saveDiffToTableService->saveData($diffData); + $this->saveDiffToTableService->saveData($diffData); + } } } \ No newline at end of file diff --git a/etc/crontab.xml b/etc/crontab.xml index 47cb61f..639b5c5 100644 --- a/etc/crontab.xml +++ b/etc/crontab.xml @@ -15,4 +15,4 @@ 0 * * * * - + \ No newline at end of file From c1a78b78d5b90ac00541b03bff9adbc00c540f66 Mon Sep 17 00:00:00 2001 From: Florian Sydekum Date: Sat, 30 Sep 2017 12:04:12 +0200 Subject: [PATCH 24/62] [MAGEHACKMUC-17] Added sort order to system configuration --- etc/adminhtml/system.xml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 14ac361..f5f2d00 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -18,21 +18,21 @@ magenerds Magenerds_SystemDiff::config_system_diff - + - + Flag for showing the differences in the backend Magento\Config\Model\Config\Source\Yesno - + - + Url of the system to compare with - + Access Token of the integration user on the remote system. This user has to have the checkmark on Stores > Settings > Configuration > SystemDiff Section From 2454db8887c5c6329dd6a1db092f0f1e5f65a580 Mon Sep 17 00:00:00 2001 From: Alexander Dite Date: Sat, 30 Sep 2017 12:14:28 +0200 Subject: [PATCH 25/62] [MAGEHACKMUC-17] implement PerformSystemDiffService Unit Test --- .../Service/PerformSystemDiffServiceTest.php | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 Test/Unit/Service/PerformSystemDiffServiceTest.php diff --git a/Test/Unit/Service/PerformSystemDiffServiceTest.php b/Test/Unit/Service/PerformSystemDiffServiceTest.php new file mode 100644 index 0000000..2cc07e1 --- /dev/null +++ b/Test/Unit/Service/PerformSystemDiffServiceTest.php @@ -0,0 +1,80 @@ +fetchLocalDataService = $this->getMockBuilder(FetchLocalDataService::class) + ->setMethods(['fetch']) + ->disableOriginalConstructor() + ->getMock(); + $this->fetchRemoteDataService = $this->getMockBuilder(FetchRemoteDataService::class) + ->setMethods(['fetch']) + ->disableOriginalConstructor() + ->getMock(); + $this->diffDataService = $this->getMockBuilder(DiffDataService::class) + ->setMethods(['diffData']) + ->disableOriginalConstructor() + ->getMock(); + $this->saveDataToTableService = $this->getMockBuilder(SaveDiffToTableService::class) + ->setMethods(['saveData']) + ->disableOriginalConstructor() + ->getMock(); + + $this->testSubject = new PerformSystemDiffService( + $this->fetchLocalDataService, + $this->fetchRemoteDataService, + $this->diffDataService, + $this->saveDataToTableService + ); + } + + /** + * @test + */ + public function performDiff() + { + $localData = ['foo' => 'bar']; + $remoteData = ['faz' => 'baz']; + + $diffData = ['bla' => 'blub']; + + $this->fetchLocalDataService->expects($this->once())->method('fetch')->willReturn($localData); + $this->fetchRemoteDataService->expects($this->once())->method('fetch')->willReturn($remoteData); + $this->diffDataService->expects($this->once())->method('diffData')->with( + $this->equalTo($localData), + $this->equalTo($remoteData) + ) + ->willReturn($diffData); + $this->saveDataToTableService->expects($this->once())->method('saveData')->with($this->equalTo($diffData)); + + // GO! + $this->testSubject->performDiff(); + } +} \ No newline at end of file From aa3214b6f7ee05606676c331f00c968ae463a3d4 Mon Sep 17 00:00:00 2001 From: Alexander Dite Date: Sat, 30 Sep 2017 12:20:58 +0200 Subject: [PATCH 26/62] [MAGEHACKMUC-17] implement PerformSystemDiffService Unit Test - adjust #1 --- Test/Unit/Service/PerformSystemDiffServiceTest.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Test/Unit/Service/PerformSystemDiffServiceTest.php b/Test/Unit/Service/PerformSystemDiffServiceTest.php index 2cc07e1..6b0b364 100644 --- a/Test/Unit/Service/PerformSystemDiffServiceTest.php +++ b/Test/Unit/Service/PerformSystemDiffServiceTest.php @@ -2,6 +2,7 @@ namespace Magenerds\SystemDiff\Test\Unit\Service; +use Magenerds\SystemDiff\Helper\Config; use Magenerds\SystemDiff\Service\DiffDataService; use Magenerds\SystemDiff\Service\FetchLocalDataService; use Magenerds\SystemDiff\Service\FetchRemoteDataService; @@ -24,6 +25,8 @@ class PerformSystemDiffServiceTest extends \PHPUnit\Framework\TestCase protected $diffDataService; /** @var \Magenerds\SystemDiff\Service\SaveDiffToTableService|\PHPUnit_Framework_MockObject_MockObject */ protected $saveDataToTableService; + /** @var \Magenerds\SystemDiff\Helper\Config|\PHPUnit_Framework_MockObject_MockObject */ + protected $helperConfig; /** * Prepare test subject @@ -46,12 +49,17 @@ protected function setUp() ->setMethods(['saveData']) ->disableOriginalConstructor() ->getMock(); + $this->helperConfig = $this->getMockBuilder(Config::class) + ->setMethods(['isEnabled']) + ->disableOriginalConstructor() + ->getMock(); $this->testSubject = new PerformSystemDiffService( $this->fetchLocalDataService, $this->fetchRemoteDataService, $this->diffDataService, - $this->saveDataToTableService + $this->saveDataToTableService, + $this->helperConfig ); } @@ -60,6 +68,8 @@ protected function setUp() */ public function performDiff() { + + $this->helperConfig->expects($this->any())->method('isEnabled')->willReturn(true); $localData = ['foo' => 'bar']; $remoteData = ['faz' => 'baz']; From 5bf9b78df685737d62eeccc614eb778e80bad119 Mon Sep 17 00:00:00 2001 From: Julien Wiedner Date: Sat, 30 Sep 2017 13:57:36 +0200 Subject: [PATCH 27/62] [MAGEHACKMUC-17] Add functionality to store diffConfig in database --- .../SaveDiffToTableServiceInterface.php | 2 +- DataWriter/DataWriterInterface.php | 19 +++ DataWriter/DataWriterPool.php | 66 ++++++++ DataWriter/StoreConfigDataWriter.php | 146 ++++++++++++++++++ Service/SaveDiffToTableService.php | 23 ++- etc/di.xml | 8 + 6 files changed, 261 insertions(+), 3 deletions(-) create mode 100644 DataWriter/DataWriterInterface.php create mode 100644 DataWriter/DataWriterPool.php create mode 100644 DataWriter/StoreConfigDataWriter.php diff --git a/Api/Service/SaveDiffToTableServiceInterface.php b/Api/Service/SaveDiffToTableServiceInterface.php index dfa45cc..b946fd5 100644 --- a/Api/Service/SaveDiffToTableServiceInterface.php +++ b/Api/Service/SaveDiffToTableServiceInterface.php @@ -17,7 +17,7 @@ interface SaveDiffToTableServiceInterface { /** * @param array $diffData - * @return bool + * @return void */ public function saveData(array $diffData); } \ No newline at end of file diff --git a/DataWriter/DataWriterInterface.php b/DataWriter/DataWriterInterface.php new file mode 100644 index 0000000..214430e --- /dev/null +++ b/DataWriter/DataWriterInterface.php @@ -0,0 +1,19 @@ +dataWriter = $tmapFactory->create( + [ + 'array' => $dataWriters, + 'type' => DataWriterInterface::class + ] + ); + } + + /** + * Retrieves data writer by code + * + * @param string $dataWriterCode + * @return DataWriterInterface + * @throws NotFoundException + */ + public function get($dataWriterCode) + { + if (!isset($this->dataWriter[$dataWriterCode])) { + throw new NotFoundException(__('Data writer %1 does not exist.', $dataWriterCode)); + } + + return $this->dataWriter[$dataWriterCode]; + } + + /** + * Retrieve an external iterator + * @link http://php.net/manual/en/iteratoraggregate.getiterator.php + * @return \Traversable An instance of an object implementing Iterator or + * Traversable + * @since 5.0.0 + */ + public function getIterator() + { + return $this->dataWriter; + } +} \ No newline at end of file diff --git a/DataWriter/StoreConfigDataWriter.php b/DataWriter/StoreConfigDataWriter.php new file mode 100644 index 0000000..d99ece1 --- /dev/null +++ b/DataWriter/StoreConfigDataWriter.php @@ -0,0 +1,146 @@ +diffConfigResource = $diffConfigResource; + $this->diffConfigFactory = $diffConfigFactory; + $this->storeManager = $storeManager; + } + + /** + * @param array $diffData + * @return void + */ + public function write(array $diffData) + { + $diffData = $diffData['storeConfig']; + + foreach ($diffData as $scope => $data) { + $localValues = $data[1]; + $remoteValues = $data[2]; + + $localModels = $this->mapDataToModels($localValues, $scope, self::LOCAL_VALUE_FIELD_NAME); + $remoteModels = $this->mapDataToModels($remoteValues, $scope, self::REMOTE_VALUE_FIELD_NAME); + + $scopeModels = array_merge($localModels, $remoteModels); + + foreach ($scopeModels as $scopeModel) { + $this->diffConfigResource->save($scopeModel); + } + } + } + + /** + * @param array $data + * @param string $scope + * @param string $valueField + * + * @return DiffConfigModel[] + */ + protected function mapDataToModels(array $data, $scope, $valueField) + { + $models = []; + + foreach ($data as $path => $value) { + /** @var DiffConfigModel $diffConfigModel */ + $diffConfigModel = $this->diffConfigFactory->create(); + $diffConfigModel->setData('scope', $scope); + + $scopeId = 0; + + if ($scope === 'websites' || $scope === 'stores') { + $splittedPath = explode('/', $path, 2); + $code = $splittedPath[0]; + $path = $splittedPath[1]; + + if ($scope === 'websites') { + $scopeId = $this->getWebsiteId($code); + } + + if ($scope === 'stores') { + $scopeId = $this->getStoreId($code); + } + } + + $diffConfigModel->setData('scope_id', $scopeId); + $diffConfigModel->setData('path', $path); + $diffConfigModel->setData($valueField, $value); + + $models[] = $diffConfigModel; + } + + return $models; + } + + /** + * @param string $code + * @return int + */ + private function getWebsiteId($code): int + { + $websites = $this->storeManager->getWebsites(true); + foreach ($websites as $website) { + if ($website->getCode() === $code) { + return $website->getId(); + } + } + return 0; + } + + /** + * @param string $code + * @return int + */ + private function getStoreId($code): int + { + $stores = $this->storeManager->getStores(true); + foreach ($stores as $store) { + if ($store->getCode() === $code) { + return $store->getId(); + } + } + return 0; + } + +} \ No newline at end of file diff --git a/Service/SaveDiffToTableService.php b/Service/SaveDiffToTableService.php index 21390ba..e0c8369 100644 --- a/Service/SaveDiffToTableService.php +++ b/Service/SaveDiffToTableService.php @@ -10,6 +10,8 @@ namespace Magenerds\SystemDiff\Service; use Magenerds\SystemDiff\Api\Service\SaveDiffToTableServiceInterface; +use Magenerds\SystemDiff\DataWriter\DataWriterInterface; +use Magenerds\SystemDiff\DataWriter\DataWriterPool; /** * Class SaveDiffToTableService @@ -17,12 +19,29 @@ */ class SaveDiffToTableService implements SaveDiffToTableServiceInterface { + /** + * @var DataWriterPool + */ + private $writerPool; + + /** + * StoreConfigDataWriter constructor. + * @param DataWriterPool $writerPool + */ + public function __construct(DataWriterPool $writerPool) + { + $this->writerPool = $writerPool; + } + /** * @param array $diffData - * @return bool + * @return void */ public function saveData(array $diffData) { - // TODO: Implement saveData() method. + foreach ($this->writerPool as $writer) { + /** @var DataWriterInterface $writer */ + $writer->write($diffData); + } } } \ No newline at end of file diff --git a/etc/di.xml b/etc/di.xml index 6f66978..bf67a1a 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -44,6 +44,14 @@ + + + + Magenerds\SystemDiff\DataWriter\StoreConfigDataWriter + + + + From c37d2f6fc9235d62559db2359498a42ee7461a9c Mon Sep 17 00:00:00 2001 From: Julien Wiedner Date: Sat, 30 Sep 2017 14:26:49 +0200 Subject: [PATCH 28/62] [MAGEHACKMUC-17] Some code improvements --- DataWriter/StoreConfigDataWriter.php | 47 ++++++++++++++-------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/DataWriter/StoreConfigDataWriter.php b/DataWriter/StoreConfigDataWriter.php index d99ece1..7a91341 100644 --- a/DataWriter/StoreConfigDataWriter.php +++ b/DataWriter/StoreConfigDataWriter.php @@ -19,6 +19,17 @@ class StoreConfigDataWriter implements DataWriterInterface const LOCAL_VALUE_FIELD_NAME = 'diff_value_local'; const REMOTE_VALUE_FIELD_NAME = 'diff_value_remote'; + const SCOPE_FIELD_NAME = 'scope'; + const SCOPE_ID_FIELD_NAME = 'scope_id'; + const PATH_FIELD_NAME = 'path'; + const SCOPE_VALUE_WEBSITES = 'websites'; + const SCOPE_VALUE_STORES = 'stores'; + + const DEFAULT_SCOPE_ID = 0; + const ARRAY_INDEX_LOCAL = 1; + const ARRAY_INDEX_REMOTE = 2; + const ARRAY_STORE_CONFIG_KEY = 'storeConfig'; + /** * @var DiffConfigResource */ @@ -54,11 +65,11 @@ public function __construct( */ public function write(array $diffData) { - $diffData = $diffData['storeConfig']; + $diffData = $diffData[self::ARRAY_STORE_CONFIG_KEY]; foreach ($diffData as $scope => $data) { - $localValues = $data[1]; - $remoteValues = $data[2]; + $localValues = $data[self::ARRAY_INDEX_LOCAL]; + $remoteValues = $data[self::ARRAY_INDEX_REMOTE]; $localModels = $this->mapDataToModels($localValues, $scope, self::LOCAL_VALUE_FIELD_NAME); $remoteModels = $this->mapDataToModels($remoteValues, $scope, self::REMOTE_VALUE_FIELD_NAME); @@ -85,26 +96,26 @@ protected function mapDataToModels(array $data, $scope, $valueField) foreach ($data as $path => $value) { /** @var DiffConfigModel $diffConfigModel */ $diffConfigModel = $this->diffConfigFactory->create(); - $diffConfigModel->setData('scope', $scope); + $diffConfigModel->setData(self::SCOPE_FIELD_NAME, $scope); - $scopeId = 0; + $scopeId = self::DEFAULT_SCOPE_ID; - if ($scope === 'websites' || $scope === 'stores') { + if ($scope === self::SCOPE_VALUE_WEBSITES || $scope === self::SCOPE_VALUE_STORES) { $splittedPath = explode('/', $path, 2); $code = $splittedPath[0]; $path = $splittedPath[1]; - if ($scope === 'websites') { + if ($scope === self::SCOPE_VALUE_WEBSITES) { $scopeId = $this->getWebsiteId($code); } - if ($scope === 'stores') { + if ($scope === self::SCOPE_VALUE_STORES) { $scopeId = $this->getStoreId($code); } } - $diffConfigModel->setData('scope_id', $scopeId); - $diffConfigModel->setData('path', $path); + $diffConfigModel->setData(self::SCOPE_ID_FIELD_NAME, $scopeId); + $diffConfigModel->setData(self::PATH_FIELD_NAME, $path); $diffConfigModel->setData($valueField, $value); $models[] = $diffConfigModel; @@ -119,13 +130,7 @@ protected function mapDataToModels(array $data, $scope, $valueField) */ private function getWebsiteId($code): int { - $websites = $this->storeManager->getWebsites(true); - foreach ($websites as $website) { - if ($website->getCode() === $code) { - return $website->getId(); - } - } - return 0; + return $this->storeManager->getWebsite($code)->getId(); } /** @@ -134,13 +139,7 @@ private function getWebsiteId($code): int */ private function getStoreId($code): int { - $stores = $this->storeManager->getStores(true); - foreach ($stores as $store) { - if ($store->getCode() === $code) { - return $store->getId(); - } - } - return 0; + return $this->storeManager->getStore($code)->getId(); } } \ No newline at end of file From 55d1f5e5e66de2a6672c41a624853707eac372ec Mon Sep 17 00:00:00 2001 From: Julien Wiedner Date: Sat, 30 Sep 2017 14:43:16 +0200 Subject: [PATCH 29/62] [MAGEHACKMUC-17] Fix UnitTests --- Test/Unit/Differ/DifferPoolUnitTest.php | 2 +- .../Unit/Differ/StoreConfigDifferUnitTest.php | 58 ++++++++++--------- Test/Unit/Service/DiffDataServiceUnitTest.php | 2 +- 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/Test/Unit/Differ/DifferPoolUnitTest.php b/Test/Unit/Differ/DifferPoolUnitTest.php index 1440ebf..1456260 100644 --- a/Test/Unit/Differ/DifferPoolUnitTest.php +++ b/Test/Unit/Differ/DifferPoolUnitTest.php @@ -14,7 +14,7 @@ use Magento\Framework\ObjectManager\TMap; use Magento\Framework\ObjectManager\TMapFactory; -class DifferPoolUnitTest extends \PHPUnit_Framework_TestCase +class DifferPoolUnitTest extends \PHPUnit\Framework\TestCase { /** * @var DifferPool diff --git a/Test/Unit/Differ/StoreConfigDifferUnitTest.php b/Test/Unit/Differ/StoreConfigDifferUnitTest.php index 5abc068..39f2163 100644 --- a/Test/Unit/Differ/StoreConfigDifferUnitTest.php +++ b/Test/Unit/Differ/StoreConfigDifferUnitTest.php @@ -11,7 +11,7 @@ use Magenerds\SystemDiff\Differ\StoreConfigDiffer; -class StoreConfigDifferUnitTest extends \PHPUnit_Framework_TestCase +class StoreConfigDifferUnitTest extends \PHPUnit\Framework\TestCase { /** * @var StoreConfigDiffer @@ -30,39 +30,43 @@ public function testDiff() { $diffData1 = [ - 'default' => [ - 'foo' => [ - 'bar' => [ - 'fooconfig' => 'foobar' + 'storeConfig' => [ + 'default' => [ + 'foo' => [ + 'bar' => [ + 'fooconfig' => 'foobar' + ] ] - ] - ], - 'websites' => [ - 'foo' => [ - 'bar' => [ - 'fooconfig' => 'foobar' + ], + 'websites' => [ + 'foo' => [ + 'bar' => [ + 'fooconfig' => 'foobar' + ] ] - ] - ], - 'stores' => [] + ], + 'stores' => [] + ] ]; $diffData2 = [ - 'default' => [ - 'foo' => [ - 'bar' => [ - 'fooconfig' => 'barfoo' + 'storeConfig' => [ + 'default' => [ + 'foo' => [ + 'bar' => [ + 'fooconfig' => 'barfoo' + ] ] - ] - ], - 'websites' => [ - 'foo' => [ - 'bar' => [ - 'fooconfig' => 'foobar' + ], + 'websites' => [ + 'foo' => [ + 'bar' => [ + 'fooconfig' => 'foobar' + ] ] - ] - ], - 'stores' => [] + ], + 'stores' => [] + ] ]; $this->assertEquals( diff --git a/Test/Unit/Service/DiffDataServiceUnitTest.php b/Test/Unit/Service/DiffDataServiceUnitTest.php index ecf8c6f..5aa52cb 100644 --- a/Test/Unit/Service/DiffDataServiceUnitTest.php +++ b/Test/Unit/Service/DiffDataServiceUnitTest.php @@ -6,7 +6,7 @@ use Magenerds\SystemDiff\Differ\DifferPool; use Magenerds\SystemDiff\Service\DiffDataService; -class DiffDataServiceUnitTest extends \PHPUnit_Framework_TestCase +class DiffDataServiceUnitTest extends \PHPUnit\Framework\TestCase { /** * @var DifferPool|\PHPUnit_Framework_MockObject_MockObject From b4619605895804cf0867a7451a56e6669ea0739f Mon Sep 17 00:00:00 2001 From: Alexander Dite Date: Sat, 30 Sep 2017 14:43:27 +0200 Subject: [PATCH 30/62] [MAGEHACKMUC-17] fix existing unit tests --- .../DataReader/DataReaderPoolUnitTest.php | 2 +- .../StoreConfigDataReaderUnitTest.php | 63 +++++++------------ 2 files changed, 24 insertions(+), 41 deletions(-) diff --git a/Test/Unit/DataReader/DataReaderPoolUnitTest.php b/Test/Unit/DataReader/DataReaderPoolUnitTest.php index ddd583e..2c68d8b 100644 --- a/Test/Unit/DataReader/DataReaderPoolUnitTest.php +++ b/Test/Unit/DataReader/DataReaderPoolUnitTest.php @@ -14,7 +14,7 @@ use Magento\Framework\ObjectManager\TMap; use Magento\Framework\ObjectManager\TMapFactory; -class DataReaderPoolUnitTest extends \PHPUnit_Framework_TestCase +class DataReaderPoolUnitTest extends \PHPUnit\Framework\TestCase { /** * @var DataReaderPool diff --git a/Test/Unit/DataReader/StoreConfigDataReaderUnitTest.php b/Test/Unit/DataReader/StoreConfigDataReaderUnitTest.php index f700bfd..e8de3cd 100644 --- a/Test/Unit/DataReader/StoreConfigDataReaderUnitTest.php +++ b/Test/Unit/DataReader/StoreConfigDataReaderUnitTest.php @@ -10,26 +10,18 @@ namespace Magenerds\SystemDiff\Test\Unit\DataReader; use Magenerds\SystemDiff\DataReader\StoreConfigDataReader; -use Magento\Store\Model\Config\Reader\DefaultReader; -use Magento\Store\Model\Config\Reader\Store; -use Magento\Store\Model\Config\Reader\Website; +use Magento\Framework\App\Config\ConfigSourceInterface; -class StoreConfigDataReaderUnitTest extends \PHPUnit_Framework_TestCase +/** + * Class StoreConfigDataReaderUnitTest + * @package Magenerds\SystemDiff\Test\Unit\DataReader + */ +class StoreConfigDataReaderUnitTest extends \PHPUnit\Framework\TestCase { /** - * @var DefaultReader|\PHPUnit_Framework_MockObject_MockObject - */ - private $defaultReader; - - /** - * @var Website|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\App\Config\ConfigSourceInterface|\PHPUnit_Framework_MockObject_MockObject */ - private $websiteReader; - - /** - * @var Store|\PHPUnit_Framework_MockObject_MockObject - */ - private $storeReader; + protected $configSource; /** * @var StoreConfigDataReader @@ -41,14 +33,11 @@ class StoreConfigDataReaderUnitTest extends \PHPUnit_Framework_TestCase */ protected function setUp() { - $this->defaultReader = $this->getMockBuilder(DefaultReader::class)->disableOriginalConstructor()->getMock(); - $this->websiteReader = $this->getMockBuilder(Website::class)->disableOriginalConstructor()->getMock(); - $this->storeReader = $this->getMockBuilder(Store::class)->disableOriginalConstructor()->getMock(); - + $this->configSource = $this->getMockBuilder(ConfigSourceInterface::class) + ->setMethods(['get']) + ->getMockForAbstractClass(); $this->reader = new StoreConfigDataReader( - $this->defaultReader, - $this->websiteReader, - $this->storeReader + $this->configSource ); } @@ -57,25 +46,19 @@ protected function setUp() */ public function itShouldReturnStoreConfigData() { - $this->defaultReader - ->expects($this->any()) - ->method('read') - ->willReturn(['foo' => 'bar']); - $this->websiteReader - ->expects($this->any()) - ->method('read') - ->willReturn(['foo' => 'bar']); - $this->storeReader - ->expects($this->any()) - ->method('read') - ->willReturn(['foo' => 'bar']); + $configArr = [ + 'default' => ['foo' => 'bar'], + 'websites' => ['foo' => 'bar'], + 'stores' => ['foo' => 'bar'] + ]; + + $this->configSource->expects($this->once()) + ->method('get') + ->willReturn($configArr); + $this->assertEquals( - [ - 'default' => ['foo' => 'bar'], - 'websites' => ['foo' => 'bar'], - 'stores' => ['foo' => 'bar'] - ], + $configArr, $this->reader->read() ); } From 89648f8f2b5361142e1b5cb5422d98a11fbda739 Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Sat, 30 Sep 2017 14:57:06 +0200 Subject: [PATCH 31/62] Problem mit API calls --- Api/Data/ConfigDataInterface.php | 13 +++ .../FetchLocalDataServiceInterface.php | 2 +- .../FetchRemoteDataServiceInterface.php | 2 +- DataReader/DataReaderInterface.php | 3 + DataReader/MockDataReader.php | 15 ++++ DataReader/StoreConfigDataReader.php | 2 +- Model/Config/Source/ApiType.php | 84 +++++++++++++++++++ Model/ConfigData.php | 27 ++++++ Remote/AbstractClient.php | 26 ++++++ Remote/Client.php | 15 ---- Remote/ClientAdapter.php | 34 ++++++++ Remote/ClientAdapterInterface.php | 15 ++++ Remote/ClientInterface.php | 7 +- Remote/RestClient.php | 32 +++++++ Remote/SoapClient.php | 39 +++++++++ Service/DiffDataService.php | 2 +- Service/FetchLocalDataService.php | 5 +- Service/FetchRemoteDataService.php | 19 +++-- Service/PerformSystemDiffService.php | 4 +- etc/adminhtml/system.xml | 17 +++- etc/config.xml | 16 ++++ etc/di.xml | 5 +- 22 files changed, 350 insertions(+), 34 deletions(-) create mode 100644 Api/Data/ConfigDataInterface.php create mode 100644 DataReader/MockDataReader.php create mode 100644 Model/Config/Source/ApiType.php create mode 100644 Model/ConfigData.php create mode 100644 Remote/AbstractClient.php delete mode 100644 Remote/Client.php create mode 100644 Remote/ClientAdapter.php create mode 100644 Remote/ClientAdapterInterface.php create mode 100644 Remote/RestClient.php create mode 100644 Remote/SoapClient.php create mode 100644 etc/config.xml diff --git a/Api/Data/ConfigDataInterface.php b/Api/Data/ConfigDataInterface.php new file mode 100644 index 0000000..6fbccde --- /dev/null +++ b/Api/Data/ConfigDataInterface.php @@ -0,0 +1,13 @@ + ['path' => 'value']]; + // TODO: Implement read() method. + } +} \ No newline at end of file diff --git a/DataReader/StoreConfigDataReader.php b/DataReader/StoreConfigDataReader.php index b1322af..6032c54 100644 --- a/DataReader/StoreConfigDataReader.php +++ b/DataReader/StoreConfigDataReader.php @@ -31,7 +31,7 @@ public function __construct( /** * Reads the store configuration from database. * - * @return [] + * @return array */ public function read() { diff --git a/Model/Config/Source/ApiType.php b/Model/Config/Source/ApiType.php new file mode 100644 index 0000000..178aea3 --- /dev/null +++ b/Model/Config/Source/ApiType.php @@ -0,0 +1,84 @@ +scopeConfig = $scopeConfig; + } + + /** + * Options getter + * + * @return array + */ + public function toOptionArray() + { + $options = []; + + $types = $this->getConfiguredOptions(); + + foreach ($types as $label => $type) { + $options[] = ['value' => $type, 'label' => $label]; + } + + return $options; + } + + /** + * Get options in "key-value" format + * + * @return array + */ + public function toArray() + { + $options = []; + + $types = $this->getConfiguredOptions(); + + foreach ($types as $label => $type) { + $options[$type] = $label; + } + + return $options; + } + + /** + * Returns the options configured in config path 'system/magenerds/system_diff/api_types'. + * + * This is the place to extend the config with additional types. + * + * @return array + */ + protected function getConfiguredOptions(): array + { + $types = $this->scopeConfig->getValue(self::XML_PATH_TYPES); + + if (is_array($types)) { + + return $types; + } + + return []; + } +} diff --git a/Model/ConfigData.php b/Model/ConfigData.php new file mode 100644 index 0000000..244d269 --- /dev/null +++ b/Model/ConfigData.php @@ -0,0 +1,27 @@ +data = new \ArrayObject($data); + } + + /** + * @return mixed + */ + public function getData() + { + return $this->data; + } +} \ No newline at end of file diff --git a/Remote/AbstractClient.php b/Remote/AbstractClient.php new file mode 100644 index 0000000..c268e8a --- /dev/null +++ b/Remote/AbstractClient.php @@ -0,0 +1,26 @@ +helper = $configHelper; + } +} \ No newline at end of file diff --git a/Remote/Client.php b/Remote/Client.php deleted file mode 100644 index e920b28..0000000 --- a/Remote/Client.php +++ /dev/null @@ -1,15 +0,0 @@ -configHelper = $configHelper; + $this->objectManager = $objectManager; + } + + public function getClient() : ClientInterface { + return $this->objectManager->get($this->configHelper->getApiType()); + } +} \ No newline at end of file diff --git a/Remote/ClientAdapterInterface.php b/Remote/ClientAdapterInterface.php new file mode 100644 index 0000000..6589409 --- /dev/null +++ b/Remote/ClientAdapterInterface.php @@ -0,0 +1,15 @@ +setUri($this->helper->getRemoteSystemUrl()); + $httpClient->setHeaders('Authorization', "Bearer {$this->helper->getRemoteSystemAccessToken()}"); + $response = $httpClient->request(\Zend_Http_Client::GET); + + if ($response->isSuccessful() + && strtolower($response->getHeader('Content-type')) === 'application/json; charset=utf-8' + ) { + $data = json_decode($response->getBody()); + if (is_array($data)) { + return new \ArrayObject($data); + } + } + + throw new \Exception("Request to remote was not successfull"); + } +} \ No newline at end of file diff --git a/Remote/SoapClient.php b/Remote/SoapClient.php new file mode 100644 index 0000000..3cf1b8f --- /dev/null +++ b/Remote/SoapClient.php @@ -0,0 +1,39 @@ + [ + 'header' => "Authorization: Bearer {$this->helper->getRemoteSystemAccessToken()}" + ] + ); + $streamContext = stream_context_create($opts); + + + $httpClient = new \Zend_Soap_Client($this->helper->getRemoteSystemUrl()); + $httpClient->setOptions( + [ + 'stream_context' => $streamContext, + ] + ); + $response = $httpClient->magenerdsSystemDiffServiceFetchLocalDataServiceV1Fetch(); + + if (isset($response->result->item)) { + return new ConfigData(json_decode(json_encode($response->result->item), true)); + } + + return new \ArrayObject(); + } + +} \ No newline at end of file diff --git a/Service/DiffDataService.php b/Service/DiffDataService.php index 02b7a79..59113e6 100644 --- a/Service/DiffDataService.php +++ b/Service/DiffDataService.php @@ -40,7 +40,7 @@ public function diffData(array $localData, array $remoteData) foreach ($this->differPool as $differCode => $differ) { /** @var DifferInterface $differ */ - $differences[$differCode] = $differ->diff($localData, $remoteData); + $differences[$differCode] = $differ->diff((array)$localData, (array)$remoteData); } return $differences; diff --git a/Service/FetchLocalDataService.php b/Service/FetchLocalDataService.php index acb92f3..3aa37b8 100644 --- a/Service/FetchLocalDataService.php +++ b/Service/FetchLocalDataService.php @@ -12,6 +12,7 @@ use Magenerds\SystemDiff\Api\Service\FetchLocalDataServiceInterface; use Magenerds\SystemDiff\DataReader\DataReaderPool; use Magenerds\SystemDiff\DataReader\DataReaderInterface; +use Magenerds\SystemDiff\Model\ConfigData; class FetchLocalDataService implements FetchLocalDataServiceInterface { @@ -30,7 +31,7 @@ public function __construct(DataReaderPool $dataReaderPool) } /** - * @return mixed + * @return \ArrayObject */ public function fetch() { @@ -41,6 +42,6 @@ public function fetch() $data[$dataReaderCode] = $dataReader->read(); } - return $data; + return new ConfigData($data); } } \ No newline at end of file diff --git a/Service/FetchRemoteDataService.php b/Service/FetchRemoteDataService.php index 1350ccf..11d9c1d 100644 --- a/Service/FetchRemoteDataService.php +++ b/Service/FetchRemoteDataService.php @@ -9,8 +9,11 @@ namespace Magenerds\SystemDiff\Service; +use Magenerds\SystemDiff\Api\Data\ConfigDataInterface; use Magenerds\SystemDiff\Api\Service\FetchRemoteDataServiceInterface; +use Magenerds\SystemDiff\Remote\ClientAdapter; use Magenerds\SystemDiff\Remote\ClientInterface; +use Magento\Framework\Config\Data\ConfigData; class FetchRemoteDataService implements FetchRemoteDataServiceInterface { @@ -18,20 +21,26 @@ class FetchRemoteDataService implements FetchRemoteDataServiceInterface * @var ClientInterface */ private $client; + /** + * @var ClientAdapter + */ + private $clientAdapter; /** - * @param ClientInterface $client + * @param ClientAdapter $clientAdapter */ - public function __construct(ClientInterface $client) + public function __construct(ClientAdapter $clientAdapter) { - $this->client = $client; + + $this->clientAdapter = $clientAdapter; + $this->client = $this->clientAdapter->getClient(); } /** - * @return array + * @return ConfigDataInterface */ public function fetch() { - return []; + return $this->client->fetch(); } } \ No newline at end of file diff --git a/Service/PerformSystemDiffService.php b/Service/PerformSystemDiffService.php index 1425f3c..ceb5ec8 100644 --- a/Service/PerformSystemDiffService.php +++ b/Service/PerformSystemDiffService.php @@ -77,9 +77,9 @@ public function performDiff() $localData = $this->fetchLocalDataService->fetch(); $remoteData = $this->fetchRemoteDataService->fetch(); - $diffData = $this->diffDataService->diffData($localData, $remoteData); + //$diffData = $this->diffDataService->diffData(((array)$localData), ((array)$remoteData)); - $this->saveDiffToTableService->saveData($diffData); + //$this->saveDiffToTableService->saveData($diffData); } } } \ No newline at end of file diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index f5f2d00..bd62af8 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -28,11 +28,22 @@ - + + + Type of API to use: REST or SOAP. + Magenerds\SystemDiff\Model\Config\Source\ApiType + + - Url of the system to compare with + + REST: http[s]://[remotehost]/index.php/rest/all/V1/systemConfig/all
+ SOAP: http[s]://[remotehost]/soap?wsdl&services=magenerdsSystemDiffServiceFetchLocalDataServiceV1
+
+ See http://devdocs.magento.com/guides/v2.2/get-started/soap/soap-web-api-calls.html + ]]>
- + Access Token of the integration user on the remote system. This user has to have the checkmark on Stores > Settings > Configuration > SystemDiff Section diff --git a/etc/config.xml b/etc/config.xml new file mode 100644 index 0000000..b73ccfa --- /dev/null +++ b/etc/config.xml @@ -0,0 +1,16 @@ + + + + + + + + \Magenerds\SystemDiff\Remote\RestClient + \Magenerds\SystemDiff\Remote\SoapClient + + + + + + diff --git a/etc/di.xml b/etc/di.xml index 6f66978..5079af6 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -16,8 +16,8 @@ - + @@ -40,6 +40,7 @@ Magenerds\SystemDiff\DataReader\StoreConfigDataReader + Magenerds\SystemDiff\DataReader\MockDataReader
From 5e5e8c4c11f0386e6951a91341d4ed0226e69089 Mon Sep 17 00:00:00 2001 From: Alexander Dite Date: Sat, 30 Sep 2017 15:53:01 +0200 Subject: [PATCH 32/62] [MAGEHACKMUC-17] add default value for clientadapter; --- Service/PerformSystemDiffService.php | 4 ++-- etc/config.xml | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Service/PerformSystemDiffService.php b/Service/PerformSystemDiffService.php index ceb5ec8..f2d8bdf 100644 --- a/Service/PerformSystemDiffService.php +++ b/Service/PerformSystemDiffService.php @@ -77,9 +77,9 @@ public function performDiff() $localData = $this->fetchLocalDataService->fetch(); $remoteData = $this->fetchRemoteDataService->fetch(); - //$diffData = $this->diffDataService->diffData(((array)$localData), ((array)$remoteData)); + $diffData = $this->diffDataService->diffData(((array)$localData), ((array)$remoteData)); - //$this->saveDiffToTableService->saveData($diffData); + $this->saveDiffToTableService->saveData($diffData); } } } \ No newline at end of file diff --git a/etc/config.xml b/etc/config.xml index b73ccfa..0c98323 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -12,5 +12,10 @@ + + + \Magenerds\SystemDiff\Remote\SoapClient + + From fe34c8b04eb33f3ab515a04f7badf682f44e3e6a Mon Sep 17 00:00:00 2001 From: Florian Sydekum Date: Sat, 30 Sep 2017 16:07:42 +0200 Subject: [PATCH 33/62] [MAGEHACKMUC-17] Added backend gui for store configuration diff --- Block/System/Config/Form/Field.php | 234 ++++++++++++++++++ Helper/Config.php | 9 + Model/DiffConfig.php | 9 +- Model/ResourceModel/DiffConfig/Collection.php | 10 +- Observer/StoreConfigDesign.php | 63 +++++ etc/adminhtml/system.xml | 10 +- etc/config.xml | 20 ++ etc/di.xml | 11 +- etc/events.xml | 16 ++ view/adminhtml/web/css/style.css | 28 +++ 10 files changed, 399 insertions(+), 11 deletions(-) create mode 100644 Block/System/Config/Form/Field.php create mode 100644 Observer/StoreConfigDesign.php create mode 100644 etc/config.xml create mode 100644 etc/events.xml create mode 100644 view/adminhtml/web/css/style.css diff --git a/Block/System/Config/Form/Field.php b/Block/System/Config/Form/Field.php new file mode 100644 index 0000000..0e32541 --- /dev/null +++ b/Block/System/Config/Form/Field.php @@ -0,0 +1,234 @@ +config = $config; + $this->collectionFactory = $collectionFactory; + + parent::__construct($context, $data); + } + + /** + * @inheritdoc + */ + public function render(AbstractElement $element) + { + // if displaying is disabled, render the core logic + if (!$this->config->isDisplayStoreConfig()) { + return parent::render($element); + } + + $isCheckboxRequired = $this->_isInheritCheckboxRequired($element); + + // Disable element if value is inherited from other scope. Flag has to be set before the value is rendered. + if ($element->getInherit() == 1 && $isCheckboxRequired) { + $element->setDisabled(true); + } + + $html = ''; + $html .= $this->_renderValue($element); + + // add the diff element + $html .= $this->_renderDiffHtml($element); + + if ($isCheckboxRequired) { + $html .= $this->_renderInheritCheckbox($element); + } + + $html .= $this->_renderHint($element); + + return $this->_decorateRowHtml($element, $html); + } + + protected function _renderDiffHtml(AbstractElement $element) + { + $html = ''; + + // get path, scope and scope id + $path = $this->_getPathByName($element->getData('name')); + $scope = $element->getData('scope'); + $scopeId = $element->getData('scope_id'); + + /** @var $collection Collection */ + $collection = $this->collectionFactory->create(); + $collection->addFieldToFilter('path', $path); + $collection->addFieldToFilter('scope', $scope); + + // if it is not default scope add scope id filter to the collection + if($scope !== 'default'){ + $collection->addFieldToFilter('scope_id', $scopeId); + // get current website/store code +// $configData = Mage::getSingleton('adminhtml/config_data'); +// $website = $configData->getWebsite(); +// $store = $configData->getStore(); +// if($store === ''){ +// // Website +// $collection->addFieldToFilter('code', $website); +// } else { +// // Store +// $collection->addFieldToFilter('code', $store); +// } + } + + // if an entry was found and has data, add the diff html + /** @var $entry DiffConfig */ + $entry = $collection->getFirstItem(); + if (count($entry->getData()) > 0) { + $html .= ''; + $html .= ''; + $html .= ''; + $html .= ''; + $html .= '
'; + $html .= $this->_getDiffElementHtml($element, $entry->getDiffValueRemote()); + $html .= '
'; + $html .= '
'; + $html .= '
'; + $html .= $path; + $html .= '
'; + $html .= '
'; + $html .= $scope; + $html .= '
'; + $html .= '
'; + $html .= $scopeId; + $html .= '
'; + $html .= '
'; + $html .= ''; // TODO: Insert sync url + $html .= '
'; + $html .= '
'; + $html .= ''; + } + return $html; + } + + /** + * The only way to get the path is by the name of the element (i.e. groups[unsecure][fields][base_media_url][value]) + * Magento does not provide the path at this point + * + * @param string $name + * @return string + */ + protected function _getPathByName($name) + { + $path = ''; + $name = explode("[", $name); + $path .= $this->getRequest()->getParam('section'); + $path .= '/' . substr_replace($name[1] ,"",-1); + $path .= '/' . substr_replace($name[3] ,"",-1); + return $path; + } + + /** + * Returns the element html rendered with the config value of the other system + * Also deletes the id and name attribute + * + * @param AbstractElement $element + * @param $value + * @return mixed|string + */ + protected function _getDiffElementHtml(AbstractElement $element, $value) + { + if($element->getRenderer() instanceof Regexceptions){ + $value = unserialize($value); + } + + // set the value of the remote system + $element->setValue($value); + + // get the rendered html + $html = $this->_getElementHtml($element); + + // adapt html + $doc = new \DOMDocument(); + $doc->loadHTML(utf8_decode($html)); + + // remove !DOCTYPE tag + $doc->removeChild($doc->firstChild); + + // throw away the id and name attribute of the field html + // to avoid changes made in this form field get saved on submit button click + /* @var \DOMNode $node */ + foreach($doc->getElementsByTagName('input') as $node){ + if($node->hasAttribute('type') && $node->getAttribute('type') === 'hidden'){ + $node->parentNode->removeChild($node); + continue; + } + $node->removeAttribute('id'); + $node->removeAttribute('name'); + } + + foreach($doc->getElementsByTagName('select') as $node){ + $node->removeAttribute('id'); + $node->removeAttribute('name'); + } + + foreach($doc->getElementsByTagName('textarea') as $node){ + $node->removeAttribute('id'); + $node->removeAttribute('name'); + } + + // get all script nodes as string to adapt them + $script = ''; + foreach($doc->getElementsByTagName('script') as $node){ + $script .= $doc->saveHtml($node); + $node->parentNode->removeChild($node); + } + + // remove all id and name attributes inside the script + $script = preg_replace('/id\="[^"]*"/', '', $script); + $script = preg_replace('/name\="[^"]*"/', '', $script); + + // add the script at the end of the html + $html = utf8_encode($doc->saveHTML()) . $script; + + // throw away html and body tags + $html = str_replace(array('', '', '', ''), array('', '', '', ''), $html); + + return $html; + } +} \ No newline at end of file diff --git a/Helper/Config.php b/Helper/Config.php index 051c551..9f1a472 100644 --- a/Helper/Config.php +++ b/Helper/Config.php @@ -26,6 +26,7 @@ class Config const XML_PATH_REMOTE_SYSTEM_URL = 'system_diff/connection/remote_system_url'; const XML_PATH_ACCESS_TOKEN = 'system_diff/connection/access_token'; const XML_PATH_API_TYPE = 'system_diff/connection/api_type'; + const XML_PATH_DISPLAY_STORE_CONFIG = 'system_diff/display/store_config'; /** * @var ScopeConfigInterface @@ -75,4 +76,12 @@ public function getRemoteSystemAccessToken() { return $this->scopeConfig->getValue(self::XML_PATH_ACCESS_TOKEN); } + + /** + * @return bool + */ + public function isDisplayStoreConfig() + { + return $this->scopeConfig->isSetFlag(self::XML_PATH_DISPLAY_STORE_CONFIG); + } } \ No newline at end of file diff --git a/Model/DiffConfig.php b/Model/DiffConfig.php index 3b97dd9..2b80f6d 100644 --- a/Model/DiffConfig.php +++ b/Model/DiffConfig.php @@ -9,11 +9,15 @@ namespace Magenerds\SystemDiff\Model; +use Magento\Framework\Model\AbstractModel; +use Magenerds\SystemDiff\Model\ResourceModel\DiffConfig as DiffConfigResource; + /** * Class DiffConfig + * @method getDiffValueRemote() * @package Magenerds\SystemDiff\Model */ -class DiffConfig extends \Magento\Framework\Model\AbstractModel +class DiffConfig extends AbstractModel { /** * Initialize resource @@ -22,7 +26,6 @@ class DiffConfig extends \Magento\Framework\Model\AbstractModel */ protected function _construct() { - $this->_init(\Magenerds\SystemDiff\Model\ResourceModel\DiffConfig::class); + $this->_init(DiffConfigResource::class); } - } \ No newline at end of file diff --git a/Model/ResourceModel/DiffConfig/Collection.php b/Model/ResourceModel/DiffConfig/Collection.php index 2992d79..dc35c65 100644 --- a/Model/ResourceModel/DiffConfig/Collection.php +++ b/Model/ResourceModel/DiffConfig/Collection.php @@ -9,11 +9,15 @@ namespace Magenerds\SystemDiff\Model\ResourceModel\DiffConfig; +use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection; +use Magenerds\SystemDiff\Model\DiffConfig; +use Magenerds\SystemDiff\Model\ResourceModel\DiffConfig as DiffConfigResource; + /** * Class Collection * @package Magenerds\SystemDiff\Model\ResourceModel\DiffConfig */ -class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection +class Collection extends AbstractCollection { /** * Resource initialization @@ -22,8 +26,8 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab */ protected function _construct() { - $this->_init(\Magenerds\SystemDiff\Model\DiffConfig::class, - \Magenerds\SystemDiff\Model\ResourceModel\DiffConfig::class + $this->_init(DiffConfig::class, + DiffConfigResource::class ); } } \ No newline at end of file diff --git a/Observer/StoreConfigDesign.php b/Observer/StoreConfigDesign.php new file mode 100644 index 0000000..000dafb --- /dev/null +++ b/Observer/StoreConfigDesign.php @@ -0,0 +1,63 @@ +assetRepository = $assetRepository; + $this->assetCollection = $assetCollection; + $this->config = $config; + } + + /** + * Adds custom design css if diff displaying is enabled + * + * @param Observer $observer + */ + public function execute(Observer $observer) + { + if ($this->config->isDisplayStoreConfig()) { + $asset = $this->assetRepository->createAsset('Magenerds_SystemDiff::css/style.css'); + $this->assetCollection->add('magenerds_systemdiff', $asset); + } + } +} \ No newline at end of file diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index f5f2d00..dba178b 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -22,7 +22,7 @@ - Flag for showing the differences in the backend + Flag for enabling the functionality Magento\Config\Model\Config\Source\Yesno @@ -37,6 +37,14 @@ Access Token of the integration user on the remote system. This user has to have the checkmark on Stores > Settings > Configuration > SystemDiff Section
+ + + + + Flag for showing the store configuration differences + Magento\Config\Model\Config\Source\Yesno + + \ No newline at end of file diff --git a/etc/config.xml b/etc/config.xml new file mode 100644 index 0000000..b5d1648 --- /dev/null +++ b/etc/config.xml @@ -0,0 +1,20 @@ + + + + + + + 1 + + + + \ No newline at end of file diff --git a/etc/di.xml b/etc/di.xml index bf67a1a..302e3c1 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -17,16 +17,19 @@ type="Magenerds\SystemDiff\Service\FetchLocalDataService" /> + type="Magenerds\SystemDiff\Remote\Client" /> + type="Magenerds\SystemDiff\Service\PerformSystemDiffService" /> - + + + diff --git a/etc/events.xml b/etc/events.xml new file mode 100644 index 0000000..a2edfd3 --- /dev/null +++ b/etc/events.xml @@ -0,0 +1,16 @@ + + + + + + + \ No newline at end of file diff --git a/view/adminhtml/web/css/style.css b/view/adminhtml/web/css/style.css new file mode 100644 index 0000000..18e58a2 --- /dev/null +++ b/view/adminhtml/web/css/style.css @@ -0,0 +1,28 @@ +/** + * NOTICE OF LICENSE + * + * This source file is subject to the Open Software License (OSL 3.0) + * that is available through the world-wide-web at this URL: + * http://opensource.org/licenses/osl-3.0.php + */ +.accordion .config td.label { + width: 20%; +} + +.accordion .config td.value { + width: 35%; +} + +.accordion .config td.sync { + width: 10%; +} + +.action-sync:before { + font-family: 'Admin Icons'; + font-weight: 700; + content: '\e629'; +} + +.diff-hidden { + display: none; +} \ No newline at end of file From 7aa8f255ec1b49e2471e3e82adf7078342e0b22e Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Sat, 30 Sep 2017 16:08:51 +0200 Subject: [PATCH 34/62] [MAGEHACKMUC-17] Use if ConfigDataInterface for API data --- Api/Data/ConfigDataInterface.php | 2 +- Model/ConfigData.php | 4 ++-- Remote/SoapClient.php | 15 ++++++++++----- Service/FetchLocalDataService.php | 2 +- Service/PerformSystemDiffService.php | 8 ++++---- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/Api/Data/ConfigDataInterface.php b/Api/Data/ConfigDataInterface.php index 6fbccde..6563094 100644 --- a/Api/Data/ConfigDataInterface.php +++ b/Api/Data/ConfigDataInterface.php @@ -7,7 +7,7 @@ interface ConfigDataInterface { /** - * @return mixed + * @return mixed|array */ public function getData(); } \ No newline at end of file diff --git a/Model/ConfigData.php b/Model/ConfigData.php index 244d269..1b6a822 100644 --- a/Model/ConfigData.php +++ b/Model/ConfigData.php @@ -18,10 +18,10 @@ public function __construct(array $data) } /** - * @return mixed + * @return mixed|array */ public function getData() { - return $this->data; + return (array)$this->data; } } \ No newline at end of file diff --git a/Remote/SoapClient.php b/Remote/SoapClient.php index 3cf1b8f..b2e0653 100644 --- a/Remote/SoapClient.php +++ b/Remote/SoapClient.php @@ -9,7 +9,7 @@ class SoapClient extends AbstractClient implements ClientInterface { /** - * @return \ArrayObject + * @return ConfigData */ public function fetch() { @@ -28,12 +28,17 @@ public function fetch() ] ); $response = $httpClient->magenerdsSystemDiffServiceFetchLocalDataServiceV1Fetch(); - - if (isset($response->result->item)) { - return new ConfigData(json_decode(json_encode($response->result->item), true)); + $data = []; + + if (isset($response->result->data->string) + && is_array($response->result->data->string) + ) { + foreach ($response->result->data->string as $poolData) { + $data[] = json_decode($poolData); + } } - return new \ArrayObject(); + return new ConfigData($data); } } \ No newline at end of file diff --git a/Service/FetchLocalDataService.php b/Service/FetchLocalDataService.php index 3aa37b8..05dcc7e 100644 --- a/Service/FetchLocalDataService.php +++ b/Service/FetchLocalDataService.php @@ -31,7 +31,7 @@ public function __construct(DataReaderPool $dataReaderPool) } /** - * @return \ArrayObject + * @return \Magenerds\SystemDiff\Api\Data\ConfigDataInterface */ public function fetch() { diff --git a/Service/PerformSystemDiffService.php b/Service/PerformSystemDiffService.php index ceb5ec8..a692c12 100644 --- a/Service/PerformSystemDiffService.php +++ b/Service/PerformSystemDiffService.php @@ -74,12 +74,12 @@ public function __construct( public function performDiff() { if ($this->config->isEnabled()) { - $localData = $this->fetchLocalDataService->fetch(); - $remoteData = $this->fetchRemoteDataService->fetch(); + $localData = $this->fetchLocalDataService->fetch()->getData(); + $remoteData = $this->fetchRemoteDataService->fetch()->getData(); - //$diffData = $this->diffDataService->diffData(((array)$localData), ((array)$remoteData)); + $diffData = $this->diffDataService->diffData(((array)$localData), ((array)$remoteData)); - //$this->saveDiffToTableService->saveData($diffData); + $this->saveDiffToTableService->saveData($diffData); } } } \ No newline at end of file From 27d9bf839825d8238169847bdfb6e0176c4cc354 Mon Sep 17 00:00:00 2001 From: Julien Wiedner Date: Sat, 30 Sep 2017 16:13:10 +0200 Subject: [PATCH 35/62] [MAGEHACKMUC-17] Added checks --- DataWriter/StoreConfigDataWriter.php | 29 ++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/DataWriter/StoreConfigDataWriter.php b/DataWriter/StoreConfigDataWriter.php index 7a91341..b74f5f0 100644 --- a/DataWriter/StoreConfigDataWriter.php +++ b/DataWriter/StoreConfigDataWriter.php @@ -27,8 +27,10 @@ class StoreConfigDataWriter implements DataWriterInterface const DEFAULT_SCOPE_ID = 0; const ARRAY_INDEX_LOCAL = 1; - const ARRAY_INDEX_REMOTE = 2; + const ARRAY_INDEX_REMOTE = self::EXPECTED_SPLITTED_PATH_LENGTH; const ARRAY_STORE_CONFIG_KEY = 'storeConfig'; + const SCOPE_KEYS = ['default', 'websites', 'stores']; + const EXPECTED_SPLITTED_PATH_LENGTH = 2; /** * @var DiffConfigResource @@ -65,9 +67,23 @@ public function __construct( */ public function write(array $diffData) { + if (empty($diffData) || !array_key_exists(self::ARRAY_STORE_CONFIG_KEY, $diffData)) { + return; + } + $diffData = $diffData[self::ARRAY_STORE_CONFIG_KEY]; foreach ($diffData as $scope => $data) { + if (!in_array($scope, self::SCOPE_KEYS)) { + continue; + } + + if (!array_key_exists(self::ARRAY_INDEX_LOCAL, $data) + || !array_key_exists(self::ARRAY_INDEX_REMOTE, $data) + ) { + continue; + } + $localValues = $data[self::ARRAY_INDEX_LOCAL]; $remoteValues = $data[self::ARRAY_INDEX_REMOTE]; @@ -101,7 +117,16 @@ protected function mapDataToModels(array $data, $scope, $valueField) $scopeId = self::DEFAULT_SCOPE_ID; if ($scope === self::SCOPE_VALUE_WEBSITES || $scope === self::SCOPE_VALUE_STORES) { - $splittedPath = explode('/', $path, 2); + if (empty($path)) { + continue; + } + + $splittedPath = explode('/', $path, self::EXPECTED_SPLITTED_PATH_LENGTH); + + if (count($splittedPath) !== self::EXPECTED_SPLITTED_PATH_LENGTH) { + continue; + } + $code = $splittedPath[0]; $path = $splittedPath[1]; From 4ae924a9eb57a6a495b2e5284065350708a3093a Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Sat, 30 Sep 2017 17:19:56 +0200 Subject: [PATCH 36/62] [MAGEHACKMUC-17] Fixed problem with missing associative array keys in API response --- Api/Data/ConfigDataInterface.php | 4 ++++ Model/ConfigData.php | 7 +++++-- Remote/AbstractClient.php | 14 ++++++++++++++ Remote/RestClient.php | 10 +++++++--- Remote/SoapClient.php | 14 ++++---------- Service/PerformSystemDiffService.php | 4 ++-- 6 files changed, 36 insertions(+), 17 deletions(-) diff --git a/Api/Data/ConfigDataInterface.php b/Api/Data/ConfigDataInterface.php index 6563094..f4374c6 100644 --- a/Api/Data/ConfigDataInterface.php +++ b/Api/Data/ConfigDataInterface.php @@ -7,6 +7,10 @@ interface ConfigDataInterface { /** + * This method MUST return an array with an index "0" which contains the actual data. + * + * This is a requirement of the REST/SOAP API, else the first index is cut out. + * * @return mixed|array */ public function getData(); diff --git a/Model/ConfigData.php b/Model/ConfigData.php index 1b6a822..dbf705d 100644 --- a/Model/ConfigData.php +++ b/Model/ConfigData.php @@ -11,17 +11,20 @@ class ConfigData implements ConfigDataInterface /** * ConfigData constructor. + * + * @param array $data */ public function __construct(array $data) { - $this->data = new \ArrayObject($data); + $this->data = $data; } /** + * @inheritdoc * @return mixed|array */ public function getData() { - return (array)$this->data; + return [$this->data]; } } \ No newline at end of file diff --git a/Remote/AbstractClient.php b/Remote/AbstractClient.php index c268e8a..aef07a0 100644 --- a/Remote/AbstractClient.php +++ b/Remote/AbstractClient.php @@ -23,4 +23,18 @@ public function __construct(Config $configHelper) { $this->helper = $configHelper; } + + /** + * @param $json + * @return array + */ + protected function buildDataFromJson($json) + { + $responseData = json_decode($json, true); + if (is_array($responseData)) { + return $responseData; + } + + return []; + } } \ No newline at end of file diff --git a/Remote/RestClient.php b/Remote/RestClient.php index 9d97b28..1df2572 100644 --- a/Remote/RestClient.php +++ b/Remote/RestClient.php @@ -4,6 +4,7 @@ namespace Magenerds\SystemDiff\Remote; use Magenerds\SystemDiff\Api\Data\ConfigDataInterface; +use Magenerds\SystemDiff\Model\ConfigData; class RestClient extends AbstractClient implements ClientInterface { @@ -21,10 +22,13 @@ public function fetch() if ($response->isSuccessful() && strtolower($response->getHeader('Content-type')) === 'application/json; charset=utf-8' ) { - $data = json_decode($response->getBody()); - if (is_array($data)) { - return new \ArrayObject($data); + $responseData = json_decode($response->getBody()); + + if (isset($responseData->data[0])) { + return new ConfigData($this->buildDataFromJson($responseData->data[0])); } + + throw new \Exception("SOAP response could not be read"); } throw new \Exception("Request to remote was not successfull"); diff --git a/Remote/SoapClient.php b/Remote/SoapClient.php index b2e0653..b0176a0 100644 --- a/Remote/SoapClient.php +++ b/Remote/SoapClient.php @@ -28,17 +28,11 @@ public function fetch() ] ); $response = $httpClient->magenerdsSystemDiffServiceFetchLocalDataServiceV1Fetch(); - $data = []; - - if (isset($response->result->data->string) - && is_array($response->result->data->string) - ) { - foreach ($response->result->data->string as $poolData) { - $data[] = json_decode($poolData); - } + + if (isset($response->result->data->string)) { + return new ConfigData($this->buildDataFromJson($response->result->data->string)); } - return new ConfigData($data); + throw new \Exception("SOAP response could not be read"); } - } \ No newline at end of file diff --git a/Service/PerformSystemDiffService.php b/Service/PerformSystemDiffService.php index a692c12..e3ff1c6 100644 --- a/Service/PerformSystemDiffService.php +++ b/Service/PerformSystemDiffService.php @@ -74,8 +74,8 @@ public function __construct( public function performDiff() { if ($this->config->isEnabled()) { - $localData = $this->fetchLocalDataService->fetch()->getData(); - $remoteData = $this->fetchRemoteDataService->fetch()->getData(); + $localData = $this->fetchLocalDataService->fetch()->getData()[0]; + $remoteData = $this->fetchRemoteDataService->fetch()->getData()[0]; $diffData = $this->diffDataService->diffData(((array)$localData), ((array)$remoteData)); From b0b6c634b98cf1b2b8f255f10db0a65ccba1c95c Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Sat, 30 Sep 2017 17:30:17 +0200 Subject: [PATCH 37/62] [MAGEHACKMUC-17] Removed MockDataReader which was only for debugging purposes --- DataReader/MockDataReader.php | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 DataReader/MockDataReader.php diff --git a/DataReader/MockDataReader.php b/DataReader/MockDataReader.php deleted file mode 100644 index dec7ba5..0000000 --- a/DataReader/MockDataReader.php +++ /dev/null @@ -1,15 +0,0 @@ - ['path' => 'value']]; - // TODO: Implement read() method. - } -} \ No newline at end of file From bb4b34920df407f3b4d287f0cb89e9fed70cce89 Mon Sep 17 00:00:00 2001 From: Julien Wiedner Date: Sun, 1 Oct 2017 09:50:54 +0200 Subject: [PATCH 38/62] [MAGEHACKMUC-17] Remove non existent preference --- etc/di.xml | 3 --- 1 file changed, 3 deletions(-) diff --git a/etc/di.xml b/etc/di.xml index fbf11fb..3d37f62 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -16,9 +16,6 @@ - - From fb0264f927fe73405aa361e195d0a165cf334c02 Mon Sep 17 00:00:00 2001 From: Alexander Dite Date: Sun, 1 Oct 2017 10:20:43 +0200 Subject: [PATCH 39/62] [MAGEHACKMUC-17] update installer: add unique key in table; add method in resource to clear current config data (truncate) --- Model/ResourceModel/DiffConfig.php | 8 ++++++++ Setup/InstallSchema.php | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/Model/ResourceModel/DiffConfig.php b/Model/ResourceModel/DiffConfig.php index 4a64c0e..290923c 100644 --- a/Model/ResourceModel/DiffConfig.php +++ b/Model/ResourceModel/DiffConfig.php @@ -26,4 +26,12 @@ protected function _construct() { $this->_init('magenerds_systemdiff_diff_config', 'diff_value_id'); } + + /** + * Clear the actual config data + */ + public function clearConfigData() + { + $this->getConnection()->truncateTable($this->getMainTable()); + } } \ No newline at end of file diff --git a/Setup/InstallSchema.php b/Setup/InstallSchema.php index b1b7648..7dcc15f 100644 --- a/Setup/InstallSchema.php +++ b/Setup/InstallSchema.php @@ -57,6 +57,14 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, null, ['nullable' => false, 'primary' => false] + )->addIndex( + $setup->getIdxName( + 'magenerds_systemdiff_diff_config', + ['scope', 'scope_id', 'path'], + \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE + ), + ['scope', 'scope_id', 'path'], + ['type' => \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE] ); $setup->getConnection()->createTable($table); From 1d4ec5daead96074daa330a486c85b307d2de093 Mon Sep 17 00:00:00 2001 From: Florian Sydekum Date: Sun, 1 Oct 2017 10:43:41 +0200 Subject: [PATCH 40/62] [MAGEHACKMUC-17] Fixed console command to work with setup:di:compile --- Console/Command/ExecuteCommand.php | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/Console/Command/ExecuteCommand.php b/Console/Command/ExecuteCommand.php index 6af0fa6..7409bd2 100644 --- a/Console/Command/ExecuteCommand.php +++ b/Console/Command/ExecuteCommand.php @@ -16,6 +16,16 @@ class ExecuteCommand extends Command { + /** + * Holds the command name + */ + const COMMAND_NAME = 'system-diff:execute'; + + /** + * Holds the command description + */ + const COMMAND_DESCRIPTION = 'system-diff:execute'; + /** * @var PerformSystemDiffService */ @@ -23,15 +33,12 @@ class ExecuteCommand extends Command /** * ExecuteCommand constructor. - * @param null $name * @param PerformSystemDiffService $performSystemDiffService */ public function __construct( - PerformSystemDiffService $performSystemDiffService, - $name = null + PerformSystemDiffService $performSystemDiffService ){ - parent::__construct($name); - + parent::__construct(self::COMMAND_NAME); $this->performSystemDiffService = $performSystemDiffService; } @@ -40,8 +47,8 @@ public function __construct( */ public function configure() { - $this->setName('system-diff:execute'); - $this->setDescription('system-diff:execute'); + $this->setName(self::COMMAND_NAME); + $this->setDescription(self::COMMAND_DESCRIPTION); parent::configure(); } From f5bf9827472f52e4ad7620497843996d9cf57944 Mon Sep 17 00:00:00 2001 From: Julien Wiedner Date: Sun, 1 Oct 2017 10:47:35 +0200 Subject: [PATCH 41/62] [MAGEHACKMUC-17] Fix saving diff to table (truncate, values in both systems) --- DataWriter/StoreConfigDataWriter.php | 39 ++++- .../DataWriter/StoreConfigDataWriterTest.php | 154 ++++++++++++++++++ 2 files changed, 190 insertions(+), 3 deletions(-) create mode 100644 Test/Unit/DataWriter/StoreConfigDataWriterTest.php diff --git a/DataWriter/StoreConfigDataWriter.php b/DataWriter/StoreConfigDataWriter.php index b74f5f0..5416f45 100644 --- a/DataWriter/StoreConfigDataWriter.php +++ b/DataWriter/StoreConfigDataWriter.php @@ -67,6 +67,8 @@ public function __construct( */ public function write(array $diffData) { + $this->clearCurrentData(); + if (empty($diffData) || !array_key_exists(self::ARRAY_STORE_CONFIG_KEY, $diffData)) { return; } @@ -87,10 +89,25 @@ public function write(array $diffData) $localValues = $data[self::ARRAY_INDEX_LOCAL]; $remoteValues = $data[self::ARRAY_INDEX_REMOTE]; + $combinedValues = []; + + foreach ($localValues as $localPath => $localValue) { + if (array_key_exists($localPath, $remoteValues)) { + $combinedValues[$localPath] = [ + self::LOCAL_VALUE_FIELD_NAME => $localValue, + self::REMOTE_VALUE_FIELD_NAME => $remoteValues[$localPath] + ]; + + unset($localValues[$localPath]); + unset($remoteValues[$localPath]); + } + } + $localModels = $this->mapDataToModels($localValues, $scope, self::LOCAL_VALUE_FIELD_NAME); $remoteModels = $this->mapDataToModels($remoteValues, $scope, self::REMOTE_VALUE_FIELD_NAME); + $combinedModels = $this->mapDataToModels($combinedValues, $scope); - $scopeModels = array_merge($localModels, $remoteModels); + $scopeModels = array_merge($localModels, $remoteModels, $combinedModels); foreach ($scopeModels as $scopeModel) { $this->diffConfigResource->save($scopeModel); @@ -98,6 +115,14 @@ public function write(array $diffData) } } + /** + * Clear current data in the beginning + */ + protected function clearCurrentData() + { + $this->diffConfigResource->clearConfigData(); + } + /** * @param array $data * @param string $scope @@ -105,7 +130,7 @@ public function write(array $diffData) * * @return DiffConfigModel[] */ - protected function mapDataToModels(array $data, $scope, $valueField) + protected function mapDataToModels(array $data, $scope, $valueField = null) { $models = []; @@ -141,7 +166,15 @@ protected function mapDataToModels(array $data, $scope, $valueField) $diffConfigModel->setData(self::SCOPE_ID_FIELD_NAME, $scopeId); $diffConfigModel->setData(self::PATH_FIELD_NAME, $path); - $diffConfigModel->setData($valueField, $value); + + if (is_array($value) && is_null($valueField)) { + foreach ($value as $fieldName => $v) { + $diffConfigModel->setData($fieldName, $v); + } + } else { + $diffConfigModel->setData($valueField, $value); + } + $models[] = $diffConfigModel; } diff --git a/Test/Unit/DataWriter/StoreConfigDataWriterTest.php b/Test/Unit/DataWriter/StoreConfigDataWriterTest.php new file mode 100644 index 0000000..c3d78eb --- /dev/null +++ b/Test/Unit/DataWriter/StoreConfigDataWriterTest.php @@ -0,0 +1,154 @@ +diffConfigResourceMock = $this->getMockBuilder(DiffConfigResource::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->diffConfigModelFactoryMock = $this->getMockBuilder(DiffConfigModelFactory::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->diffConfigModelMock = $this->getMockBuilder(DiffConfigModel::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->writer = new StoreConfigDataWriter( + $this->diffConfigResourceMock, + $this->diffConfigModelFactoryMock, + $this->storeManagerMock + ); + + $this->writerPartialMock = $this->getMockBuilder(StoreConfigDataWriter::class) + ->setConstructorArgs([ + $this->diffConfigResourceMock, + $this->diffConfigModelFactoryMock, + $this->storeManagerMock + ]) + ->setMethods(['mapDataToModels']) + ->getMock(); + + + } + + /** + * Dataprovider for testWrite Method + * + * @return array + */ + public function writeDataProvider() + { + return [ + 'emptyInput' => [ + [] + ], + 'missingKey' => [ + [ + 'testKey123' => [ + [ + 'default' => [ + 1 => 'test1', + 2 => 'test2' + ] + ] + ] + ] + ], + 'emptyStoreConfig' => [ + ['storeConfig' => []] + ], + 'wrongScopeKey' => [ + [ + 'storeConfig' => [ + 'testKey' => [ + [ + 'default' => 'testValue' + ] + ] + ] + ] + ], + 'missingLocalKey' => [ + [ + 'storeConfig' => [ + 'testKey' => [ + [ + 'default' => [ + 1 => 'test1', + 2 => 'test2' + ] + ] + ] + ] + ] + ] + ]; + } + + /** + * @test + * + * @param array $inputData + * + * @dataProvider writeDataProvider + */ + public function testWrite(array $inputData) + { + $this->writerPartialMock->expects($this->never())->method('mapDataToModels'); + $this->diffConfigResourceMock->expects($this->never())->method('save'); + $this->writerPartialMock->write($inputData); + } +} From 4d068be0c5389ddf776c803824ef14b576acdb3f Mon Sep 17 00:00:00 2001 From: Julien Wiedner Date: Sun, 1 Oct 2017 11:54:07 +0200 Subject: [PATCH 42/62] [MAGEHACKMUC-17] Fix for empty values --- Differ/StoreConfigDiffer.php | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/Differ/StoreConfigDiffer.php b/Differ/StoreConfigDiffer.php index f820535..da94c6d 100644 --- a/Differ/StoreConfigDiffer.php +++ b/Differ/StoreConfigDiffer.php @@ -38,6 +38,13 @@ public function diff(array $localData, array $remoteData) $remoteConfig['websites'] = $this->flattenArray($remoteData['websites'], ''); $remoteConfig['stores'] = $this->flattenArray($remoteData['stores'], ''); + $localConfig['default'] = $this->filterEmptyValues($localConfig['default']); + $localConfig['websites'] = $this->filterEmptyValues($localConfig['websites']); + $localConfig['stores'] = $this->filterEmptyValues($localConfig['stores']); + $remoteConfig['default'] = $this->filterEmptyValues($remoteConfig['default']); + $remoteConfig['websites'] = $this->filterEmptyValues($remoteConfig['websites']); + $remoteConfig['stores'] = $this->filterEmptyValues($remoteConfig['stores']); + $diff = []; $diff['default'] = $this->diffArrays($localConfig['default'], $remoteConfig['default']); $diff['websites'] = $this->diffArrays($localConfig['websites'], $remoteConfig['websites']); @@ -87,11 +94,11 @@ protected function flattenArray($arr, $path) { $result = []; - if(!is_array($arr)){ + if (!is_array($arr)) { return [ltrim($path, '/') => $arr]; } - foreach($arr as $key => $value){ + foreach ($arr as $key => $value) { $_path = $path; $_path = $_path . '/' . $key; $res = $this->flattenArray($value, $_path); @@ -100,4 +107,19 @@ protected function flattenArray($arr, $path) return $result; } + + /** + * @param array $data + * @return array + */ + protected function filterEmptyValues(array $data) + { + foreach ($data as $path => $value) { + if (empty($value)) { + unset($data[$path]); + } + } + + return $data; + } } \ No newline at end of file From a56ad173fb48bdcec5ce6091f1c29024ed35c3c6 Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Sun, 1 Oct 2017 13:32:58 +0200 Subject: [PATCH 43/62] [MAGEHACKMUC-17] Removed ClientAdapterInterface --- Remote/ClientAdapter.php | 18 +++++++++++++++--- Remote/ClientAdapterInterface.php | 15 --------------- Service/FetchRemoteDataService.php | 13 +++---------- etc/di.xml | 3 +++ 4 files changed, 21 insertions(+), 28 deletions(-) delete mode 100644 Remote/ClientAdapterInterface.php diff --git a/Remote/ClientAdapter.php b/Remote/ClientAdapter.php index c88a8fc..a6e632f 100644 --- a/Remote/ClientAdapter.php +++ b/Remote/ClientAdapter.php @@ -6,12 +6,18 @@ use Magento\Framework\ObjectManager\ObjectManager; use Magento\Framework\ObjectManagerInterface; -class ClientAdapter implements ClientAdapterInterface +class ClientAdapter implements ClientInterface { + /** + * @var ClientInterface + */ + protected $subject; + /** * @var Config */ private $configHelper; + /** * @var ObjectManager */ @@ -19,6 +25,7 @@ class ClientAdapter implements ClientAdapterInterface /** * ClientAdapter constructor. + * * @param Config $configHelper * @param ObjectManagerInterface $objectManager */ @@ -28,7 +35,12 @@ public function __construct(Config $configHelper, ObjectManagerInterface $object $this->objectManager = $objectManager; } - public function getClient() : ClientInterface { - return $this->objectManager->get($this->configHelper->getApiType()); + public function fetch() + { + if (null === $this->subject) { + $this->subject = $this->objectManager->get($this->configHelper->getApiType()); + } + + return $this->subject->fetch(); } } \ No newline at end of file diff --git a/Remote/ClientAdapterInterface.php b/Remote/ClientAdapterInterface.php deleted file mode 100644 index 6589409..0000000 --- a/Remote/ClientAdapterInterface.php +++ /dev/null @@ -1,15 +0,0 @@ -clientAdapter = $clientAdapter; - $this->client = $this->clientAdapter->getClient(); + $this->client = $clientAdapter; } /** diff --git a/etc/di.xml b/etc/di.xml index 3d37f62..502d414 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -10,6 +10,9 @@ --> + + From f51f70d83136ca0e54ed5bac4f475e21388cd71e Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Sun, 1 Oct 2017 13:36:41 +0200 Subject: [PATCH 44/62] [MAGEHACKMUC-17] OSL 3.0 license added --- Remote/ClientAdapter.php | 7 +++++++ Remote/RestClient.php | 8 +++++++- Remote/SoapClient.php | 10 ++++++++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Remote/ClientAdapter.php b/Remote/ClientAdapter.php index a6e632f..10ba92a 100644 --- a/Remote/ClientAdapter.php +++ b/Remote/ClientAdapter.php @@ -1,4 +1,11 @@ Date: Sun, 1 Oct 2017 13:40:32 +0200 Subject: [PATCH 45/62] [MAGEHACKMUC-17] Added asynchronous update function in order to update remote store configuration to local --- Block/System/Config/Form/Field.php | 4 +- Controller/Adminhtml/StoreConfig/Update.php | 98 +++++++++++++++++++ Helper/Config.php | 2 +- Helper/StoreConfigUpdater.php | 97 ++++++++++++++++++ Model/ResourceModel/DiffConfig/Collection.php | 4 +- etc/acl.xml | 1 + etc/adminhtml/routes.xml | 18 ++++ .../layout/adminhtml_system_config_edit.xml | 17 ++++ view/adminhtml/templates/storeconfig/js.phtml | 38 +++++++ view/adminhtml/web/css/style.css | 1 + 10 files changed, 274 insertions(+), 6 deletions(-) create mode 100644 Controller/Adminhtml/StoreConfig/Update.php create mode 100644 Helper/StoreConfigUpdater.php create mode 100644 etc/adminhtml/routes.xml create mode 100644 view/adminhtml/layout/adminhtml_system_config_edit.xml create mode 100644 view/adminhtml/templates/storeconfig/js.phtml diff --git a/Block/System/Config/Form/Field.php b/Block/System/Config/Form/Field.php index 0e32541..b3ed88f 100644 --- a/Block/System/Config/Form/Field.php +++ b/Block/System/Config/Form/Field.php @@ -119,7 +119,7 @@ protected function _renderDiffHtml(AbstractElement $element) $entry = $collection->getFirstItem(); if (count($entry->getData()) > 0) { $html .= ''; - $html .= ''; $html .= ''; @@ -138,7 +138,7 @@ protected function _renderDiffHtml(AbstractElement $element) $html .= $scopeId; $html .= ''; $html .= '
'; - $html .= ''; // TODO: Insert sync url + $html .= $this->getUrl('magenerds/storeConfig/update'); $html .= '
'; $html .= ''; $html .= ''; diff --git a/Controller/Adminhtml/StoreConfig/Update.php b/Controller/Adminhtml/StoreConfig/Update.php new file mode 100644 index 0000000..0dbdbdd --- /dev/null +++ b/Controller/Adminhtml/StoreConfig/Update.php @@ -0,0 +1,98 @@ +jsonFactory = $jsonFactory; + $this->storeConfigUpdater = $storeConfigUpdater; + } + + /** + * Updates store configuration + * + * @return Json + */ + public function execute() + { + $result = $this->jsonFactory->create(); + + if (!$params = $this->getParams()) { + $result->setHttpResponseCode(400); + return $result->setData(['errors' => true, 'message' => 'Missing parameters']); + } + + if ($this->getRequest()->isAjax()) { + $this->storeConfigUpdater->updateStoreConfig($params['path'], $params['scope'], $params['scopeId']); + $result->setHttpResponseCode(200); + return $result->setData([]); + } + } + + /** + * @return []|bool + */ + protected function getParams() + { + $params = $this->getRequest()->getParams(); + + if (!array_key_exists('path', $params) + || !array_key_exists('scope', $params) + || !array_key_exists('scopeId', $params)) + { + return false; + } + + return [ + 'path' => $params['path'], + 'scope' => $params['scope'], + 'scopeId' => $params['scopeId'] + ]; + } + + /** + * Check for is allowed + * + * @return boolean + */ + protected function _isAllowed() + { + return $this->_authorization->isAllowed('Magenerds_SystemDiff::store_config_update'); + } +} \ No newline at end of file diff --git a/Helper/Config.php b/Helper/Config.php index 9f1a472..c5b8141 100644 --- a/Helper/Config.php +++ b/Helper/Config.php @@ -31,7 +31,7 @@ class Config /** * @var ScopeConfigInterface */ - protected $scopeConfig; + private $scopeConfig; /** * Config constructor. diff --git a/Helper/StoreConfigUpdater.php b/Helper/StoreConfigUpdater.php new file mode 100644 index 0000000..981e88c --- /dev/null +++ b/Helper/StoreConfigUpdater.php @@ -0,0 +1,97 @@ +config = $config; + $this->collectionFactory = $collectionFactory; + $this->diffConfigResourceFactory = $diffConfigResourceFactory; + } + + /** + * Updates the local store configuration with the remote value + * + * @param $path + * @param $scope + * @param $scopeId + * @return bool + */ + public function updateStoreConfig($path, $scope, $scopeId) + { + /** @var $collection Collection */ + $collection = $this->collectionFactory->create(); + + $collection->addFieldToFilter('path', $path); + $collection->addFieldToFilter('scope', $scope); + $collection->addFieldToFilter('scope_id', $scopeId); + + if ($collection->count() === 0) { + return false; + } + + /** @var $entry DiffConfig */ + $entry = $collection->getFirstItem(); + $value = $entry->getDiffValueRemote(); + $this->saveStoreConfig($path, $value, $scope, $scopeId); + + /** @var $diffConfigResource DiffConfigResource */ + $diffConfigResource = $this->diffConfigResourceFactory->create(); + $diffConfigResource->delete($entry); + } + + /** + * Save given store configuration + * + * @param $path + * @param $value + * @param string $scope + * @param int $scopeId + */ + public function saveStoreConfig($path, $value, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = Store::DEFAULT_STORE_ID) + { + $this->config->saveConfig($path, $value, $scope, $scopeId); + } +} \ No newline at end of file diff --git a/Model/ResourceModel/DiffConfig/Collection.php b/Model/ResourceModel/DiffConfig/Collection.php index dc35c65..b49dcc8 100644 --- a/Model/ResourceModel/DiffConfig/Collection.php +++ b/Model/ResourceModel/DiffConfig/Collection.php @@ -26,8 +26,6 @@ class Collection extends AbstractCollection */ protected function _construct() { - $this->_init(DiffConfig::class, - DiffConfigResource::class - ); + $this->_init(DiffConfig::class, DiffConfigResource::class); } } \ No newline at end of file diff --git a/etc/acl.xml b/etc/acl.xml index 1eebff5..147b2bd 100644 --- a/etc/acl.xml +++ b/etc/acl.xml @@ -19,6 +19,7 @@ + diff --git a/etc/adminhtml/routes.xml b/etc/adminhtml/routes.xml new file mode 100644 index 0000000..a2cde09 --- /dev/null +++ b/etc/adminhtml/routes.xml @@ -0,0 +1,18 @@ + + + + + + + + + \ No newline at end of file diff --git a/view/adminhtml/layout/adminhtml_system_config_edit.xml b/view/adminhtml/layout/adminhtml_system_config_edit.xml new file mode 100644 index 0000000..a5614f5 --- /dev/null +++ b/view/adminhtml/layout/adminhtml_system_config_edit.xml @@ -0,0 +1,17 @@ + + + + + + + + + diff --git a/view/adminhtml/templates/storeconfig/js.phtml b/view/adminhtml/templates/storeconfig/js.phtml new file mode 100644 index 0000000..8edb3fd --- /dev/null +++ b/view/adminhtml/templates/storeconfig/js.phtml @@ -0,0 +1,38 @@ + + \ No newline at end of file diff --git a/view/adminhtml/web/css/style.css b/view/adminhtml/web/css/style.css index 18e58a2..93e0a41 100644 --- a/view/adminhtml/web/css/style.css +++ b/view/adminhtml/web/css/style.css @@ -11,6 +11,7 @@ .accordion .config td.value { width: 35%; + vertical-align: unset; } .accordion .config td.sync { From 58cd7144f42ebb24a942ae89451fcc74828cca7c Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Sun, 1 Oct 2017 13:53:59 +0200 Subject: [PATCH 46/62] [MAGEHACKMUC-17] Added Sync/Run/Diff button in backend --- Block/System/Config/Form/SyncButton.php | 94 +++++++++++++++++++ Controller/Adminhtml/SystemDiff/Diff.php | 75 +++++++++++++++ etc/adminhtml/routes.xml | 18 ++++ etc/adminhtml/system.xml | 5 + .../templates/system/config/sync_button.phtml | 45 +++++++++ 5 files changed, 237 insertions(+) create mode 100644 Block/System/Config/Form/SyncButton.php create mode 100644 Controller/Adminhtml/SystemDiff/Diff.php create mode 100644 etc/adminhtml/routes.xml create mode 100644 view/adminhtml/templates/system/config/sync_button.phtml diff --git a/Block/System/Config/Form/SyncButton.php b/Block/System/Config/Form/SyncButton.php new file mode 100644 index 0000000..644a669 --- /dev/null +++ b/Block/System/Config/Form/SyncButton.php @@ -0,0 +1,94 @@ +unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue(); + return parent::render($element); + } + + /** + * Return element html + * + * @param AbstractElement $element + * @return string + */ + protected function _getElementHtml(AbstractElement $element) + { + // used later in getButtonHtml + $this->element = $element; + + return $this->_toHtml(); + } + + /** + * Return ajax url for diff button + * + * @return string + */ + public function getAjaxUrl() + { + return $this->getUrl('magenerds/systemdiff/diff'); + } + + /** + * Generate diff button html + * + * @return string + */ + public function getButtonHtml() + { + /** @var \Magento\Backend\Block\Widget\Button $button */ + $button = $this->getLayout()->createBlock( + 'Magento\Backend\Block\Widget\Button' + ); + $button->setData( + [ + 'id' => $this->element->getHtmlId(), + 'label' => __('Run'), + 'class' => 'disabled' // reset when page loaded, see template script + ] + ); + + return $button->toHtml(); + } + + /** + * Returns the element defined in system.xml + * + * @return AbstractElement + */ + public function getElement() + { + return $this->element; + } +} \ No newline at end of file diff --git a/Controller/Adminhtml/SystemDiff/Diff.php b/Controller/Adminhtml/SystemDiff/Diff.php new file mode 100644 index 0000000..2918ba2 --- /dev/null +++ b/Controller/Adminhtml/SystemDiff/Diff.php @@ -0,0 +1,75 @@ +context = $context; + $this->jsonFactory = $jsonFactory; + $this->performSystemDiffService = $performSystemDiffService; + + parent::__construct($context); + } + + /** + * Perform system diff. + * + * @return \Magento\Framework\Controller\Result\Json + */ + public function execute() + { + + $result = $this->jsonFactory->create(); + + $message = 'OK'; + + try { + $this->performSystemDiffService->performDiff(); + } catch (\Exception $e) { + $message = "Error performing system diff."; + } + + $result->setData(['message' => $message]); + + return $result; + } +} \ No newline at end of file diff --git a/etc/adminhtml/routes.xml b/etc/adminhtml/routes.xml new file mode 100644 index 0000000..3741df1 --- /dev/null +++ b/etc/adminhtml/routes.xml @@ -0,0 +1,18 @@ + + + + + + + + + \ No newline at end of file diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 40878d3..05e1031 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -47,6 +47,11 @@ Access Token of the integration user on the remote system. This user has to have the checkmark on Stores > Settings > Configuration > SystemDiff Section + + Magenerds\SystemDiff\Block\System\Config\Form\SyncButton + + Run the sync/diff process now. + diff --git a/view/adminhtml/templates/system/config/sync_button.phtml b/view/adminhtml/templates/system/config/sync_button.phtml new file mode 100644 index 0000000..72e8a42 --- /dev/null +++ b/view/adminhtml/templates/system/config/sync_button.phtml @@ -0,0 +1,45 @@ + + + + +getButtonHtml() ?> \ No newline at end of file From d7b1d12cdc38eb94b3caf3181e41f036d594bb26 Mon Sep 17 00:00:00 2001 From: Florian Sydekum Date: Sun, 1 Oct 2017 14:06:42 +0200 Subject: [PATCH 47/62] [MAGEHACKMUC-17] Minor code style enhancements, license headers and refactorings --- Api/Data/ConfigDataInterface.php | 9 ++++++-- Api/Service/DiffDataServiceInterface.php | 4 ++-- .../FetchLocalDataServiceInterface.php | 4 +++- .../FetchRemoteDataServiceInterface.php | 4 +++- .../SaveDiffToTableServiceInterface.php | 2 +- Block/System/Config/Form/Field.php | 2 +- Controller/Adminhtml/StoreConfig/Update.php | 1 + DataReader/DataReaderInterface.php | 2 +- DataReader/StoreConfigDataReader.php | 2 +- DataWriter/DataWriterInterface.php | 2 +- DataWriter/DataWriterPool.php | 2 +- DataWriter/StoreConfigDataWriter.php | 12 +++++++--- Differ/AbstractDiffer.php | 12 +++++----- Differ/DifferInterface.php | 6 ++--- Differ/DifferPool.php | 2 +- Differ/StoreConfigDiffer.php | 4 ++-- Model/Config/Source/ApiType.php | 10 ++++---- Model/ConfigData.php | 17 ++++++++++---- README.md | 14 ++++++++--- Remote/AbstractClient.php | 8 +++++-- Remote/ClientAdapter.php | 11 ++++++++- Remote/ClientAdapterInterface.php | 8 ++++++- Remote/RestClient.php | 8 ++++++- Remote/SoapClient.php | 19 +++++++++------ Service/DiffDataService.php | 6 ++--- Service/FetchLocalDataService.php | 23 +++++++++++++++---- Service/FetchRemoteDataService.php | 1 + Service/PerformSystemDiffService.php | 6 ++--- Service/SaveDiffToTableService.php | 2 +- .../DataWriter/StoreConfigDataWriterTest.php | 5 +++- Test/Unit/Service/DiffDataServiceUnitTest.php | 7 ++++++ .../Service/PerformSystemDiffServiceTest.php | 7 ++++++ composer.json | 5 ++-- etc/adminhtml/system.xml | 6 ++--- etc/config.xml | 8 +++---- etc/module.xml | 2 +- etc/webapi.xml | 2 +- registration.php | 2 +- .../layout/adminhtml_system_config_edit.xml | 2 +- view/adminhtml/templates/storeconfig/js.phtml | 2 +- 40 files changed, 173 insertions(+), 78 deletions(-) diff --git a/Api/Data/ConfigDataInterface.php b/Api/Data/ConfigDataInterface.php index f4374c6..fb3ea18 100644 --- a/Api/Data/ConfigDataInterface.php +++ b/Api/Data/ConfigDataInterface.php @@ -1,9 +1,14 @@ getFirstItem(); if (count($entry->getData()) > 0) { $html .= ''; - $html .= ''; $html .= ''; diff --git a/Controller/Adminhtml/StoreConfig/Update.php b/Controller/Adminhtml/StoreConfig/Update.php index 0dbdbdd..de0b174 100644 --- a/Controller/Adminhtml/StoreConfig/Update.php +++ b/Controller/Adminhtml/StoreConfig/Update.php @@ -51,6 +51,7 @@ public function __construct( */ public function execute() { + /** @var $result Json */ $result = $this->jsonFactory->create(); if (!$params = $this->getParams()) { diff --git a/DataReader/DataReaderInterface.php b/DataReader/DataReaderInterface.php index a2ffccd..611021d 100644 --- a/DataReader/DataReaderInterface.php +++ b/DataReader/DataReaderInterface.php @@ -12,7 +12,7 @@ interface DataReaderInterface { /** - * @return array + * @return [] */ public function read(); } \ No newline at end of file diff --git a/DataReader/StoreConfigDataReader.php b/DataReader/StoreConfigDataReader.php index 6032c54..b1322af 100644 --- a/DataReader/StoreConfigDataReader.php +++ b/DataReader/StoreConfigDataReader.php @@ -31,7 +31,7 @@ public function __construct( /** * Reads the store configuration from database. * - * @return array + * @return [] */ public function read() { diff --git a/DataWriter/DataWriterInterface.php b/DataWriter/DataWriterInterface.php index 214430e..f5722d3 100644 --- a/DataWriter/DataWriterInterface.php +++ b/DataWriter/DataWriterInterface.php @@ -12,7 +12,7 @@ interface DataWriterInterface { /** - * @param array $diffData + * @param [] $diffData * @return void */ public function write(array $diffData); diff --git a/DataWriter/DataWriterPool.php b/DataWriter/DataWriterPool.php index e92f728..6292bc8 100644 --- a/DataWriter/DataWriterPool.php +++ b/DataWriter/DataWriterPool.php @@ -22,7 +22,7 @@ class DataWriterPool implements \IteratorAggregate /** * @param TMapFactory $tmapFactory - * @param array $dataWriters + * @param [] $dataWriters */ public function __construct( TMapFactory $tmapFactory, diff --git a/DataWriter/StoreConfigDataWriter.php b/DataWriter/StoreConfigDataWriter.php index 5416f45..7ae8a04 100644 --- a/DataWriter/StoreConfigDataWriter.php +++ b/DataWriter/StoreConfigDataWriter.php @@ -16,15 +16,20 @@ class StoreConfigDataWriter implements DataWriterInterface { + /** + * Holds database field names + */ const LOCAL_VALUE_FIELD_NAME = 'diff_value_local'; const REMOTE_VALUE_FIELD_NAME = 'diff_value_remote'; - const SCOPE_FIELD_NAME = 'scope'; const SCOPE_ID_FIELD_NAME = 'scope_id'; const PATH_FIELD_NAME = 'path'; const SCOPE_VALUE_WEBSITES = 'websites'; const SCOPE_VALUE_STORES = 'stores'; + /** + * Holds further necessary consts + */ const DEFAULT_SCOPE_ID = 0; const ARRAY_INDEX_LOCAL = 1; const ARRAY_INDEX_REMOTE = self::EXPECTED_SPLITTED_PATH_LENGTH; @@ -36,10 +41,12 @@ class StoreConfigDataWriter implements DataWriterInterface * @var DiffConfigResource */ private $diffConfigResource; + /** * @var DiffConfigFactory */ private $diffConfigFactory; + /** * @var StoreManagerInterface */ @@ -62,7 +69,7 @@ public function __construct( } /** - * @param array $diffData + * @param [] $diffData * @return void */ public function write(array $diffData) @@ -199,5 +206,4 @@ private function getStoreId($code): int { return $this->storeManager->getStore($code)->getId(); } - } \ No newline at end of file diff --git a/Differ/AbstractDiffer.php b/Differ/AbstractDiffer.php index 71b7e76..28ebbe9 100644 --- a/Differ/AbstractDiffer.php +++ b/Differ/AbstractDiffer.php @@ -14,18 +14,18 @@ abstract class AbstractDiffer implements DifferInterface /** * Diffs two data sets of two systems. * - * @param array $localData - * @param array $remoteData - * @return array + * @param [] $localData + * @param [] $remoteData + * @return [] */ abstract function diff(array $localData, array $remoteData); /** * Does the diff of two arrays and returns the diff as array. * - * @param array $arr1 - * @param array $arr2 - * @return array + * @param [] $arr1 + * @param [] $arr2 + * @return [] */ protected function diffArrays(array $arr1, array $arr2) { diff --git a/Differ/DifferInterface.php b/Differ/DifferInterface.php index 9f4d7d9..3abb5d9 100644 --- a/Differ/DifferInterface.php +++ b/Differ/DifferInterface.php @@ -14,9 +14,9 @@ interface DifferInterface /** * Diffs two data sets of two systems. * - * @param array $localData - * @param array $remoteData - * @return array + * @param [] $localData + * @param [] $remoteData + * @return [] */ public function diff(array $localData, array $remoteData); } \ No newline at end of file diff --git a/Differ/DifferPool.php b/Differ/DifferPool.php index f1f8abc..524076c 100644 --- a/Differ/DifferPool.php +++ b/Differ/DifferPool.php @@ -23,7 +23,7 @@ class DifferPool implements \IteratorAggregate /** * @param TMapFactory $tmapFactory - * @param array $differs + * @param [] $differs */ public function __construct( TMapFactory $tmapFactory, diff --git a/Differ/StoreConfigDiffer.php b/Differ/StoreConfigDiffer.php index da94c6d..dcb930d 100644 --- a/Differ/StoreConfigDiffer.php +++ b/Differ/StoreConfigDiffer.php @@ -109,8 +109,8 @@ protected function flattenArray($arr, $path) } /** - * @param array $data - * @return array + * @param [] $data + * @return [] */ protected function filterEmptyValues(array $data) { diff --git a/Model/Config/Source/ApiType.php b/Model/Config/Source/ApiType.php index 178aea3..83110ee 100644 --- a/Model/Config/Source/ApiType.php +++ b/Model/Config/Source/ApiType.php @@ -10,6 +10,9 @@ */ class ApiType implements ArrayInterface { + /** + * Holds the configuration key for api types + */ const XML_PATH_TYPES = 'system/magenerds/system_diff/api_types'; /** @@ -30,7 +33,7 @@ public function __construct(ScopeConfigInterface $scopeConfig) /** * Options getter * - * @return array + * @return [] */ public function toOptionArray() { @@ -48,7 +51,7 @@ public function toOptionArray() /** * Get options in "key-value" format * - * @return array + * @return [] */ public function toArray() { @@ -68,14 +71,13 @@ public function toArray() * * This is the place to extend the config with additional types. * - * @return array + * @return [] */ protected function getConfiguredOptions(): array { $types = $this->scopeConfig->getValue(self::XML_PATH_TYPES); if (is_array($types)) { - return $types; } diff --git a/Model/ConfigData.php b/Model/ConfigData.php index dbf705d..7b6d7db 100644 --- a/Model/ConfigData.php +++ b/Model/ConfigData.php @@ -1,18 +1,27 @@ objectManager = $objectManager; } - public function getClient() : ClientInterface { + public function getClient() : ClientInterface + { return $this->objectManager->get($this->configHelper->getApiType()); } } \ No newline at end of file diff --git a/Remote/ClientAdapterInterface.php b/Remote/ClientAdapterInterface.php index 6589409..3741171 100644 --- a/Remote/ClientAdapterInterface.php +++ b/Remote/ClientAdapterInterface.php @@ -1,5 +1,11 @@ [ - 'header' => "Authorization: Bearer {$this->helper->getRemoteSystemAccessToken()}" - ] - ); + $opts = [ + 'http' => [ + 'header' => "Authorization: Bearer {$this->helper->getRemoteSystemAccessToken()}" + ] + ]; $streamContext = stream_context_create($opts); diff --git a/Service/DiffDataService.php b/Service/DiffDataService.php index 59113e6..946ae56 100644 --- a/Service/DiffDataService.php +++ b/Service/DiffDataService.php @@ -30,9 +30,9 @@ public function __construct(DifferPool $differPool) } /** - * @param array $localData - * @param array $remoteData - * @return array + * @param [] $localData + * @param [] $remoteData + * @return [] */ public function diffData(array $localData, array $remoteData) { diff --git a/Service/FetchLocalDataService.php b/Service/FetchLocalDataService.php index 05dcc7e..44117f2 100644 --- a/Service/FetchLocalDataService.php +++ b/Service/FetchLocalDataService.php @@ -12,7 +12,8 @@ use Magenerds\SystemDiff\Api\Service\FetchLocalDataServiceInterface; use Magenerds\SystemDiff\DataReader\DataReaderPool; use Magenerds\SystemDiff\DataReader\DataReaderInterface; -use Magenerds\SystemDiff\Model\ConfigData; +use Magenerds\SystemDiff\Api\Data\ConfigDataInterface; +use Magenerds\SystemDiff\Api\Data\ConfigDataInterfaceFactory; class FetchLocalDataService implements FetchLocalDataServiceInterface { @@ -21,17 +22,26 @@ class FetchLocalDataService implements FetchLocalDataServiceInterface */ private $dataReaderPool; + /** + * @var ConfigDataInterfaceFactory + */ + private $configDataFactory; + /** * FetchLocalDataService constructor. * @param DataReaderPool $dataReaderPool + * @param ConfigDataInterfaceFactory $configDataFactory */ - public function __construct(DataReaderPool $dataReaderPool) - { + public function __construct( + DataReaderPool $dataReaderPool, + ConfigDataInterfaceFactory $configDataFactory + ){ $this->dataReaderPool = $dataReaderPool; + $this->configDataFactory = $configDataFactory; } /** - * @return \Magenerds\SystemDiff\Api\Data\ConfigDataInterface + * @return ConfigDataInterface */ public function fetch() { @@ -42,6 +52,9 @@ public function fetch() $data[$dataReaderCode] = $dataReader->read(); } - return new ConfigData($data); + /** @var $configData ConfigDataInterface */ + $configData = $this->configDataFactory->create($data); + + return $configData; } } \ No newline at end of file diff --git a/Service/FetchRemoteDataService.php b/Service/FetchRemoteDataService.php index 11d9c1d..2e9568c 100644 --- a/Service/FetchRemoteDataService.php +++ b/Service/FetchRemoteDataService.php @@ -21,6 +21,7 @@ class FetchRemoteDataService implements FetchRemoteDataServiceInterface * @var ClientInterface */ private $client; + /** * @var ClientAdapter */ diff --git a/Service/PerformSystemDiffService.php b/Service/PerformSystemDiffService.php index e3ff1c6..660db1f 100644 --- a/Service/PerformSystemDiffService.php +++ b/Service/PerformSystemDiffService.php @@ -24,17 +24,17 @@ class PerformSystemDiffService implements PerformSystemDiffServiceInterface /** * @var FetchLocalDataServiceInterface */ - protected $fetchLocalDataService; + private $fetchLocalDataService; /** * @var FetchRemoteDataServiceInterface */ - protected $fetchRemoteDataService; + private $fetchRemoteDataService; /** * @var DiffDataService */ - protected $diffDataService; + private $diffDataService; /** * @var SaveDiffToTableServiceInterface diff --git a/Service/SaveDiffToTableService.php b/Service/SaveDiffToTableService.php index e0c8369..8b22a0a 100644 --- a/Service/SaveDiffToTableService.php +++ b/Service/SaveDiffToTableService.php @@ -34,7 +34,7 @@ public function __construct(DataWriterPool $writerPool) } /** - * @param array $diffData + * @param [] $diffData * @return void */ public function saveData(array $diffData) diff --git a/Test/Unit/DataWriter/StoreConfigDataWriterTest.php b/Test/Unit/DataWriter/StoreConfigDataWriterTest.php index c3d78eb..0718905 100644 --- a/Test/Unit/DataWriter/StoreConfigDataWriterTest.php +++ b/Test/Unit/DataWriter/StoreConfigDataWriterTest.php @@ -48,6 +48,9 @@ class StoreConfigDataWriterTest extends TestCase */ private $writerPartialMock; + /** + * Set up function + */ public function setUp() { $this->diffConfigResourceMock = $this->getMockBuilder(DiffConfigResource::class) @@ -151,4 +154,4 @@ public function testWrite(array $inputData) $this->diffConfigResourceMock->expects($this->never())->method('save'); $this->writerPartialMock->write($inputData); } -} +} \ No newline at end of file diff --git a/Test/Unit/Service/DiffDataServiceUnitTest.php b/Test/Unit/Service/DiffDataServiceUnitTest.php index 5aa52cb..6f7b74e 100644 --- a/Test/Unit/Service/DiffDataServiceUnitTest.php +++ b/Test/Unit/Service/DiffDataServiceUnitTest.php @@ -1,4 +1,11 @@ - + Type of API to use: REST or SOAP. Magenerds\SystemDiff\Model\Config\Source\ApiType - + @@ -43,7 +43,7 @@ See http://devdocs.magento.com/guides/v2.2/get-started/soap/soap-web-api-calls.html ]]> - + Access Token of the integration user on the remote system. This user has to have the checkmark on Stores > Settings > Configuration > SystemDiff Section diff --git a/etc/config.xml b/etc/config.xml index bd7f6c9..ed9390f 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -16,18 +16,18 @@ 1 - \Magenerds\SystemDiff\Remote\SoapClient + Magenerds\SystemDiff\Remote\SoapClient - \Magenerds\SystemDiff\Remote\RestClient - \Magenerds\SystemDiff\Remote\SoapClient + Magenerds\SystemDiff\Remote\RestClient + Magenerds\SystemDiff\Remote\SoapClient -
+ \ No newline at end of file diff --git a/etc/module.xml b/etc/module.xml index 8958425..f6575f9 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -10,4 +10,4 @@ --> - + \ No newline at end of file diff --git a/etc/webapi.xml b/etc/webapi.xml index d510632..de678af 100644 --- a/etc/webapi.xml +++ b/etc/webapi.xml @@ -16,4 +16,4 @@ - + \ No newline at end of file diff --git a/registration.php b/registration.php index d5c54ac..5af2580 100644 --- a/registration.php +++ b/registration.php @@ -11,4 +11,4 @@ \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Magenerds_SystemDiff', __DIR__ -); +); \ No newline at end of file diff --git a/view/adminhtml/layout/adminhtml_system_config_edit.xml b/view/adminhtml/layout/adminhtml_system_config_edit.xml index a5614f5..fb585b8 100644 --- a/view/adminhtml/layout/adminhtml_system_config_edit.xml +++ b/view/adminhtml/layout/adminhtml_system_config_edit.xml @@ -14,4 +14,4 @@ - + \ No newline at end of file diff --git a/view/adminhtml/templates/storeconfig/js.phtml b/view/adminhtml/templates/storeconfig/js.phtml index 8edb3fd..74087d6 100644 --- a/view/adminhtml/templates/storeconfig/js.phtml +++ b/view/adminhtml/templates/storeconfig/js.phtml @@ -13,7 +13,7 @@ ], function ($) { "use strict"; - $('.config-diff-action').on('click', function() { + $('.sync-config').on('click', function() { var caller = this; var sibling = $(caller).parent().next(); var path = $(sibling).find(".path").text(); From 3afa20857460f946c2607403efb32f7c267297d3 Mon Sep 17 00:00:00 2001 From: Florian Sydekum Date: Sun, 1 Oct 2017 14:13:54 +0200 Subject: [PATCH 48/62] [MAGEHACKMUC-17] Changed documentation in README.md --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 766e1a6..a1eec6f 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,10 @@ in order to integrate more differs and readers. For general usage the functionality should be enabled in the system configuration. In order to connect two systems you need to configure the web service API. You can either choose REST or SOAP. Enter the url of the remote system and provide an access token. + +## Backend Usage +In the system configuration of the module you can find a `Run diff` button which triggers the sync to the remote system +and starts the diff. ## Command Line Usage There is a command available which currently outputs the differences of the data sets of both systems. You can use it @@ -18,7 +22,7 @@ like the following: ## Cron Job Usage There is a cron job defined which triggers a diff every hour. - + ## Integrate your own differs and readers We implemented differ and reader pools which hold concrete differ and reader implementations configured via `di.xml`. Of course it is necessary to add a differ and a compatible data reader. The data reader's job is to know how to read the From 61071f9de46276e809a8c12e8b701d67c901a90e Mon Sep 17 00:00:00 2001 From: Florian Sydekum Date: Sun, 1 Oct 2017 14:22:34 +0200 Subject: [PATCH 49/62] [MAGEHACKMUC-17] Minor fixes --- Api/Data/ConfigDataInterface.php | 2 +- etc/di.xml | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Api/Data/ConfigDataInterface.php b/Api/Data/ConfigDataInterface.php index fb3ea18..be382c7 100644 --- a/Api/Data/ConfigDataInterface.php +++ b/Api/Data/ConfigDataInterface.php @@ -16,7 +16,7 @@ interface ConfigDataInterface * * This is a requirement of the REST/SOAP API, else the first index is cut out. * - * @return mixed|array + * @return mixed|[] */ public function getData(); } \ No newline at end of file diff --git a/etc/di.xml b/etc/di.xml index 502d414..9ebab20 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -28,6 +28,9 @@ + + From 6d02abd0c15635067f75ba7f3149104358bb4c3b Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Sun, 1 Oct 2017 14:48:01 +0200 Subject: [PATCH 50/62] [MAGEHACKMUC-17] JS fix --- view/adminhtml/templates/storeconfig/js.phtml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/view/adminhtml/templates/storeconfig/js.phtml b/view/adminhtml/templates/storeconfig/js.phtml index 74087d6..23288f9 100644 --- a/view/adminhtml/templates/storeconfig/js.phtml +++ b/view/adminhtml/templates/storeconfig/js.phtml @@ -30,7 +30,7 @@ var toReplace = $(parent).find('.value')[0]; var replace = $(parent).find('.value')[1]; $(toReplace).replaceWith(replace); - $(parent).parent().find('.sync').remove(); + $(parent).find('.sync').remove(); } }); }); From a07644afbea874efd8df9d259f3403e2a92c47eb Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Sun, 1 Oct 2017 15:12:30 +0200 Subject: [PATCH 51/62] [MAGEHACKMUC-17] ConfigData via factory class --- Api/Service/FetchLocalDataServiceInterface.php | 4 +--- Remote/AbstractClient.php | 16 +++++++++++++--- Remote/RestClient.php | 5 ++--- Remote/SoapClient.php | 2 +- Service/FetchLocalDataService.php | 5 +---- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/Api/Service/FetchLocalDataServiceInterface.php b/Api/Service/FetchLocalDataServiceInterface.php index cb7d7f8..cacd7d7 100644 --- a/Api/Service/FetchLocalDataServiceInterface.php +++ b/Api/Service/FetchLocalDataServiceInterface.php @@ -9,8 +9,6 @@ namespace Magenerds\SystemDiff\Api\Service; -use Magenerds\SystemDiff\Api\Data\ConfigDataInterface; - /** * Return locally configured config data. * @@ -21,7 +19,7 @@ interface FetchLocalDataServiceInterface /** * Returns locally configured config data. * - * @return ConfigDataInterface + * @return \Magenerds\SystemDiff\Model\ConfigData */ public function fetch(); } \ No newline at end of file diff --git a/Remote/AbstractClient.php b/Remote/AbstractClient.php index dec7889..0c2f54c 100644 --- a/Remote/AbstractClient.php +++ b/Remote/AbstractClient.php @@ -10,22 +10,32 @@ namespace Magenerds\SystemDiff\Remote; use Magenerds\SystemDiff\Helper\Config; +use Magenerds\SystemDiff\Api\Data\ConfigDataInterfaceFactory; class AbstractClient { /** * @var Config */ - private $helper; + protected $helper; + + /** + * @var ConfigDataInterfaceFactory + */ + protected $configDataFactory; /** * Client constructor. * * @param Config $configHelper + * @param ConfigDataInterfaceFactory $configDataFactory */ - public function __construct(Config $configHelper) - { + public function __construct( + Config $configHelper, + ConfigDataInterfaceFactory $configDataFactory + ) { $this->helper = $configHelper; + $this->configDataFactory = $configDataFactory; } /** diff --git a/Remote/RestClient.php b/Remote/RestClient.php index 9c20b6c..d46ee50 100644 --- a/Remote/RestClient.php +++ b/Remote/RestClient.php @@ -10,7 +10,6 @@ namespace Magenerds\SystemDiff\Remote; use Magenerds\SystemDiff\Api\Data\ConfigDataInterface; -use Magenerds\SystemDiff\Model\ConfigData; class RestClient extends AbstractClient implements ClientInterface { @@ -31,10 +30,10 @@ public function fetch() $responseData = json_decode($response->getBody()); if (isset($responseData->data[0])) { - return new ConfigData($this->buildDataFromJson($responseData->data[0])); + return $this->configDataFactory->create(['data' => $this->buildDataFromJson($responseData->data[0])]); } - throw new \Exception("SOAP response could not be read"); + throw new \Exception("REST response could not be read"); } throw new \Exception("Request to remote was not successfull"); diff --git a/Remote/SoapClient.php b/Remote/SoapClient.php index 6fd38f0..687b6e3 100644 --- a/Remote/SoapClient.php +++ b/Remote/SoapClient.php @@ -36,7 +36,7 @@ public function fetch() $response = $httpClient->magenerdsSystemDiffServiceFetchLocalDataServiceV1Fetch(); if (isset($response->result->data->string)) { - return new ConfigData($this->buildDataFromJson($response->result->data->string)); + return $this->configDataFactory->create(['data' => $this->buildDataFromJson($response->result->data->string)]); } throw new \Exception("SOAP response could not be read"); diff --git a/Service/FetchLocalDataService.php b/Service/FetchLocalDataService.php index 44117f2..e30247e 100644 --- a/Service/FetchLocalDataService.php +++ b/Service/FetchLocalDataService.php @@ -52,9 +52,6 @@ public function fetch() $data[$dataReaderCode] = $dataReader->read(); } - /** @var $configData ConfigDataInterface */ - $configData = $this->configDataFactory->create($data); - - return $configData; + return $this->configDataFactory->create(['data' => $data]); } } \ No newline at end of file From 3354bc97fe24d718a12d1d132b9de96ec70e43b1 Mon Sep 17 00:00:00 2001 From: Florian Sydekum Date: Mon, 2 Oct 2017 12:00:03 +0200 Subject: [PATCH 52/62] [MAGEHACKMUC-17] Fixed css for backend --- view/adminhtml/web/css/style.css | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/view/adminhtml/web/css/style.css b/view/adminhtml/web/css/style.css index 93e0a41..69f35fa 100644 --- a/view/adminhtml/web/css/style.css +++ b/view/adminhtml/web/css/style.css @@ -5,16 +5,16 @@ * that is available through the world-wide-web at this URL: * http://opensource.org/licenses/osl-3.0.php */ -.accordion .config td.label { +.accordion .config td.label, .accordion .config colgroup.label { width: 20%; } -.accordion .config td.value { +.accordion .config td.value, .accordion .config colgroup.value { width: 35%; vertical-align: unset; } -.accordion .config td.sync { +.accordion .config td.sync, .accordion .config colgroup.sync { width: 10%; } From 593a8509a8c5d3c14f404233f018d54519e4c137 Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Mon, 2 Oct 2017 12:55:03 +0200 Subject: [PATCH 53/62] [MAGEHACKMUC-17] Added Requirements and more detailed configuration to README --- README.md | 46 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a1eec6f..4cdf6b2 100644 --- a/README.md +++ b/README.md @@ -4,19 +4,53 @@ This extension is the successor of [TechDivision_SystemConfigDiff](https://githu for Magento 2. Data from one Magento instance can be compared with another instance. This is useful if you have a test and a live system and you have to compare its data, i.e. system configuration. It is designed to be extended via `di.xml` in order to integrate more differs and readers. + +## Requirements + +Magento version >= 2.2 ## Configuration -For general usage the functionality should be enabled in the system configuration. In order to connect two systems -you need to configure the web service API. You can either choose REST or SOAP. Enter the url of the remote system -and provide an access token. + +The extension must be installed on both the local and remote instance. + +In order to connect two systems you need to configure the web service API. + +An integration (```System > Integrations```) must exist on the *remote* system +with the API resource + +```Stores > Settings > Configuration > System Diff Section``` + +The Access Token of this integration must be used on the *local* instance in + +```Stores > Configuration > Magenerds > SystemDiff > Connection > Access Token``` + +You can choose REST or SOAP as API to use in + +```Stores > Configuration > Magenerds > SystemDiff > Connection > API Type``` + +Enter the url of the remote system in + +```Stores > Configuration > Magenerds > SystemDiff > Connection > Remote System URL``` + +The module must be enabled in the system configuration to compare a remote configuration: + +```Stores > Configuration > Magenerds > SystemDiff > General > Enabled``` + +To actually see the field differences between the instances in the system configuration, the display must be enabled: + +```Stores > Configuration > Magenerds > SystemDiff > Display > Store configuration diff``` + ## Backend Usage -In the system configuration of the module you can find a `Run diff` button which triggers the sync to the remote system +In the system configuration of the module + +```Stores > Configuration > Magenerds > SystemDiff > Connection``` + +is a `Run` button which triggers the sync between local and remote system and starts the diff. ## Command Line Usage -There is a command available which currently outputs the differences of the data sets of both systems. You can use it -like the following: +The diff can be initiated via CLI command: `bin/magento system-diff:execute` From 29bc0667ab00951415f007eaa74c5a56717a9848 Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Mon, 2 Oct 2017 13:17:57 +0200 Subject: [PATCH 54/62] CLI command prints basic info about progress. --- Console/Command/ExecuteCommand.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Console/Command/ExecuteCommand.php b/Console/Command/ExecuteCommand.php index 7409bd2..62cffaf 100644 --- a/Console/Command/ExecuteCommand.php +++ b/Console/Command/ExecuteCommand.php @@ -26,6 +26,11 @@ class ExecuteCommand extends Command */ const COMMAND_DESCRIPTION = 'system-diff:execute'; + /** + * Exit code when exception occurred + */ + const EXIT_CODE_EXCEPTION = 4; + /** * @var PerformSystemDiffService */ @@ -56,14 +61,20 @@ public function configure() /** * @param InputInterface $input An InputInterface instance * @param OutputInterface $output An OutputInterface instance - * @return null|int null or 0 if everything went fine, or an error code + * @return int 0 if everything went fine, or an error code */ public function execute(InputInterface $input, OutputInterface $output) { + $exitStatus = 0; try { + $output->write('Performing sync and diff...'); $this->performSystemDiffService->performDiff(); + $output->writeln(' Done.'); } catch (\Exception $e) { + $exitStatus = self::EXIT_CODE_EXCEPTION; $output->writeln(sprintf('An error occurred during diff: %s', $e->getMessage())); } + + return $exitStatus; } } \ No newline at end of file From bf3e0f73ab9f9a7ff025d6068b75d3aa5c70fc56 Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Mon, 2 Oct 2017 14:56:07 +0200 Subject: [PATCH 55/62] Run action in backend more informational --- Controller/Adminhtml/SystemDiff/Diff.php | 24 ++++++++++-- .../templates/system/config/sync_button.phtml | 39 +++++++++++++++---- view/adminhtml/web/css/style.css | 6 +++ 3 files changed, 58 insertions(+), 11 deletions(-) diff --git a/Controller/Adminhtml/SystemDiff/Diff.php b/Controller/Adminhtml/SystemDiff/Diff.php index 2918ba2..bd8d74c 100644 --- a/Controller/Adminhtml/SystemDiff/Diff.php +++ b/Controller/Adminhtml/SystemDiff/Diff.php @@ -13,6 +13,7 @@ use Magento\Backend\App\Action; use Magento\Backend\App\Action\Context; use Magento\Framework\Controller\Result\JsonFactory; +use Magento\Framework\Stdlib\DateTime\TimezoneInterface; class Diff extends Action { @@ -31,21 +32,29 @@ class Diff extends Action */ private $performSystemDiffService; + /** + * @var TimezoneInterface + */ + private $timezone; + /** * Diff action constructor. * * @param Context $context * @param JsonFactory $jsonFactory * @param PerformSystemDiffService $performSystemDiffService + * @param TimezoneInterface $timezone */ public function __construct( Context $context, JsonFactory $jsonFactory, - PerformSystemDiffService $performSystemDiffService + PerformSystemDiffService $performSystemDiffService, + TimezoneInterface $timezone ) { $this->context = $context; $this->jsonFactory = $jsonFactory; $this->performSystemDiffService = $performSystemDiffService; + $this->timezone = $timezone; parent::__construct($context); } @@ -60,15 +69,22 @@ public function execute() $result = $this->jsonFactory->create(); - $message = 'OK'; + $message = 'Diff successfully done at %s.'; try { $this->performSystemDiffService->performDiff(); } catch (\Exception $e) { - $message = "Error performing system diff."; + $message = __("Error performing system diff at %s."); + $result->setStatusHeader(500); } - $result->setData(['message' => $message]); + $result->setData( + [ + 'message' => sprintf( + __($message), + $this->timezone->formatDateTime(null, \IntlDateFormatter::SHORT, true)) + ] + ); return $result; } diff --git a/view/adminhtml/templates/system/config/sync_button.phtml b/view/adminhtml/templates/system/config/sync_button.phtml index 72e8a42..096b839 100644 --- a/view/adminhtml/templates/system/config/sync_button.phtml +++ b/view/adminhtml/templates/system/config/sync_button.phtml @@ -13,26 +13,50 @@ require( ['jquery', 'prototype'], function(jQuery){ + var buttonId = 'getElement()->getHtmlId() ?>'; + var buttonSelector = '#'+buttonId; + var infoSelector = buttonSelector+'_info'; + + var $info = jQuery(infoSelector); + var $button = jQuery(buttonSelector); + function enableSyncButton() { - Form.Element.enable('getElement()->getHtmlId() ?>'); - jQuery('#getElement()->getHtmlId() ?>').removeClass('disabled'); + Form.Element.enable(buttonId); + $button.removeClass('disabled'); } function disableSyncButton() { - Form.Element.disable('getElement()->getHtmlId() ?>'); - jQuery('#getElement()->getHtmlId() ?>').addClass('disabled'); + Form.Element.disable(buttonId); + $button.addClass('disabled'); + } + + function message(message, type) { + $info.html(message); + $info.addClass('message'); + + if (type === "error") { + $info.addClass('message-error'); + } else { + $info.removeClass('message-error'); + } } Event.observe(window, 'load', function(){ enableSyncButton(); }); - jQuery('#getElement()->getHtmlId() ?>').click( + jQuery(buttonSelector).click( function () { disableSyncButton(); + message(''); jQuery.get( 'getAjaxUrl() ?>' - ).always(function(){ + ).done(function (data) { + message(data.message); + }).fail(function (jqXHR, textStatus, errorThrown) { + message(errorThrown, "error"); + + }).always(function(){ enableSyncButton(); }) ; @@ -42,4 +66,5 @@ require( ); -getButtonHtml() ?> \ No newline at end of file +getButtonHtml() ?> + diff --git a/view/adminhtml/web/css/style.css b/view/adminhtml/web/css/style.css index 69f35fa..6f832cf 100644 --- a/view/adminhtml/web/css/style.css +++ b/view/adminhtml/web/css/style.css @@ -26,4 +26,10 @@ .diff-hidden { display: none; +} + +.message.magenerds_sync_info { + display: block; + float: none; + margin: .5em 0; } \ No newline at end of file From ab6bb129b91779a70cf25dba97d31d36e3bd7a9b Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Mon, 2 Oct 2017 14:56:32 +0200 Subject: [PATCH 56/62] Link in comment with target blank --- etc/adminhtml/system.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index b051c9a..a5c22a3 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -40,7 +40,7 @@ REST: http[s]://[remotehost]/index.php/rest/all/V1/systemConfig/all
SOAP: http[s]://[remotehost]/soap?wsdl&services=magenerdsSystemDiffServiceFetchLocalDataServiceV1

- See http://devdocs.magento.com/guides/v2.2/get-started/soap/soap-web-api-calls.html + See http://devdocs.magento.com/guides/v2.2/get-started/soap/soap-web-api-calls.html ]]> From 3c18e01f4ed23ed04bafc237c1b28039facfeee5 Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Thu, 5 Oct 2017 00:19:24 +0200 Subject: [PATCH 57/62] DiffConfig model has getter and setter --- Model/DiffConfig.php | 32 ++++++++++++++++++++++++++++++ Model/ResourceModel/DiffConfig.php | 15 ++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/Model/DiffConfig.php b/Model/DiffConfig.php index 2b80f6d..d746035 100644 --- a/Model/DiffConfig.php +++ b/Model/DiffConfig.php @@ -28,4 +28,36 @@ protected function _construct() { $this->_init(DiffConfigResource::class); } + + /** + * @param string $value + */ + public function setDiffValueLocal($value) + { + $this->setData(DiffConfigResource::LOCAL_VALUE_FIELD_NAME, $value); + } + + /** + * @return string + */ + public function getDiffValueLocal() + { + return (string)$this->getData(DiffConfigResource::LOCAL_VALUE_FIELD_NAME); + } + + /** + * @param string $path + */ + public function setPath(string $path) + { + $this->setData(DiffConfigResource::PATH_FIELD_NAME, $path); + } + + /** + * @param string $scope + */ + public function setScope(string $scope) + { + $this->setData(DiffConfigResource::SCOPE_FIELD_NAME, $scope); + } } \ No newline at end of file diff --git a/Model/ResourceModel/DiffConfig.php b/Model/ResourceModel/DiffConfig.php index 290923c..35c1d76 100644 --- a/Model/ResourceModel/DiffConfig.php +++ b/Model/ResourceModel/DiffConfig.php @@ -17,6 +17,21 @@ */ class DiffConfig extends AbstractDb { + /** + * Holds database field names + */ + const LOCAL_VALUE_FIELD_NAME = 'diff_value_local'; + const REMOTE_VALUE_FIELD_NAME = 'diff_value_remote'; + const SCOPE_FIELD_NAME = 'scope'; + const SCOPE_ID_FIELD_NAME = 'scope_id'; + const PATH_FIELD_NAME = 'path'; + + /** + * Values for scope + */ + const SCOPE_VALUE_WEBSITES = 'websites'; + const SCOPE_VALUE_STORES = 'stores'; + /** * Resource initialization * From 13258f68acd454b47bf3be7e549b124d0a16ee41 Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Thu, 5 Oct 2017 00:20:59 +0200 Subject: [PATCH 58/62] StoreConfigDataWriter uses DiffConfig setter and resource model constants --- DataWriter/StoreConfigDataWriter.php | 32 +++++++++------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/DataWriter/StoreConfigDataWriter.php b/DataWriter/StoreConfigDataWriter.php index 7ae8a04..2843a58 100644 --- a/DataWriter/StoreConfigDataWriter.php +++ b/DataWriter/StoreConfigDataWriter.php @@ -16,17 +16,6 @@ class StoreConfigDataWriter implements DataWriterInterface { - /** - * Holds database field names - */ - const LOCAL_VALUE_FIELD_NAME = 'diff_value_local'; - const REMOTE_VALUE_FIELD_NAME = 'diff_value_remote'; - const SCOPE_FIELD_NAME = 'scope'; - const SCOPE_ID_FIELD_NAME = 'scope_id'; - const PATH_FIELD_NAME = 'path'; - const SCOPE_VALUE_WEBSITES = 'websites'; - const SCOPE_VALUE_STORES = 'stores'; - /** * Holds further necessary consts */ @@ -101,8 +90,8 @@ public function write(array $diffData) foreach ($localValues as $localPath => $localValue) { if (array_key_exists($localPath, $remoteValues)) { $combinedValues[$localPath] = [ - self::LOCAL_VALUE_FIELD_NAME => $localValue, - self::REMOTE_VALUE_FIELD_NAME => $remoteValues[$localPath] + DiffConfigResource::LOCAL_VALUE_FIELD_NAME => $localValue, + DiffConfigResource::REMOTE_VALUE_FIELD_NAME => $remoteValues[$localPath] ]; unset($localValues[$localPath]); @@ -110,8 +99,8 @@ public function write(array $diffData) } } - $localModels = $this->mapDataToModels($localValues, $scope, self::LOCAL_VALUE_FIELD_NAME); - $remoteModels = $this->mapDataToModels($remoteValues, $scope, self::REMOTE_VALUE_FIELD_NAME); + $localModels = $this->mapDataToModels($localValues, $scope, DiffConfigResource::LOCAL_VALUE_FIELD_NAME); + $remoteModels = $this->mapDataToModels($remoteValues, $scope, DiffConfigResource::REMOTE_VALUE_FIELD_NAME); $combinedModels = $this->mapDataToModels($combinedValues, $scope); $scopeModels = array_merge($localModels, $remoteModels, $combinedModels); @@ -144,11 +133,11 @@ protected function mapDataToModels(array $data, $scope, $valueField = null) foreach ($data as $path => $value) { /** @var DiffConfigModel $diffConfigModel */ $diffConfigModel = $this->diffConfigFactory->create(); - $diffConfigModel->setData(self::SCOPE_FIELD_NAME, $scope); + $diffConfigModel->setScope($scope); $scopeId = self::DEFAULT_SCOPE_ID; - if ($scope === self::SCOPE_VALUE_WEBSITES || $scope === self::SCOPE_VALUE_STORES) { + if ($scope === DiffConfigResource::SCOPE_VALUE_WEBSITES || $scope === DiffConfigResource::SCOPE_VALUE_STORES) { if (empty($path)) { continue; } @@ -162,17 +151,17 @@ protected function mapDataToModels(array $data, $scope, $valueField = null) $code = $splittedPath[0]; $path = $splittedPath[1]; - if ($scope === self::SCOPE_VALUE_WEBSITES) { + if ($scope === DiffConfigResource::SCOPE_VALUE_WEBSITES) { $scopeId = $this->getWebsiteId($code); } - if ($scope === self::SCOPE_VALUE_STORES) { + if ($scope === DiffConfigResource::SCOPE_VALUE_STORES) { $scopeId = $this->getStoreId($code); } } - $diffConfigModel->setData(self::SCOPE_ID_FIELD_NAME, $scopeId); - $diffConfigModel->setData(self::PATH_FIELD_NAME, $path); + $diffConfigModel->setScope($scopeId); + $diffConfigModel->setPath($path); if (is_array($value) && is_null($valueField)) { foreach ($value as $fieldName => $v) { @@ -182,7 +171,6 @@ protected function mapDataToModels(array $data, $scope, $valueField = null) $diffConfigModel->setData($valueField, $value); } - $models[] = $diffConfigModel; } From 03cf2388ce5a94db9f92e59e16286c0bb0779460 Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Thu, 5 Oct 2017 00:22:30 +0200 Subject: [PATCH 59/62] SaveDiffToTableSerice sets a last sync timestamp via helper --- Helper/Config.php | 113 ++++++++++++++++++++++++++++- Service/SaveDiffToTableService.php | 22 +++++- 2 files changed, 132 insertions(+), 3 deletions(-) diff --git a/Helper/Config.php b/Helper/Config.php index c5b8141..53f208d 100644 --- a/Helper/Config.php +++ b/Helper/Config.php @@ -9,7 +9,14 @@ namespace Magenerds\SystemDiff\Helper; +use DateTimeInterface; +use Magenerds\SystemDiff\Model\DiffConfig; +use Magenerds\SystemDiff\Model\ResourceModel\DiffConfig as DiffConfigResource; use Magento\Framework\App\Config\ScopeConfigInterface; +use Magenerds\SystemDiff\Model\ResourceModel\DiffConfig\CollectionFactory; +use Magenerds\SystemDiff\Model\DiffConfigFactory; +use Magento\Framework\Stdlib\DateTime\TimezoneInterface; +use Magento\Framework\Stdlib\DateTime\DateTime; /** * Class to read module-specific configuration @@ -27,20 +34,54 @@ class Config const XML_PATH_ACCESS_TOKEN = 'system_diff/connection/access_token'; const XML_PATH_API_TYPE = 'system_diff/connection/api_type'; const XML_PATH_DISPLAY_STORE_CONFIG = 'system_diff/display/store_config'; + const XML_PATH_LAST_SYNC_DATETIME = 'system_diff/general/last_sync_datetime'; /** * @var ScopeConfigInterface */ private $scopeConfig; + /** + * @var CollectionFactory + */ + private $diffConfigCollectionFactory; + + /** + * @var DiffConfigFactory + */ + private $diffConfigFactory; + + /** + * @var DateTime + */ + private $dateTime; + + /** + * @var TimezoneInterface + */ + private $timezone; + /** * Config constructor. + * * @param ScopeConfigInterface $scopeConfig + * @param CollectionFactory $collectionFactory + * @param DiffConfigFactory $diffConfigFactory + * @param TimezoneInterface $timezone + * @param DateTime $dateTime */ public function __construct( - ScopeConfigInterface $scopeConfig + ScopeConfigInterface $scopeConfig, + CollectionFactory $collectionFactory, + DiffConfigFactory $diffConfigFactory, + TimezoneInterface $timezone, + DateTime $dateTime ) { $this->scopeConfig = $scopeConfig; + $this->diffConfigCollectionFactory = $collectionFactory; + $this->diffConfigFactory = $diffConfigFactory; + $this->dateTime = $dateTime; + $this->timezone = $timezone; } /** @@ -84,4 +125,74 @@ public function isDisplayStoreConfig() { return $this->scopeConfig->isSetFlag(self::XML_PATH_DISPLAY_STORE_CONFIG); } + + /** + * Return a string with date time of last sync or n/a. + * + * @return string + */ + public function getLastSyncDatetimeFormatted() + { + $dateTime = $this->getLastSyncDatetime(); + if ($dateTime instanceof DateTimeInterface) { + + return $this->timezone->formatDateTime($dateTime, \IntlDateFormatter::SHORT, true); + } + + return (string)__('n/a'); + } + + /** + * The last saved sync datetime timestamp as string. + * + * Should be a gmt timestamp. + * + * @return DateTimeInterface|null; + */ + public function getLastSyncDatetime() + { + $dataObject = $this->getLastSyncDiffConfig(); + $dateTime = null; + if ($dataObject instanceof DiffConfig) { + $timeStampString = $dataObject->getDiffValueLocal(); + $dateTime = \DateTime::createFromFormat("U", $timeStampString); + } + + return $dateTime; + } + + /** + * DiffConfig entry that holds the last sync time. + * + * @return DiffConfig|null; + */ + protected function getLastSyncDiffConfig() + { + $collection = $this->diffConfigCollectionFactory->create(); + + /** @var DiffConfig $dataObject */ + $dataObject = $collection->getItemByColumnValue( + DiffConfigResource::PATH_FIELD_NAME, + self::XML_PATH_LAST_SYNC_DATETIME + ); + + return $dataObject; + } + + /** + * Create or update last sync entry. + * + * @return void + */ + public function updateLastDiffTimestamp() + { + $diffConfig = $this->getLastSyncDiffConfig(); + if (!($diffConfig instanceof DiffConfig)) { + $diffConfig = $this->diffConfigFactory->create(); + $diffConfig->setScope('default'); + $diffConfig->setPath(Config::XML_PATH_LAST_SYNC_DATETIME); + } + $diffConfig->setDiffValueLocal($this->dateTime->gmtTimestamp()); + $diffConfig->save(); + } } \ No newline at end of file diff --git a/Service/SaveDiffToTableService.php b/Service/SaveDiffToTableService.php index 8b22a0a..1648f18 100644 --- a/Service/SaveDiffToTableService.php +++ b/Service/SaveDiffToTableService.php @@ -12,6 +12,7 @@ use Magenerds\SystemDiff\Api\Service\SaveDiffToTableServiceInterface; use Magenerds\SystemDiff\DataWriter\DataWriterInterface; use Magenerds\SystemDiff\DataWriter\DataWriterPool; +use Magenerds\SystemDiff\Helper\Config; /** * Class SaveDiffToTableService @@ -24,17 +25,32 @@ class SaveDiffToTableService implements SaveDiffToTableServiceInterface */ private $writerPool; + /** + * @var Config + */ + private $configHelper; + /** * StoreConfigDataWriter constructor. + * * @param DataWriterPool $writerPool + * @param Config $configHelper + * + * @internal param DiffConfigFactory $diffConfigFactory + * @internal param DateTime $dateTime + * @internal param StoreConfigUpdater $configUpdater */ - public function __construct(DataWriterPool $writerPool) - { + public function __construct( + DataWriterPool $writerPool, + Config $configHelper + ) { $this->writerPool = $writerPool; + $this->configHelper = $configHelper; } /** * @param [] $diffData + * * @return void */ public function saveData(array $diffData) @@ -43,5 +59,7 @@ public function saveData(array $diffData) /** @var DataWriterInterface $writer */ $writer->write($diffData); } + + $this->configHelper->updateLastDiffTimestamp(); } } \ No newline at end of file From 238ee5f4e6652627a63b38f92a94ceeef27e9133 Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Thu, 5 Oct 2017 00:23:46 +0200 Subject: [PATCH 60/62] Display of last sync timestamp in CLI and backend. --- Block/System/Config/Form/SyncButton.php | 50 ++++++++++++++++++- Console/Command/ExecuteCommand.php | 24 +++++++-- Controller/Adminhtml/SystemDiff/Diff.php | 19 +++---- .../templates/system/config/sync_button.phtml | 4 +- 4 files changed, 80 insertions(+), 17 deletions(-) diff --git a/Block/System/Config/Form/SyncButton.php b/Block/System/Config/Form/SyncButton.php index 644a669..3598409 100644 --- a/Block/System/Config/Form/SyncButton.php +++ b/Block/System/Config/Form/SyncButton.php @@ -9,6 +9,7 @@ namespace Magenerds\SystemDiff\Block\System\Config\Form; +use Magenerds\SystemDiff\Helper\Config; use Magento\Config\Block\System\Config\Form\Field; use Magento\Framework\Data\Form\Element\AbstractElement; @@ -24,15 +25,41 @@ class SyncButton extends Field */ protected $element; + /** + * @var Config + */ + public $configHelper; + + /** + * SyncButton constructor. + * + * Additionally injects config helper. + * + * @param \Magento\Backend\Block\Template\Context $context + * @param array $data + * @param Config $configHelper + */ + public function __construct( + \Magento\Backend\Block\Template\Context $context, + Config $configHelper, + array $data = [] + ) { + $this->configHelper = $configHelper; + + parent::__construct($context, $data); + } + /** * Remove scope label * * @param AbstractElement $element + * * @return string */ public function render(AbstractElement $element) { $element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue(); + return parent::render($element); } @@ -40,6 +67,7 @@ public function render(AbstractElement $element) * Return element html * * @param AbstractElement $element + * * @return string */ protected function _getElementHtml(AbstractElement $element) @@ -73,7 +101,7 @@ public function getButtonHtml() ); $button->setData( [ - 'id' => $this->element->getHtmlId(), + 'id' => $this->element->getHtmlId(), 'label' => __('Run'), 'class' => 'disabled' // reset when page loaded, see template script ] @@ -82,6 +110,24 @@ public function getButtonHtml() return $button->toHtml(); } + /** + * Returns info about last sync date time as a string for usage in "'"-JS string. + * + * @return string + */ + public function getLastSyncInfo() + { + $formattedDateTime = $this->configHelper->getLastSyncDatetimeFormatted(); + + return (string)filter_var( + sprintf( + __('Last diff on %s'), + $formattedDateTime + ), + FILTER_SANITIZE_MAGIC_QUOTES + ); + } + /** * Returns the element defined in system.xml * @@ -91,4 +137,4 @@ public function getElement() { return $this->element; } -} \ No newline at end of file +} diff --git a/Console/Command/ExecuteCommand.php b/Console/Command/ExecuteCommand.php index 62cffaf..6e76734 100644 --- a/Console/Command/ExecuteCommand.php +++ b/Console/Command/ExecuteCommand.php @@ -9,6 +9,7 @@ namespace Magenerds\SystemDiff\Console\Command; +use Magenerds\SystemDiff\Helper\Config; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -36,15 +37,24 @@ class ExecuteCommand extends Command */ private $performSystemDiffService; + /** + * @var Config + */ + private $configHelper; + /** * ExecuteCommand constructor. + * * @param PerformSystemDiffService $performSystemDiffService + * @param Config $configHelper */ public function __construct( - PerformSystemDiffService $performSystemDiffService - ){ + PerformSystemDiffService $performSystemDiffService, + Config $configHelper + ) { parent::__construct(self::COMMAND_NAME); $this->performSystemDiffService = $performSystemDiffService; + $this->configHelper = $configHelper; } /** @@ -59,8 +69,9 @@ public function configure() } /** - * @param InputInterface $input An InputInterface instance + * @param InputInterface $input An InputInterface instance * @param OutputInterface $output An OutputInterface instance + * * @return int 0 if everything went fine, or an error code */ public function execute(InputInterface $input, OutputInterface $output) @@ -69,7 +80,12 @@ public function execute(InputInterface $input, OutputInterface $output) try { $output->write('Performing sync and diff...'); $this->performSystemDiffService->performDiff(); - $output->writeln(' Done.'); + $output->writeln( + sprintf( + 'Done at %s.', + $this->configHelper->getLastSyncDatetimeFormatted() + ) + ); } catch (\Exception $e) { $exitStatus = self::EXIT_CODE_EXCEPTION; $output->writeln(sprintf('An error occurred during diff: %s', $e->getMessage())); diff --git a/Controller/Adminhtml/SystemDiff/Diff.php b/Controller/Adminhtml/SystemDiff/Diff.php index bd8d74c..971183f 100644 --- a/Controller/Adminhtml/SystemDiff/Diff.php +++ b/Controller/Adminhtml/SystemDiff/Diff.php @@ -9,11 +9,11 @@ namespace Magenerds\SystemDiff\Controller\Adminhtml\SystemDiff; +use Magenerds\SystemDiff\Helper\Config; use Magenerds\SystemDiff\Service\PerformSystemDiffService; use Magento\Backend\App\Action; use Magento\Backend\App\Action\Context; use Magento\Framework\Controller\Result\JsonFactory; -use Magento\Framework\Stdlib\DateTime\TimezoneInterface; class Diff extends Action { @@ -33,28 +33,28 @@ class Diff extends Action private $performSystemDiffService; /** - * @var TimezoneInterface + * @var Config */ - private $timezone; + private $configHelper; /** * Diff action constructor. * - * @param Context $context - * @param JsonFactory $jsonFactory + * @param Context $context + * @param JsonFactory $jsonFactory * @param PerformSystemDiffService $performSystemDiffService - * @param TimezoneInterface $timezone + * @param Config $configHelper */ public function __construct( Context $context, JsonFactory $jsonFactory, PerformSystemDiffService $performSystemDiffService, - TimezoneInterface $timezone + Config $configHelper ) { $this->context = $context; $this->jsonFactory = $jsonFactory; $this->performSystemDiffService = $performSystemDiffService; - $this->timezone = $timezone; + $this->configHelper = $configHelper; parent::__construct($context); } @@ -82,7 +82,8 @@ public function execute() [ 'message' => sprintf( __($message), - $this->timezone->formatDateTime(null, \IntlDateFormatter::SHORT, true)) + $this->configHelper->getLastSyncDatetimeFormatted() + ), ] ); diff --git a/view/adminhtml/templates/system/config/sync_button.phtml b/view/adminhtml/templates/system/config/sync_button.phtml index 096b839..2b3ba1a 100644 --- a/view/adminhtml/templates/system/config/sync_button.phtml +++ b/view/adminhtml/templates/system/config/sync_button.phtml @@ -41,7 +41,7 @@ require( } } - Event.observe(window, 'load', function(){ + jQuery(document).ready(function(){ enableSyncButton(); }); @@ -67,4 +67,4 @@ require( getButtonHtml() ?> - +getLastSyncInfo(); ?> From d04a01c7ed574f2becfff807e50265aff71c3989 Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Wed, 4 Oct 2017 08:52:39 +0200 Subject: [PATCH 61/62] Log errors in client. --- Remote/AbstractClient.php | 11 ++++++++++- Remote/RestClient.php | 2 ++ Remote/SoapClient.php | 2 ++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Remote/AbstractClient.php b/Remote/AbstractClient.php index 0c2f54c..a6c9f2e 100644 --- a/Remote/AbstractClient.php +++ b/Remote/AbstractClient.php @@ -11,6 +11,7 @@ use Magenerds\SystemDiff\Helper\Config; use Magenerds\SystemDiff\Api\Data\ConfigDataInterfaceFactory; +use Psr\Log\LoggerInterface; class AbstractClient { @@ -24,18 +25,26 @@ class AbstractClient */ protected $configDataFactory; + /** + * @var LoggerInterface + */ + protected $logger; + /** * Client constructor. * * @param Config $configHelper * @param ConfigDataInterfaceFactory $configDataFactory + * @param LoggerInterface $logger */ public function __construct( Config $configHelper, - ConfigDataInterfaceFactory $configDataFactory + ConfigDataInterfaceFactory $configDataFactory, + LoggerInterface $logger ) { $this->helper = $configHelper; $this->configDataFactory = $configDataFactory; + $this->logger = $logger; } /** diff --git a/Remote/RestClient.php b/Remote/RestClient.php index d46ee50..9774f7b 100644 --- a/Remote/RestClient.php +++ b/Remote/RestClient.php @@ -36,6 +36,8 @@ public function fetch() throw new \Exception("REST response could not be read"); } + // From here error state -> exception. + $this->logger->error($response->getBody()); throw new \Exception("Request to remote was not successfull"); } } \ No newline at end of file diff --git a/Remote/SoapClient.php b/Remote/SoapClient.php index 687b6e3..319b5bf 100644 --- a/Remote/SoapClient.php +++ b/Remote/SoapClient.php @@ -39,6 +39,8 @@ public function fetch() return $this->configDataFactory->create(['data' => $this->buildDataFromJson($response->result->data->string)]); } + // error + $this->logger->error($httpClient->getLastResponse()); throw new \Exception("SOAP response could not be read"); } } \ No newline at end of file From 5f50051740be842d991c3acabd23947581666d7d Mon Sep 17 00:00:00 2001 From: Florian Beyerlein Date: Wed, 4 Oct 2017 08:53:49 +0200 Subject: [PATCH 62/62] API type, Remote URL and Access Token overwriteable on CLI --- Console/Command/ExecuteCommand.php | 43 ++++++++++++++++++++++++++++-- Helper/Config.php | 19 +++++++------ 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/Console/Command/ExecuteCommand.php b/Console/Command/ExecuteCommand.php index 6e76734..cd8e633 100644 --- a/Console/Command/ExecuteCommand.php +++ b/Console/Command/ExecuteCommand.php @@ -10,8 +10,10 @@ namespace Magenerds\SystemDiff\Console\Command; use Magenerds\SystemDiff\Helper\Config; +use Magento\Framework\App\Config\MutableScopeConfigInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Magenerds\SystemDiff\Service\PerformSystemDiffService; @@ -32,6 +34,13 @@ class ExecuteCommand extends Command */ const EXIT_CODE_EXCEPTION = 4; + /** + * Command options + */ + const OPTION_REMOTE_SYSTEM_URL = 'remote-system-url'; + const OPTION_API_TYPE = 'api-type'; + const OPTION_ACCESS_TOKEN = 'access-token'; + /** * @var PerformSystemDiffService */ @@ -42,33 +51,52 @@ class ExecuteCommand extends Command */ private $configHelper; + /** + * @var MutableScopeConfigInterface + */ + private $scopeConfig; + /** * ExecuteCommand constructor. * - * @param PerformSystemDiffService $performSystemDiffService - * @param Config $configHelper + * @param PerformSystemDiffService $performSystemDiffService + * @param MutableScopeConfigInterface $scopeConfig + * @param Config $configHelper */ public function __construct( PerformSystemDiffService $performSystemDiffService, + MutableScopeConfigInterface $scopeConfig, Config $configHelper ) { parent::__construct(self::COMMAND_NAME); + $this->performSystemDiffService = $performSystemDiffService; + $this->scopeConfig = $scopeConfig; $this->configHelper = $configHelper; } /** * Configures the current command. + * + * @return void */ public function configure() { $this->setName(self::COMMAND_NAME); $this->setDescription(self::COMMAND_DESCRIPTION); + $this->addOption(self::OPTION_API_TYPE, 'a', InputOption::VALUE_OPTIONAL, 'Overwrite configured api type'); + $this->addOption(self::OPTION_REMOTE_SYSTEM_URL, 'u', InputOption::VALUE_OPTIONAL, + 'Overwrite configured remote url'); + $this->addOption(self::OPTION_ACCESS_TOKEN, 't', InputOption::VALUE_OPTIONAL, + 'Overwrite configured access token'); + parent::configure(); } /** + * Perform system diff via service. + * * @param InputInterface $input An InputInterface instance * @param OutputInterface $output An OutputInterface instance * @@ -77,6 +105,17 @@ public function configure() public function execute(InputInterface $input, OutputInterface $output) { $exitStatus = 0; + + if ($url = $input->getOption(self::OPTION_REMOTE_SYSTEM_URL)) { + $this->scopeConfig->setValue(Config::XML_PATH_REMOTE_SYSTEM_URL, $url); + } + if ($api = $input->getOption(self::OPTION_API_TYPE)) { + $this->scopeConfig->setValue(Config::XML_PATH_API_TYPE, $api); + } + if ($token = $input->getOption(self::OPTION_ACCESS_TOKEN)) { + $this->scopeConfig->setValue(Config::XML_PATH_ACCESS_TOKEN, $token); + } + try { $output->write('Performing sync and diff...'); $this->performSystemDiffService->performDiff(); diff --git a/Helper/Config.php b/Helper/Config.php index 53f208d..6629874 100644 --- a/Helper/Config.php +++ b/Helper/Config.php @@ -9,10 +9,10 @@ namespace Magenerds\SystemDiff\Helper; +use Magento\Framework\App\Config\MutableScopeConfigInterface; use DateTimeInterface; use Magenerds\SystemDiff\Model\DiffConfig; use Magenerds\SystemDiff\Model\ResourceModel\DiffConfig as DiffConfigResource; -use Magento\Framework\App\Config\ScopeConfigInterface; use Magenerds\SystemDiff\Model\ResourceModel\DiffConfig\CollectionFactory; use Magenerds\SystemDiff\Model\DiffConfigFactory; use Magento\Framework\Stdlib\DateTime\TimezoneInterface; @@ -37,7 +37,7 @@ class Config const XML_PATH_LAST_SYNC_DATETIME = 'system_diff/general/last_sync_datetime'; /** - * @var ScopeConfigInterface + * @var MutableScopeConfigInterface */ private $scopeConfig; @@ -64,14 +64,14 @@ class Config /** * Config constructor. * - * @param ScopeConfigInterface $scopeConfig - * @param CollectionFactory $collectionFactory - * @param DiffConfigFactory $diffConfigFactory - * @param TimezoneInterface $timezone - * @param DateTime $dateTime + * @param MutableScopeConfigInterface $scopeConfig + * @param CollectionFactory $collectionFactory + * @param DiffConfigFactory $diffConfigFactory + * @param TimezoneInterface $timezone + * @param DateTime $dateTime */ public function __construct( - ScopeConfigInterface $scopeConfig, + MutableScopeConfigInterface $scopeConfig, CollectionFactory $collectionFactory, DiffConfigFactory $diffConfigFactory, TimezoneInterface $timezone, @@ -80,8 +80,8 @@ public function __construct( $this->scopeConfig = $scopeConfig; $this->diffConfigCollectionFactory = $collectionFactory; $this->diffConfigFactory = $diffConfigFactory; - $this->dateTime = $dateTime; $this->timezone = $timezone; + $this->dateTime = $dateTime; } /** @@ -135,7 +135,6 @@ public function getLastSyncDatetimeFormatted() { $dateTime = $this->getLastSyncDatetime(); if ($dateTime instanceof DateTimeInterface) { - return $this->timezone->formatDateTime($dateTime, \IntlDateFormatter::SHORT, true); }