Skip to content

Commit 2f1e361

Browse files
committed
Refactor commands
1 parent 46c5bec commit 2f1e361

File tree

5 files changed

+122
-69
lines changed

5 files changed

+122
-69
lines changed

Console/Command/CleanUpAttributesAndValuesWithoutParentCommand.php

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace Hackathon\EAVCleaner\Console\Command;
44

5-
use Magento\Eav\Model\Entity\Type;
6-
use Magento\Framework\App\ObjectManager;
5+
use Magento\Catalog\Api\Data\CategoryAttributeInterface;
6+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
7+
use Magento\Customer\Api\AddressMetadataInterface;
8+
use Magento\Customer\Api\CustomerMetadataInterface;
9+
use Magento\Eav\Model\ResourceModel\Entity\Type\CollectionFactory as EavEntityTypeCollectionFactory;
710
use Magento\Framework\App\ResourceConnection;
811
use Symfony\Component\Console\Command\Command;
912
use Symfony\Component\Console\Input\InputInterface;
@@ -12,6 +15,26 @@
1215

1316
class CleanUpAttributesAndValuesWithoutParentCommand extends Command
1417
{
18+
/**
19+
* @var ResourceConnection
20+
*/
21+
private $resourceConnection;
22+
23+
/**
24+
* @var EavEntityTypeCollectionFactory
25+
*/
26+
private $eavEntityTypeCollectionFactory;
27+
28+
public function __construct(
29+
ResourceConnection $resourceConnection,
30+
EavEntityTypeCollectionFactory $eavEntityTypeCollectionFactory,
31+
string $name = null
32+
) {
33+
parent::__construct($name);
34+
$this->resourceConnection = $resourceConnection;
35+
$this->eavEntityTypeCollectionFactory = $eavEntityTypeCollectionFactory;
36+
}
37+
1538
protected function configure()
1639
{
1740
$description
@@ -35,33 +58,31 @@ public function execute(InputInterface $input, OutputInterface $output)
3558
}
3659
}
3760

38-
$objectManager = ObjectManager::getInstance();
39-
/** @var ResourceConnection $db */
40-
$resConnection = $objectManager->get(ResourceConnection::class);
41-
$db = $resConnection->getConnection();
61+
$db = $this->resourceConnection->getConnection();
4262
$types = ['varchar', 'int', 'decimal', 'text', 'datetime'];
4363
$entityTypeCodes = [
44-
$db->getTableName('catalog_product'),
45-
$db->getTableName('catalog_category'),
46-
$db->getTableName('customer'),
47-
$db->getTableName('customer_address')
64+
ProductAttributeInterface::ENTITY_TYPE_CODE,
65+
CategoryAttributeInterface::ENTITY_TYPE_CODE,
66+
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
67+
AddressMetadataInterface::ENTITY_TYPE_ADDRESS
4868
];
4969
foreach ($entityTypeCodes as $code) {
50-
$entityType = $objectManager->get(Type::class)
51-
->getCollection()
52-
->addFieldToFilter('code', $code);
70+
$entityType = $this->eavEntityTypeCollectionFactory
71+
->create()
72+
->addFieldToFilter('entity_type_code', $code)
73+
->getFirstItem();
5374
$output->writeln("<info>Cleaning values for $code</info>");
5475
foreach ($types as $type) {
5576
$eavTable = $db->getTableName('eav_attribute');
5677
$entityValueTable = $db->getTableName($code . '_entity_' . $type);
57-
$query = "SELECT * FROM $entityValueTable WHERE `attribute_id` not in(SELECT attribute_id"
58-
. " FROM `$eavTable`)";
78+
$query = "SELECT * FROM $entityValueTable WHERE `attribute_id` NOT IN(SELECT attribute_id"
79+
. " FROM `$eavTable` WHERE entity_type_id = " . $entityType->getEntityTypeId() . ")";
5980
$results = $db->fetchAll($query);
6081
$output->writeln("Clean up " . count($results) . " rows in $entityValueTable");
6182

6283
if (!$isDryRun && count($results) > 0) {
63-
$db->query("DELETE FROM $entityValueTable WHERE `attribute_id` not in(SELECT attribute_id"
64-
. " FROM `$eavTable` where entity_type_id = " . $entityType->getEntityTypeId() . ")");
84+
$db->query("DELETE FROM $entityValueTable WHERE `attribute_id` NOT IN(SELECT attribute_id"
85+
. " FROM `$eavTable` WHERE entity_type_id = " . $entityType->getEntityTypeId() . ")");
6586
}
6687
}
6788
}

