From 2cf9d24451ddd1730d34a4cb865e291703a9d720 Mon Sep 17 00:00:00 2001 From: Melvin Capron Date: Sun, 9 Jan 2022 18:15:57 +0100 Subject: [PATCH 1/4] #309: Refactoring upgrade data/schema as patch to use customer eav entity dependency --- .../Data/AddMpSmtpIsSyncedCustomerEntity.php | 89 ++++++++++ Setup/Patch/Data/UpdateSmtpConfigPath.php | 78 +++++++++ Setup/Patch/Schema/AddMpSmtpColumns.php | 76 +++++++++ Setup/UpgradeData.php | 156 ------------------ Setup/UpgradeSchema.php | 55 +----- 5 files changed, 244 insertions(+), 210 deletions(-) create mode 100644 Setup/Patch/Data/AddMpSmtpIsSyncedCustomerEntity.php create mode 100644 Setup/Patch/Data/UpdateSmtpConfigPath.php create mode 100644 Setup/Patch/Schema/AddMpSmtpColumns.php delete mode 100644 Setup/UpgradeData.php diff --git a/Setup/Patch/Data/AddMpSmtpIsSyncedCustomerEntity.php b/Setup/Patch/Data/AddMpSmtpIsSyncedCustomerEntity.php new file mode 100644 index 0000000..11b52cc --- /dev/null +++ b/Setup/Patch/Data/AddMpSmtpIsSyncedCustomerEntity.php @@ -0,0 +1,89 @@ +moduleDataSetup = $moduleDataSetup; + $this->customerSetupFactory = $customerSetupFactory; + $this->attributeSetFactory = $attributeSetFactory; + } + + public function apply(): void + { + $setup = $this->moduleDataSetup->startSetup(); + + /** @var CustomerSetup $customerSetup */ + $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); + + $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer'); + $attributeSetId = $customerEntity->getDefaultAttributeSetId(); + + /** @var $attributeSet AttributeSet */ + $attributeSet = $this->attributeSetFactory->create(); + $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId); + + $customerSetup->addAttribute(Customer::ENTITY, 'mp_smtp_is_synced', [ + 'type' => 'int', + 'label' => 'Mp SMTP is synced', + 'input' => 'hidden', + 'required' => false, + 'visible' => false, + 'user_defined' => false, + 'sort_order' => 90, + 'position' => 90, + 'system' => 0, + 'is_used_in_grid' => false, + ]); + + $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'mp_smtp_is_synced') + ->addData([ + 'attribute_set_id' => $attributeSetId, + 'attribute_group_id' => $attributeGroupId, + 'used_in_forms' => ['adminhtml_customer'] + ]) + ->save(); + + $this->moduleDataSetup->endSetup(); + } + + public function getAliases(): array + { + return []; + } + + public static function getDependencies(): array + { + return [DefaultCustomerGroupsAndAttributes::class]; + } +} diff --git a/Setup/Patch/Data/UpdateSmtpConfigPath.php b/Setup/Patch/Data/UpdateSmtpConfigPath.php new file mode 100644 index 0000000..c543dc3 --- /dev/null +++ b/Setup/Patch/Data/UpdateSmtpConfigPath.php @@ -0,0 +1,78 @@ +moduleDataSetup = $moduleDataSetup; + $this->configCollection = $configCollection; + $this->cacheTypeList = $cacheTypeList; + } + + public function apply(): void + { + $setup = $this->moduleDataSetup->startSetup(); + + $connection = $setup->getConnection(); + $configCollection = $this->configCollection->addPathFilter('smtp/abandoned_cart'); + if ($configCollection->getSize() > 0) { + $table = $this->configCollection->getMainTable(); + $paths = [ + 'smtp/abandoned_cart/enabled' => 'email_marketing/general/enabled', + 'smtp/abandoned_cart/app_id' => 'email_marketing/general/app_id', + 'smtp/abandoned_cart/secret_key' => 'email_marketing/general/secret_key' + ]; + + foreach ($paths as $oldPath => $newPath) { + $connection->update( + $table, + ['path' => $newPath], + ['path = ?' => $oldPath] + ); + } + $this->cacheTypeList->cleanType('config'); + } + + $this->moduleDataSetup->endSetup(); + } + + public function getAliases(): array + { + return []; + } + + public static function getDependencies(): array + { + return []; + } +} diff --git a/Setup/Patch/Schema/AddMpSmtpColumns.php b/Setup/Patch/Schema/AddMpSmtpColumns.php new file mode 100644 index 0000000..bb26d40 --- /dev/null +++ b/Setup/Patch/Schema/AddMpSmtpColumns.php @@ -0,0 +1,76 @@ +moduleDataSetup = $moduleDataSetup; + $this->customerResource = $customerResource; + $this->subscriberResource = $subscriberResource; + } + + public function apply(): void + { + $setup = $this->moduleDataSetup->startSetup(); + + $column = [ + 'type' => Table::TYPE_SMALLINT, + 'nullable' => true, + 'length' => null, + 'default' => 0, + 'comment' => 'Mp SMTP Email Marketing synced' + ]; + + $customerConnection = $this->customerResource->getConnection(); + $customerConnection->addColumn( + $setup->getTable('customer_entity'), + 'mp_smtp_email_marketing_synced', + $column + ); + + $subscriberConnection = $this->subscriberResource->getConnection(); + $subscriberConnection->addColumn( + $setup->getTable('newsletter_subscriber'), + 'mp_smtp_email_marketing_synced', + $column + ); + + $this->moduleDataSetup->endSetup(); + } + + public function getAliases(): array + { + return []; + } + + public static function getDependencies(): array + { + return [DefaultCustomerGroupsAndAttributes::class]; + } +} diff --git a/Setup/UpgradeData.php b/Setup/UpgradeData.php deleted file mode 100644 index 90ef64a..0000000 --- a/Setup/UpgradeData.php +++ /dev/null @@ -1,156 +0,0 @@ -salesSetupFactory = $salesSetupFactory; - $this->attributeSetFactory = $attributeSetFactory; - $this->customerSetupFactory = $customerSetupFactory; - $this->configCollection = $configCollection; - $this->_cacheTypeList = $cacheTypeList; - } - - /** - * @param ModuleDataSetupInterface $setup - * @param ModuleContextInterface $context - * - * @throws Exception - */ - public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) - { - if (version_compare($context->getVersion(), '1.2.2', '<')) { - $setup->startSetup(); - - /** @var CustomerSetup $customerSetup */ - $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); - - $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer'); - $attributeSetId = $customerEntity->getDefaultAttributeSetId(); - - /** @var $attributeSet AttributeSet */ - $attributeSet = $this->attributeSetFactory->create(); - $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId); - - $customerSetup->addAttribute(Customer::ENTITY, 'mp_smtp_is_synced', [ - 'type' => 'int', - 'label' => 'Mp SMTP is synced', - 'input' => 'hidden', - 'required' => false, - 'visible' => false, - 'user_defined' => false, - 'sort_order' => 90, - 'position' => 90, - 'system' => 0, - 'is_used_in_grid' => false, - ]); - - $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'mp_smtp_is_synced') - ->addData([ - 'attribute_set_id' => $attributeSetId, - 'attribute_group_id' => $attributeGroupId, - 'used_in_forms' => ['adminhtml_customer'] - ]) - ->save(); - } - - if (version_compare($context->getVersion(), '1.2.3', '<')) { - $connection = $setup->getConnection(); - $configCollection = $this->configCollection->addPathFilter('smtp/abandoned_cart'); - if ($configCollection->getSize() > 0) { - $table = $this->configCollection->getMainTable(); - $paths = [ - 'smtp/abandoned_cart/enabled' => 'email_marketing/general/enabled', - 'smtp/abandoned_cart/app_id' => 'email_marketing/general/app_id', - 'smtp/abandoned_cart/secret_key' => 'email_marketing/general/secret_key' - ]; - - foreach ($paths as $oldPath => $newPath) { - $connection->update( - $table, - ['path' => $newPath], - ['path = ?' => $oldPath] - ); - } - $this->_cacheTypeList->cleanType('config'); - } - } - } -} diff --git a/Setup/UpgradeSchema.php b/Setup/UpgradeSchema.php index 52e63ee..cbcd821 100644 --- a/Setup/UpgradeSchema.php +++ b/Setup/UpgradeSchema.php @@ -27,8 +27,6 @@ use Magento\Framework\Setup\UpgradeSchemaInterface; use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; use Magento\Sales\Model\ResourceModel\Order as OrderResource; -use Magento\Customer\Model\ResourceModel\Customer as CustomerResource; -use Magento\Newsletter\Model\ResourceModel\Subscriber as SubscriberResource; use Zend_Db_Exception; /** @@ -47,34 +45,18 @@ class UpgradeSchema implements UpgradeSchemaInterface */ protected $orderResource; - /** - * @var CustomerResource - */ - protected $customerResource; - - /** - * @var SubscriberResource - */ - protected $subscriberResource; - /** * UpgradeSchema constructor. * * @param QuoteResource $quoteResource * @param OrderResource $orderResource - * @param CustomerResource $customerResource - * @param SubscriberResource $subscriberResource */ public function __construct( QuoteResource $quoteResource, - OrderResource $orderResource, - CustomerResource $customerResource, - SubscriberResource $subscriberResource + OrderResource $orderResource ) { $this->quoteResource = $quoteResource; $this->orderResource = $orderResource; - $this->customerResource = $customerResource; - $this->subscriberResource = $subscriberResource; } /** @@ -218,41 +200,6 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con ]); } - if (version_compare($context->getVersion(), '1.2.4', '<')) { - $column = [ - 'type' => Table::TYPE_SMALLINT, - 'nullable' => true, - 'length' => null, - 'default' => 0, - 'comment' => 'Mp SMTP Email Marketing synced' - ]; - - $customerConnection = $this->customerResource->getConnection(); - $customerConnection->addColumn( - $setup->getTable('customer_entity'), - 'mp_smtp_email_marketing_synced', - $column - ); - - $subscriberConnection = $this->subscriberResource->getConnection(); - $subscriberConnection->addColumn( - $setup->getTable('newsletter_subscriber'), - 'mp_smtp_email_marketing_synced', - $column - ); - } - - if (version_compare($context->getVersion(), '1.2.5', '<')) { - $salesOrderConnection = $this->orderResource->getConnection(); - $salesOrderConnection->addColumn($setup->getTable('sales_order'), 'mp_smtp_email_marketing_order_created', [ - 'type' => Table::TYPE_SMALLINT, - 'nullable' => true, - 'length' => null, - 'default' => 0, - 'comment' => 'Mp SMTP Email Marketing order created' - ]); - } - $setup->endSetup(); } } From ec8584fd890a0ce591205d6f54a35b1b0b4a1e06 Mon Sep 17 00:00:00 2001 From: Melvin Capron Date: Sun, 9 Jan 2022 18:28:58 +0100 Subject: [PATCH 2/4] Bump to 4.7.1 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 11f5555..3428e08 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "mageplaza/module-core": "^1.4.11" }, "type": "magento2-module", - "version": "4.7.0", + "version": "4.7.1", "license": "proprietary", "authors": [ { From 877dbb2993c25404ca62fdd3df661b6764903081 Mon Sep 17 00:00:00 2001 From: Melvin Capron Date: Sun, 9 Jan 2022 21:11:26 +0100 Subject: [PATCH 3/4] Fixing import classes --- Setup/Patch/Data/AddMpSmtpIsSyncedCustomerEntity.php | 2 +- Setup/Patch/Data/UpdateSmtpConfigPath.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Setup/Patch/Data/AddMpSmtpIsSyncedCustomerEntity.php b/Setup/Patch/Data/AddMpSmtpIsSyncedCustomerEntity.php index 11b52cc..55fe100 100644 --- a/Setup/Patch/Data/AddMpSmtpIsSyncedCustomerEntity.php +++ b/Setup/Patch/Data/AddMpSmtpIsSyncedCustomerEntity.php @@ -5,7 +5,7 @@ namespace Mageplaza\Smtp\Setup\Patch\Data; use Magento\Customer\Model\Customer; -use Magento\Customer\Setup\CustomerSetup; +use Magento\Customer\Setup\CustomerSetupFactory; use Magento\Customer\Setup\Patch\Data\DefaultCustomerGroupsAndAttributes; use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet; use Magento\Framework\Setup\ModuleDataSetupInterface; diff --git a/Setup/Patch/Data/UpdateSmtpConfigPath.php b/Setup/Patch/Data/UpdateSmtpConfigPath.php index c543dc3..7982e0a 100644 --- a/Setup/Patch/Data/UpdateSmtpConfigPath.php +++ b/Setup/Patch/Data/UpdateSmtpConfigPath.php @@ -9,7 +9,7 @@ use Magento\Customer\Setup\Patch\Data\DefaultCustomerGroupsAndAttributes; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; -use Magento\Config\Model\ResourceModel\Config\Data\Collection; +use Magento\Config\Model\ResourceModel\Config\Data\Collection as ConfigCollection; use Magento\Framework\App\Cache\TypeListInterface; class UpdateSmtpConfigPath implements DataPatchInterface @@ -20,7 +20,7 @@ class UpdateSmtpConfigPath implements DataPatchInterface private $moduleDataSetup; /** - * @var Collection + * @var ConfigCollection */ private $configCollection; From 0b7532e24f8cbe9af6dbb643d093772d356c13ef Mon Sep 17 00:00:00 2001 From: Melvin Capron Date: Sun, 9 Jan 2022 21:11:47 +0100 Subject: [PATCH 4/4] Remove unused --- Setup/Patch/Data/UpdateSmtpConfigPath.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Setup/Patch/Data/UpdateSmtpConfigPath.php b/Setup/Patch/Data/UpdateSmtpConfigPath.php index 7982e0a..2f02dae 100644 --- a/Setup/Patch/Data/UpdateSmtpConfigPath.php +++ b/Setup/Patch/Data/UpdateSmtpConfigPath.php @@ -5,7 +5,6 @@ namespace Mageplaza\Smtp\Setup\Patch\Data; use Magento\Customer\Model\Customer; -use Magento\Customer\Setup\CustomerSetup; use Magento\Customer\Setup\Patch\Data\DefaultCustomerGroupsAndAttributes; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface;