From 8095b4300602387f3c997bb02f853b61b555187e Mon Sep 17 00:00:00 2001 From: Renon Stewart Date: Fri, 8 May 2020 13:08:05 -0400 Subject: [PATCH] Code refactoring --- .github/workflows/php.yml | 11 ++ .../System/Config/Form/Composer/Version.php | 18 +-- .../Config/Form/Field/OrderIncrement.php | 30 ++-- .../System/Config/Form/Field/Preview.php | 15 +- .../System/Config/Form/Module/Version.php | 88 ----------- Helper/Data.php | 13 -- Model/Config/Backend/AccessCode.php | 12 +- Model/Config/Backend/ModifyTimestamp.php | 7 +- Model/Config/Backend/ValidFor.php | 4 - .../Model/Session/SuccessValidatorPlugin.php | 5 - composer.json | 2 +- etc/adminhtml/system.xml | 6 +- view/adminhtml/requirejs-config.js | 4 +- .../system/config/form/field/preview.phtml | 142 ++---------------- view/adminhtml/web/js/preview.js | 102 +++++++++++++ 15 files changed, 167 insertions(+), 292 deletions(-) create mode 100644 .github/workflows/php.yml delete mode 100644 Block/Adminhtml/System/Config/Form/Module/Version.php create mode 100644 view/adminhtml/web/js/preview.js diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml new file mode 100644 index 0000000..2355901 --- /dev/null +++ b/.github/workflows/php.yml @@ -0,0 +1,11 @@ +name: PHPCS +on: [push] +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@master + - name: PHPCS + run: docker run --rm -v $PWD:/code:ro domw/phpcs phpcs --colors --standard=Magento2 --report=full,summary,gitblame --extensions=php,phtml ./ + - name: compatibility + run: docker run --rm -v $PWD:/code:ro domw/phpcompatibility phpcs --standard=PHPCompatibility --runtime-set testVersion 5.6-7.3 --colors --warning-severity=0 --report=full,summary --extensions=php,phtml ./ \ No newline at end of file diff --git a/Block/Adminhtml/System/Config/Form/Composer/Version.php b/Block/Adminhtml/System/Config/Form/Composer/Version.php index c0a09ba..9d49289 100644 --- a/Block/Adminhtml/System/Config/Form/Composer/Version.php +++ b/Block/Adminhtml/System/Config/Form/Composer/Version.php @@ -18,13 +18,8 @@ use Magento\Framework\Filesystem\Directory\ReadFactory; use Magento\Framework\Phrase; -/** - * Class Version - * @package MagePal\PreviewCheckoutSuccessPage\Block\Adminhtml\System\Config\Form\Composer - */ class Version extends Field { - /** * @var DeploymentConfig */ @@ -65,7 +60,6 @@ public function __construct( * * @param AbstractElement $element * @return string - * @throws LocalizedException */ public function render(AbstractElement $element) { @@ -96,16 +90,6 @@ public function getVersion() return $this->getComposerVersion($this->getModuleName()); } - /** - * @return string - */ - public function getModuleName() - { - $classArray = explode('\\', get_class($this)); - - return count($classArray) > 2 ? "{$classArray[0]}_{$classArray[1]}" : ''; - } - /** * Get module composer version * @@ -128,7 +112,7 @@ public function getComposerVersion($moduleName) return !empty($data->version) ? $data->version : __('Unknown'); } } catch (Exception $e) { - // + return 'Unknown'; } return 'Unknown'; diff --git a/Block/Adminhtml/System/Config/Form/Field/OrderIncrement.php b/Block/Adminhtml/System/Config/Form/Field/OrderIncrement.php index e7d8507..dd953d1 100644 --- a/Block/Adminhtml/System/Config/Form/Field/OrderIncrement.php +++ b/Block/Adminhtml/System/Config/Form/Field/OrderIncrement.php @@ -16,17 +16,19 @@ use Magento\Config\Block\System\Config\Form\Field; use Magento\Framework\Data\Form\Element\AbstractElement; use Magento\Framework\DataObject; +use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\Sales\Model\Order; use Magento\Sales\Model\ResourceModel\Order\CollectionFactory; use Magento\Store\Api\Data\StoreInterface; /** - * Class OrderIncrement - * @package MagePal\PreviewCheckoutSuccessPage\Block\Adminhtml\System\Config\Form\Field + * @method setElement($element) + * @method getElement() + * @method getForm() */ class OrderIncrement extends Field { - /** * @var CollectionFactory */ @@ -50,21 +52,25 @@ public function __construct( /** * Get the grid and scripts contents * - * @param AbstractElement $element + * @param AbstractElement $element * @return string */ protected function _getElementHtml(AbstractElement $element) { $this->setElement($element); - if (!$element->getEscapedValue()) { + try { + if (!$element->getEscapedValue()) { - /** @var Order $order */ - $order = $this->getLastOrder(); + /** @var Order $order */ + $order = $this->getLastOrder(); - if ($order->getId()) { - $element->setValue($order->getIncrementId()); + if ($order->getId()) { + $element->setValue($order->getIncrementId()); + } } + } catch (\Exception $e) { + return parent::_getElementHtml($element); } return parent::_getElementHtml($element); @@ -72,6 +78,9 @@ protected function _getElementHtml(AbstractElement $element) /** * @return StoreInterface|null + * @throws LocalizedException + * @throws NoSuchEntityException + * @return \Magento\Store\Api\Data\StoreInterface|null */ public function getCurrentStore() { @@ -89,6 +98,9 @@ public function getCurrentStore() /** * @return DataObject + * @throws LocalizedException + * @throws NoSuchEntityException + * @return Order */ public function getLastOrder() { diff --git a/Block/Adminhtml/System/Config/Form/Field/Preview.php b/Block/Adminhtml/System/Config/Form/Field/Preview.php index ed1bf71..3c597da 100644 --- a/Block/Adminhtml/System/Config/Form/Field/Preview.php +++ b/Block/Adminhtml/System/Config/Form/Field/Preview.php @@ -13,10 +13,6 @@ use Magento\Framework\Url; use Magento\Store\Api\Data\StoreInterface; -/** - * Class Preview - * @package MagePal\PreviewCheckoutSuccessPage\Block\Adminhtml\System\Config\Form\Field - */ class Preview extends Field { /** @@ -44,7 +40,6 @@ public function __construct( parent::__construct($context, $data); } - /** * @param AbstractElement $element * @return string @@ -115,4 +110,14 @@ public function getSuccessPageUrl() return $this->urlHelper->setScope($store->getId())->getUrl('checkout/onepage/success', $routeParams); } + + /** + * @return string + */ + public function getSpinnerTemplate() + { + $value = '
'; + $value .= '
'; + return $value; + } } diff --git a/Block/Adminhtml/System/Config/Form/Module/Version.php b/Block/Adminhtml/System/Config/Form/Module/Version.php deleted file mode 100644 index 8d71178..0000000 --- a/Block/Adminhtml/System/Config/Form/Module/Version.php +++ /dev/null @@ -1,88 +0,0 @@ -_moduleList = $moduleList; - } - - /** - * Render button - * - * @param AbstractElement $element - * @return string - * @throws LocalizedException - */ - public function render(AbstractElement $element) - { - // Remove scope label - $element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue(); - return parent::render($element); - } - - /** - * Return element html - * - * @param AbstractElement $element - * @return string - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - */ - protected function _getElementHtml(AbstractElement $element) - { - return 'v' . $this->getVersion(); - } - - /** - * Get Module version number - * - * @return string - */ - public function getVersion() - { - $moduleInfo = $this->_moduleList->getOne($this->getModuleName()); - return $moduleInfo['setup_version']; - } - - /** - * @return string - */ - public function getModuleName() - { - $classArray = explode('\\', get_class($this)); - - return count($classArray) > 2 ? "{$classArray[0]}_{$classArray[1]}" : ''; - } -} diff --git a/Helper/Data.php b/Helper/Data.php index bd920d2..3dc4ae2 100755 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -12,23 +12,10 @@ use Magento\Store\Model\ScopeInterface; use MagePal\PreviewCheckoutSuccessPage\Model\Config\Backend\ValidFor; -/** - * Class Data - * @package MagePal\PreviewCheckoutSuccessPage\Helper - */ class Data extends AbstractHelper { const XML_PATH_ACTIVE = 'magepal_checkout/preview_success_page/active'; - /** - * @param Context $context - */ - public function __construct( - Context $context - ) { - parent::__construct($context); - } - /** * Whether is active * diff --git a/Model/Config/Backend/AccessCode.php b/Model/Config/Backend/AccessCode.php index b365e69..b68f876 100644 --- a/Model/Config/Backend/AccessCode.php +++ b/Model/Config/Backend/AccessCode.php @@ -10,15 +10,12 @@ use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\App\Config\Value; use Magento\Framework\Data\Collection\AbstractDb; +use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Math\Random; use Magento\Framework\Model\Context; use Magento\Framework\Model\ResourceModel\AbstractResource; use Magento\Framework\Registry; -/** - * Class AccessCode - * @package MagePal\PreviewCheckoutSuccessPage\Model\Config\Backend - */ class AccessCode extends Value { @@ -34,9 +31,9 @@ class AccessCode extends Value * @param Registry $registry * @param ScopeConfigInterface $config * @param TypeListInterface $cacheTypeList + * @param Random $random * @param AbstractResource|null $resource * @param AbstractDb|null $resourceCollection - * @param Random $random * @param array $data */ public function __construct( @@ -44,11 +41,10 @@ public function __construct( Registry $registry, ScopeConfigInterface $config, TypeListInterface $cacheTypeList, + Random $random, AbstractResource $resource = null, AbstractDb $resourceCollection = null, - Random $random, array $data = [] - ) { $this->random = $random; parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); @@ -56,7 +52,7 @@ public function __construct( /** * @return $this - * @throws \Magento\Framework\Exception\LocalizedException + * @throws LocalizedException */ public function beforeSave() { diff --git a/Model/Config/Backend/ModifyTimestamp.php b/Model/Config/Backend/ModifyTimestamp.php index 26eada3..f4b69bb 100644 --- a/Model/Config/Backend/ModifyTimestamp.php +++ b/Model/Config/Backend/ModifyTimestamp.php @@ -15,10 +15,6 @@ use Magento\Framework\Registry; use Magento\Framework\Stdlib\DateTime\TimezoneInterface; -/** - * Class ModifyTimestamp - * @package MagePal\PreviewCheckoutSuccessPage\Model\Config\Backend - */ class ModifyTimestamp extends Value { @@ -44,11 +40,10 @@ public function __construct( Registry $registry, ScopeConfigInterface $config, TypeListInterface $cacheTypeList, + TimezoneInterface $localeDate, AbstractResource $resource = null, AbstractDb $resourceCollection = null, - TimezoneInterface $localeDate, array $data = [] - ) { $this->localeDate = $localeDate; parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); diff --git a/Model/Config/Backend/ValidFor.php b/Model/Config/Backend/ValidFor.php index 23cd365..35e8a6d 100644 --- a/Model/Config/Backend/ValidFor.php +++ b/Model/Config/Backend/ValidFor.php @@ -8,10 +8,6 @@ use Magento\Framework\App\Config\Value; -/** - * Class ValidFor - * @package MagePal\PreviewCheckoutSuccessPage\Model\Config\Backend - */ class ValidFor extends Value { const MAX_ACCESS_TIME = 15; diff --git a/Plugin/Model/Session/SuccessValidatorPlugin.php b/Plugin/Model/Session/SuccessValidatorPlugin.php index 19742d5..0b24087 100644 --- a/Plugin/Model/Session/SuccessValidatorPlugin.php +++ b/Plugin/Model/Session/SuccessValidatorPlugin.php @@ -16,13 +16,8 @@ use Magento\Sales\Model\ResourceModel\Order\CollectionFactory; use MagePal\PreviewCheckoutSuccessPage\Helper\Data; -/** - * Class SuccessValidatorPlugin - * @package MagePal\PreviewCheckoutSuccessPage\Plugin\Model\Session - */ class SuccessValidatorPlugin { - /** @var Data */ protected $dataHelper; diff --git a/composer.json b/composer.json index 1485f29..26cc9fe 100644 --- a/composer.json +++ b/composer.json @@ -40,7 +40,7 @@ "magepal/magento2-checkout-success-misc-script":"Add miscellaneous HTML and scripts to your checkout success page" }, "type": "magento2-module", - "version": "1.0.9", + "version": "1.1.0", "autoload": { "files": [ "registration.php" diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 22a90ed..334e6a0 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -20,7 +20,7 @@ - Copyright © 2019 MagePal, LLC + Copyright © 2020 MagePal, LLC Documentation Support Latest Version @@ -53,10 +53,6 @@ } ]]> - - - MagePal\PreviewCheckoutSuccessPage\Block\Adminhtml\System\Config\Form\Module\Version - MagePal\PreviewCheckoutSuccessPage\Block\Adminhtml\System\Config\Form\Composer\Version diff --git a/view/adminhtml/requirejs-config.js b/view/adminhtml/requirejs-config.js index 730aa73..7e8bb0d 100644 --- a/view/adminhtml/requirejs-config.js +++ b/view/adminhtml/requirejs-config.js @@ -1,7 +1,7 @@ var config = { map: { '*': { - checkoutSuccessMiscScript: 'MagePal_CheckoutSuccessMiscScript/js/checkout-success-misc-script' + previewCheckoutSuccessPage: 'MagePal_PreviewCheckoutSuccessPage/js/preview' } } -}; \ No newline at end of file +}; diff --git a/view/adminhtml/templates/system/config/form/field/preview.phtml b/view/adminhtml/templates/system/config/form/field/preview.phtml index 7a8865b..a3ae411 100644 --- a/view/adminhtml/templates/system/config/form/field/preview.phtml +++ b/view/adminhtml/templates/system/config/form/field/preview.phtml @@ -11,139 +11,23 @@ ?> -
- - - + + +
-
+
- \ No newline at end of file + } + } + diff --git a/view/adminhtml/web/js/preview.js b/view/adminhtml/web/js/preview.js new file mode 100644 index 0000000..97d8eb0 --- /dev/null +++ b/view/adminhtml/web/js/preview.js @@ -0,0 +1,102 @@ +define([ + 'jquery', + 'Magento_Ui/js/modal/alert' +], function ($, alert) { + + var saveConfig = function (urlTemplate, processCallback) { + if (configForm.validation('isValid')) { + $.ajax({ + method: 'POST', + url: configForm.attr('action'), + data: configForm.serialize(), + context: this + }).done(function (response) { + if (typeof response === 'object') { + if (response.error) { + alert({ title: 'Error', content: response.message }); + } else if (response.ajaxExpired) { + window.location.href = response.ajaxRedirect; + } + } else { + var url = null; + var $result = $(response); + var $accessCode = $result.find('#magepal_checkout_preview_success_page_access_code'); + var $orderIncrement = $result.find('#magepal_checkout_preview_success_page_order_increment'); + + if ($accessCode.size() && $accessCode.val() + && $orderIncrement.size() && $orderIncrement.val() + ) { + url = urlTemplate.replace("--previewAccessCode--", $accessCode.val()); + } + + processCallback(url); + } + }); + + return true; + } else { + return false; + } + }; + + return function (config) { + var $iframe = $('[data-role="iframe-preview-panel"] iframe'); + var $iframePreviewPanel = $('[data-role="iframe-preview-panel"]'); + var urlTemplate = config.urlTemplate; + + $('#inline-preview').on('click', function () { + $iframe.hide(); + $iframePreviewPanel.css({"min-height": "500px"}).trigger('processStart'); + + $iframe.on('load', function () { + $iframePreviewPanel.trigger('processStop'); + $(this).show() + }); + + saveConfig(urlTemplate, function (url) { + if (url) { + $iframe.attr('src', url); + } + + }); + }); + + $('#view-source').on('click', function () { + $('body').trigger('processStart'); + $iframePreviewPanel.css({"min-height": "0px"}); + $iframe.hide(); + + saveConfig(urlTemplate, function (url) { + if (url) { + $.ajax({ + method: 'GET', + url: url, + context: this + }).done(function (response) { + + alert({ + title: 'HTML Source', + content: '' + }); + + $('body').trigger('processStop'); + }); + } + }); + }); + + $('#new-window').on('click', function () { + $('body').trigger('processStart'); + $iframePreviewPanel.css({"min-height": "0px"}); + $iframe.hide(); + + saveConfig(urlTemplate, function (url) { + if (url) { + window.open(url); + } + + $('body').trigger('processStop'); + }); + }); + }; +});