Console/Command/RemoveUnusedAttributesCommand.php

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace Hackathon\EAVCleaner\Console\Command;
44

5-
use Magento\Eav\Model\Entity\Attribute;
6-
use Magento\Framework\App\ObjectManager;
5+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
6+
use Magento\Eav\Api\AttributeRepositoryInterface;
7+
use Magento\Framework\Api\SearchCriteriaBuilderFactory;
78
use Magento\Framework\App\ResourceConnection;
89
use Symfony\Component\Console\Command\Command;
910
use Symfony\Component\Console\Input\InputInterface;
@@ -12,6 +13,33 @@
1213

1314
class RemoveUnusedAttributesCommand extends Command
1415
{
16+
/**
17+
* @var ResourceConnection
18+
*/
19+
private $resourceConnection;
20+
21+
/**
22+
* @var AttributeRepositoryInterface
23+
*/
24+
private $attributeRepository;
25+
26+
/**
27+
* @var SearchCriteriaBuilderFactory
28+
*/
29+
private $searchCriteriaBuilderFactory;
30+
31+
public function __construct(
32+
ResourceConnection $resourceConnection,
33+
AttributeRepositoryInterface $attributeRepository,
34+
SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory,
35+
string $name = null
36+
) {
37+
parent::__construct($name);
38+
$this->resourceConnection = $resourceConnection;
39+
$this->attributeRepository = $attributeRepository;
40+
$this->searchCriteriaBuilderFactory = $searchCriteriaBuilderFactory;
41+
}
42+
1543
protected function configure()
1644
{
1745
$this
@@ -31,39 +59,40 @@ public function execute(InputInterface $input, OutputInterface $output)
3159
}
3260
}
3361

