Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#309: Refactoring upgrade data/schema as patch to use customer eav entity dependency #355

Open
wants to merge 4 commits into
base: 2.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions Setup/Patch/Data/AddMpSmtpIsSyncedCustomerEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace Mageplaza\Smtp\Setup\Patch\Data;

use Magento\Customer\Model\Customer;
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;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;

class AddMpSmtpIsSyncedCustomerEntity implements DataPatchInterface
{
/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;

/**
* @var CustomerSetupFactory
*/
private $customerSetupFactory;

/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;

public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory
) {
$this->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];
}
}
77 changes: 77 additions & 0 deletions Setup/Patch/Data/UpdateSmtpConfigPath.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace Mageplaza\Smtp\Setup\Patch\Data;

use Magento\Customer\Model\Customer;
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 as ConfigCollection;
use Magento\Framework\App\Cache\TypeListInterface;

class UpdateSmtpConfigPath implements DataPatchInterface
{
/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;

/**
* @var ConfigCollection
*/
private $configCollection;

/**
* @var TypeListInterface
*/
protected $cacheTypeList;

public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
ConfigCollection $configCollection,
TypeListInterface $cacheTypeList
) {
$this->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 [];
}
}
76 changes: 76 additions & 0 deletions Setup/Patch/Schema/AddMpSmtpColumns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Mageplaza\Smtp\Setup\Patch\Schema;

use Magento\Customer\Model\ResourceModel\Customer as CustomerResource;
use Magento\Customer\Setup\Patch\Data\DefaultCustomerGroupsAndAttributes;
use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\SchemaPatchInterface;
use Magento\Newsletter\Model\ResourceModel\Subscriber as SubscriberResource;

class AddMpSmtpColumns implements SchemaPatchInterface
{
/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;

/**
* @var CustomerResource
*/
private $customerResource;

/**
* @var SubscriberResource
*/
private $subscriberResource;

public function __construct(ModuleDataSetupInterface $moduleDataSetup, CustomerResource $customerResource, SubscriberResource $subscriberResource)
{
$this->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];
}
}
Loading