34-
$objectManager = ObjectManager::getInstance();
35-
$resource = $objectManager->get(ResourceConnection::class);
36-
$db = $resource->getConnection('core_write');
62+
$db = $this->resourceConnection->getConnection('core_write');
3763
$deleted = 0;
38-
$attributes = $objectManager->get(Attribute::class)
39-
->getCollection()
40-
->addFieldToFilter('is_user_defined', 1);
41-
$eavAttributeTable = $resource->getConnection()->getTableName('eav_attribute');
42-
$eavEntityAttributeTable = $resource->getConnection()->getTableName('eav_entity_attribute');
64+
$searchCriteria = $this->searchCriteriaBuilderFactory->create()
65+
->addFilter('is_user_defined', 1)
66+
->create();
67+
$attributes = $this->attributeRepository
68+
->getList(ProductAttributeInterface::ENTITY_TYPE_CODE, $searchCriteria)
69+
->getItems();
70+
$eavAttributeTable = $db->getTableName('eav_attribute');
71+
$eavEntityAttributeTable = $db->getTableName('eav_entity_attribute');
4372
foreach ($attributes as $attribute) {
44-
$table = $resource->getConnection()->getTableName('catalog_product_entity_' . $attribute['backend_type']);
73+
$table = $db->getTableName('catalog_product_entity_' . $attribute->getBackendType());
4574
/* Look for attributes that have no values set in products */
46-
$attributeValues = $db->fetchOne('SELECT COUNT(*) FROM ' . $table . ' WHERE attribute_id = ?',
47-
[$attribute['attribute_id']]);
48-
if ($attributeValues == 0) {
49-
$output->writeln($attribute['attribute_code'] . ' has ' . $attributeValues
75+
$attributeValues = (int)$db->fetchOne('SELECT COUNT(*) FROM ' . $table . ' WHERE attribute_id = ?',
76+
[$attribute->getAttributeId()]);
77+
if ($attributeValues === 0) {
78+
$output->writeln($attribute->getAttributeCode() . ' has ' . $attributeValues
5079
. ' values; deleting attribute');
5180
if (!$isDryRun) {
5281
$db->query('DELETE FROM ' . $eavAttributeTable . ' WHERE attribute_code = ?',
53-
$attribute['attribute_code']);
82+
$attribute->getAttributeCode());
5483
}
5584
$deleted++;
5685
} else {
5786
/* Look for attributes that are not assigned to attribute sets */
58-
$attributeGroups = $db->fetchOne('SELECT COUNT(*) FROM ' . $eavEntityAttributeTable
59-
. ' WHERE attribute_id = ?', [$attribute['attribute_id']]);
60-
if ($attributeGroups == 0) {
61-
$output->writeln($attribute['attribute_code']
87+
$attributeGroups = (int)$db->fetchOne('SELECT COUNT(*) FROM ' . $eavEntityAttributeTable
88+
. ' WHERE attribute_id = ?', [$attribute->getAttributeId()]);
89+
if ($attributeGroups === 0) {
90+
$output->writeln($attribute->getAttributeCode()
6291
. ' is not assigned to any attribute set; deleting attribute and its ' . $attributeValues
6392
. ' orphaned value(s)');
6493
if (!$isDryRun) {
6594
$db->query('DELETE FROM ' . $eavAttributeTable . ' WHERE attribute_code = ?',
66-
$attribute['attribute_code']);
95+
$attribute->getAttributeCode());
6796
}
6897
$deleted++;
6998
}

Console/Command/RemoveUnusedMediaCommand.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,8 @@ class RemoveUnusedMediaCommand extends Command
2424
*/
2525
protected $filesystem;
2626

27-
public function __construct(
28-
Filesystem $filesystem,
29-
ResourceConnection $resourceConnection,
30-
string $name = null
31-
) {
27+
public function __construct(Filesystem $filesystem, ResourceConnection $resourceConnection, string $name = null)
28+
{
3229
parent::__construct($name);
3330
$this->resourceConnection = $resourceConnection;
3431
$this->filesystem = $filesystem;
@@ -63,15 +60,11 @@ public function execute(InputInterface $input, OutputInterface $output): void
6360

6461
$imageDir = $this->getImageDir();
6562
$connection = $this->resourceConnection->getConnection('core_read');
66-
$mediaGalleryTable = $connection->getTableName(
67-
'catalog_product_entity_media_gallery'
68-
);
63+
$mediaGalleryTable = $connection->getTableName('catalog_product_entity_media_gallery');
6964

7065
$directoryIterator = new RecursiveDirectoryIterator($imageDir);
7166

72-
$imagesToKeep = $connection->fetchCol(
73-
'SELECT value FROM ' . $mediaGalleryTable
74-
);
67+
$imagesToKeep = $connection->fetchCol('SELECT value FROM ' . $mediaGalleryTable);
7568

7669
foreach (new RecursiveIteratorIterator($directoryIterator) as $file) {
7770
// Cached path guard
@@ -127,6 +120,6 @@ private function getImageDir(): string
127120

128121
private function isInCachePath(?string $file): bool
129122
{
130-
return strpos($file, "/cache") !== false;
123+
return strpos($file, '/cache') !== false;
131124
}
132125
}

Console/Command/RestoreUseDefaultConfigValueCommand.php

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

33
namespace Hackathon\EAVCleaner\Console\Command;
44

5-
use Magento\Framework\App\ObjectManager;
65
use Magento\Framework\App\ResourceConnection;
76
use Symfony\Component\Console\Command\Command;
87
use Symfony\Component\Console\Input\InputInterface;
@@ -11,6 +10,17 @@
1110

1211
class RestoreUseDefaultConfigValueCommand extends Command
1312
{
13+
/**
14+
* @var ResourceConnection
15+
*/
16+
private $resourceConnection;
17+
18+
public function __construct(ResourceConnection $resourceConnection, string $name = null)
19+
{
20+
parent::__construct($name);
21+
$this->resourceConnection = $resourceConnection;
22+
}
23+
1424
protected function configure()
1525
{
1626
$description = "Restore config's 'Use Default Value' if the non-global value is the same as the global value";
@@ -35,11 +45,8 @@ public function execute(InputInterface $input, OutputInterface $output)
3545

3646
$removedConfigValues = 0;
3747

38-
$objectManager = ObjectManager::getInstance();
39-
/** @var ResourceConnection $db */
40-
$resConnection = $objectManager->get(ResourceConnection::class);
41-
$db = $resConnection->getConnection();
42-
$configData = $db->fetchAll('SELECT DISTINCT path, value FROM ' . $db->getTableName('core_config_data')
48+
$db = $this->resourceConnection->getConnection();
49+
$configData = $db->fetchAll('SELECT DISTINCT path, value FROM ' . $db->getTableName('core_config_data')
4350
. ' WHERE scope_id = 0');
4451
foreach ($configData as $config) {
4552
$count = $db->fetchOne('SELECT COUNT(*) FROM ' . $db->getTableName('core_config_data')

Console/Command/RestoreUseDefaultValueCommand.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Hackathon\EAVCleaner\Console\Command;
44

5-
use Magento\Framework\App\ObjectManager;
65
use Magento\Framework\App\ProductMetadataInterface;
76
use Magento\Framework\App\ResourceConnection;
87
use Symfony\Component\Console\Command\Command;
@@ -18,10 +17,19 @@ class RestoreUseDefaultValueCommand extends Command
1817
*/
1918
protected $productMetaData;
2019

21-
public function __construct(ProductMetaDataInterface $productMetaData, string $name = null)
22-
{
20+
/**
21+
* @var ResourceConnection
22+
*/
23+
private $resourceConnection;
24+
25+
public function __construct(
26+
ProductMetaDataInterface $productMetaData,
27+
ResourceConnection $resourceConnection,
28+
string $name = null
29+
) {
2330
parent::__construct($name);
24-
$this->productMetaData = $productMetaData;
31+
$this->productMetaData = $productMetaData;
32+
$this->resourceConnection = $resourceConnection;
2533
}
2634

2735
protected function configure()
@@ -60,14 +68,10 @@ public function execute(InputInterface $input, OutputInterface $output)
6068
}
6169
}
6270

63-
$objectManager = ObjectManager::getInstance();
64-
/** @var ResourceConnection $db */
65-
$resConnection = $objectManager->get(ResourceConnection::class);
66-
$db = $resConnection->getConnection();
67-
$counts = [];
68-
$i = 0;
69-
$tables = ['varchar', 'int', 'decimal', 'text', 'datetime'];
70-
$column = $this->productMetaData->getEdition() === 'Enterprise' ? 'row_id' : 'entity_id';
71+
$db = $this->resourceConnection->getConnection();
72+
$counts = [];
73+
$tables = ['varchar', 'int', 'decimal', 'text', 'datetime'];
74+
$column = $this->productMetaData->getEdition() === 'Enterprise' ? 'row_id' : 'entity_id';
7175

7276
foreach ($tables as $table) {
7377
// Select all non-global values
@@ -101,7 +105,6 @@ public function execute(InputInterface $input, OutputInterface $output)
101105
$counts[$row['attribute_id']] = 0;
102106
}
103107
$counts[$row['attribute_id']]++;
104-
$i++;
105108
}
106109
}
107110

0 commit comments

Comments
 (0)