Skip to content

Commit 5c4aad7

Browse files
committed
Refactor code
1 parent eaf3917 commit 5c4aad7

8 files changed

+124
-169
lines changed

Console/Command/CleanUpAttributesAndValuesWithoutParentCommand.php

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,27 @@
11
<?php
2+
23
namespace Hackathon\EAVCleaner\Console\Command;
34

5+
use Magento\Eav\Model\Entity\Type;
6+
use Magento\Framework\App\ObjectManager;
7+
use Magento\Framework\App\ResourceConnection;
48
use Symfony\Component\Console\Command\Command;
59
use Symfony\Component\Console\Input\InputInterface;
610
use Symfony\Component\Console\Output\OutputInterface;
711
use Symfony\Component\Console\Question\ConfirmationQuestion;
812

913
class CleanUpAttributesAndValuesWithoutParentCommand extends Command
1014
{
11-
public $questionHelper;
12-
13-
/**
14-
* Init command
15-
*/
1615
protected function configure()
1716
{
17+
$description
18+
= 'Remove catalog_eav_attribute and attribute values which are missing parent entry in eav_attribute';
1819
$this
1920
->setName('eav:clean:attributes-and-values-without-parent')
20-
->setDescription("
21-
Restore product's 'Use Default Value' if the non-global value is the same as the global value
22-
")
21+
->setDescription($description)
2322
->addOption('dry-run');
2423
}
2524

26-
/**
27-
* Execute Command
28-
*
29-
* @param InputInterface $input
30-
* @param OutputInterface $output
31-
*
32-
* @return void;
33-
*/
3425
public function execute(InputInterface $input, OutputInterface $output)
3526
{
3627
$isDryRun = $input->getOption('dry-run');
@@ -39,30 +30,33 @@ public function execute(InputInterface $input, OutputInterface $output)
3930
$output->writeln('WARNING: this is not a dry run. If you want to do a dry-run, add --dry-run.');
4031
$question = new ConfirmationQuestion('Are you sure you want to continue? [No] ', false);
4132

42-
$this->questionHelper = $this->getHelper('question');
43-
if (!$this->questionHelper->ask($input, $output, $question)) {
33+
if (!$this->getHelper('question')->ask($input, $output, $question)) {
4434
return;
4535
}
4636
}
4737

48-
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
49-
/** @var \Magento\Framework\App\ResourceConnection $db */
50-
$resConnection = $objectManager->get('Magento\Framework\App\ResourceConnection');
51-
$db = $resConnection->getConnection();
52-
$types = array('varchar', 'int', 'decimal', 'text', 'datetime');
53-
$entityTypeCodes = array($db->getTableName('catalog_product'), $db->getTableName('catalog_category'), $db->getTableName('customer'), $db->getTableName('customer_address'));
38+
$objectManager = ObjectManager::getInstance();
39+
/** @var ResourceConnection $db */
40+
$resConnection = $objectManager->get(ResourceConnection::class);
41+
$db = $resConnection->getConnection();
42+
$types = ['varchar', 'int', 'decimal', 'text', 'datetime'];
43+
$entityTypeCodes = [
44+
$db->getTableName('catalog_product'),
45+
$db->getTableName('catalog_category'),
46+
$db->getTableName('customer'),
47+
$db->getTableName('customer_address')
48+
];
5449
foreach ($entityTypeCodes as $code) {
55-
$entityType = $objectManager->get('Magento\Eav\Model\Entity\Type')
50+
$entityType = $objectManager->get(Type::class)
5651
->getCollection()
5752
->addFieldToFilter('code', $code);
5853
$output->writeln("<info>Cleaning values for $code</info>");
59-
//removing attribute values
6054
foreach ($types as $type) {
61-
$eavTable = $db->getTableName('eav_attribute');
55+
$eavTable = $db->getTableName('eav_attribute');
6256
$entityValueTable = $db->getTableName($code . '_entity_' . $type);
63-
$query = "SELECT * FROM $entityValueTable WHERE `attribute_id` not in(SELECT attribute_id"
57+
$query = "SELECT * FROM $entityValueTable WHERE `attribute_id` not in(SELECT attribute_id"
6458
. " FROM `$eavTable`)";
65-
$results = $db->fetchAll($query);
59+
$results = $db->fetchAll($query);
6660
$output->writeln("Clean up " . count($results) . " rows in $entityValueTable");
6761

6862
if (!$isDryRun && count($results) > 0) {

Console/Command/RemoveUnusedAttributesCommand.php

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,69 @@
11
<?php
2+
23
namespace Hackathon\EAVCleaner\Console\Command;
34

5+
use Magento\Eav\Model\Entity\Attribute;
6+
use Magento\Framework\App\ObjectManager;
7+
use Magento\Framework\App\ResourceConnection;
48
use Symfony\Component\Console\Command\Command;
59
use Symfony\Component\Console\Input\InputInterface;
610
use Symfony\Component\Console\Output\OutputInterface;
711
use Symfony\Component\Console\Question\ConfirmationQuestion;
8-
use Symfony\Component\Console\Input\InputOption;
912

1013
class RemoveUnusedAttributesCommand extends Command
1114
{
12-
/**
13-
* Init command
14-
*/
1515
protected function configure()
1616
{
1717
$this
1818
->setName('eav:attributes:remove-unused')
19-
->setDescription('Remove unused attributes')
19+
->setDescription('Remove unused attributes (without values or not assigned to any attribute set')
2020
->addOption('dry-run');
2121
}
2222

23-
/**
24-
* Execute Command
25-
*
26-
* @param InputInterface $input
27-
* @param OutputInterface $output
28-
*
29-
* @return void;
30-
*/
3123
public function execute(InputInterface $input, OutputInterface $output)
3224
{
3325
$isDryRun = $input->getOption('dry-run');
3426
if (!$isDryRun && $input->isInteractive()) {
3527
$output->writeln('WARNING: this is not a dry run. If you want to do a dry-run, add --dry-run.');
3628
$question = new ConfirmationQuestion('Are you sure you want to continue? [No] ', false);
37-
$this->questionHelper = $this->getHelper('question');
38-
if (!$this->questionHelper->ask($input, $output, $question)) {
29+
if (!$this->getHelper('question')->ask($input, $output, $question)) {
3930
return;
4031
}
4132
}
4233

43-
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
44-
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
45-
$db = $resource->getConnection('core_write');
46-
$deleted = 0;
47-
$attributes = $objectManager->get('Magento\Eav\Model\Entity\Attribute')
34+
$objectManager = ObjectManager::getInstance();
35+
$resource = $objectManager->get(ResourceConnection::class);
36+
$db = $resource->getConnection('core_write');
37+
$deleted = 0;
38+
$attributes = $objectManager->get(Attribute::class)
4839
->getCollection()
4940
->addFieldToFilter('is_user_defined', 1);
50-
$eavAttributeTable = $resource->getConnection()->getTableName('eav_attribute');
41+
$eavAttributeTable = $resource->getConnection()->getTableName('eav_attribute');
5142
$eavEntityAttributeTable = $resource->getConnection()->getTableName('eav_entity_attribute');
5243
foreach ($attributes as $attribute) {
5344
$table = $resource->getConnection()->getTableName('catalog_product_entity_' . $attribute['backend_type']);
5445
/* Look for attributes that have no values set in products */
55-
$attributeValues = $db->fetchOne('SELECT COUNT(*) FROM ' . $table . ' WHERE attribute_id = ?', array($attribute['attribute_id']));
46+
$attributeValues = $db->fetchOne('SELECT COUNT(*) FROM ' . $table . ' WHERE attribute_id = ?',
47+
[$attribute['attribute_id']]);
5648
if ($attributeValues == 0) {
57-
$output->writeln($attribute['attribute_code'] . ' has ' . $attributeValues . ' values; deleting attribute');
49+
$output->writeln($attribute['attribute_code'] . ' has ' . $attributeValues
50+
. ' values; deleting attribute');
5851
if (!$isDryRun) {
59-
$db->query('DELETE FROM ' . $eavAttributeTable . ' WHERE attribute_code = ?', $attribute['attribute_code']);
52+
$db->query('DELETE FROM ' . $eavAttributeTable . ' WHERE attribute_code = ?',
53+
$attribute['attribute_code']);
6054
}
6155
$deleted++;
6256
} else {
6357
/* Look for attributes that are not assigned to attribute sets */
64-
$attributeGroups = $db->fetchOne('SELECT COUNT(*) FROM ' . $eavEntityAttributeTable . ' WHERE attribute_id = ?', array($attribute['attribute_id']));
58+
$attributeGroups = $db->fetchOne('SELECT COUNT(*) FROM ' . $eavEntityAttributeTable
59+
. ' WHERE attribute_id = ?', [$attribute['attribute_id']]);
6560
if ($attributeGroups == 0) {
66-
$output->writeln($attribute['attribute_code'] . ' is not assigned to any attribute set; deleting attribute and its ' . $attributeValues . ' orphaned value(s)');
61+
$output->writeln($attribute['attribute_code']
62+
. ' is not assigned to any attribute set; deleting attribute and its ' . $attributeValues
63+
. ' orphaned value(s)');
6764
if (!$isDryRun) {
68-
$db->query('DELETE FROM ' . $eavAttributeTable . ' WHERE attribute_code = ?', $attribute['attribute_code']);
65+
$db->query('DELETE FROM ' . $eavAttributeTable . ' WHERE attribute_code = ?',
66+
$attribute['attribute_code']);
6967
}
7068
$deleted++;
7169
}

Console/Command/RemoveUnusedMediaCommand.php

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,23 @@
22

33
namespace Hackathon\EAVCleaner\Console\Command;
44

5+
use Magento\Framework\App\Filesystem\DirectoryList;
56
use Magento\Framework\App\ResourceConnection;
67
use Magento\Framework\Filesystem;
8+
use RecursiveDirectoryIterator;
9+
use RecursiveIteratorIterator;
710
use Symfony\Component\Console\Command\Command;
811
use Symfony\Component\Console\Input\InputInterface;
912
use Symfony\Component\Console\Output\OutputInterface;
1013
use Symfony\Component\Console\Question\ConfirmationQuestion;
11-
use Symfony\Component\Console\Input\InputOption;
12-
use Magento\Framework\App\Filesystem\DirectoryList;
1314

14-
/**
15-
* Class RemoveUnusedMediaCommand
16-
* @package Hackathon\EAVCleaner\Console\Command
17-
*/
1815
class RemoveUnusedMediaCommand extends Command
1916
{
2017
/**
2118
* @var ResourceConnection
2219
*/
2320
protected $resourceConnection;
21+
2422
/**
2523
* @var Filesystem
2624
*/
@@ -33,7 +31,7 @@ public function __construct(
3331
) {
3432
parent::__construct($name);
3533
$this->resourceConnection = $resourceConnection;
36-
$this->filesystem = $filesystem;
34+
$this->filesystem = $filesystem;
3735
}
3836

3937
/**
@@ -47,11 +45,11 @@ protected function configure()
4745
->addOption('dry-run');
4846
}
4947

50-
public function execute(InputInterface $input, OutputInterface $output) : void
48+
public function execute(InputInterface $input, OutputInterface $output): void
5149
{
52-
$fileSize = 0;
50+
$fileSize = 0;
5351
$countFiles = 0;
54-
$isDryRun = $input->getOption('dry-run');
52+
$isDryRun = $input->getOption('dry-run');
5553

5654
if (!$isDryRun && $input->isInteractive()) {
5755
$output->writeln(
@@ -63,20 +61,19 @@ public function execute(InputInterface $input, OutputInterface $output) : void
6361
}
6462
}
6563

66-
$imageDir = $this->getImageDir();
67-
$connection = $this->resourceConnection->getConnection('core_read');
64+
$imageDir = $this->getImageDir();
65+
$connection = $this->resourceConnection->getConnection('core_read');
6866
$mediaGalleryTable = $connection->getTableName(
6967
'catalog_product_entity_media_gallery'
7068
);
7169

72-
$directoryIterator = new \RecursiveDirectoryIterator($imageDir);
73-
70+
$directoryIterator = new RecursiveDirectoryIterator($imageDir);
71+
7472
$imagesToKeep = $connection->fetchCol(
7573
'SELECT value FROM ' . $mediaGalleryTable
7674
);
7775

78-
foreach (new \RecursiveIteratorIterator($directoryIterator) as $file) {
79-
76+
foreach (new RecursiveIteratorIterator($directoryIterator) as $file) {
8077
// Cached path guard
8178
if ($this->isInCachePath($file)) {
8279
continue;
@@ -124,10 +121,10 @@ private function printResult(OutputInterface $output, $isDryRun, int $countFiles
124121
private function getImageDir(): string
125122
{
126123
$directory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
124+
127125
return $directory->getAbsolutePath() . DIRECTORY_SEPARATOR . 'catalog' . DIRECTORY_SEPARATOR . 'product';
128126
}
129127

130-
131128
private function isInCachePath(?string $file): bool
132129
{
133130
return strpos($file, "/cache") !== false;

Console/Command/RestoreUseDefaultConfigValueCommand.php

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,25 @@
11
<?php
2+
23
namespace Hackathon\EAVCleaner\Console\Command;
34

5+
use Magento\Framework\App\ObjectManager;
6+
use Magento\Framework\App\ResourceConnection;
47
use Symfony\Component\Console\Command\Command;
58
use Symfony\Component\Console\Input\InputInterface;
69
use Symfony\Component\Console\Output\OutputInterface;
710
use Symfony\Component\Console\Question\ConfirmationQuestion;
811

912
class RestoreUseDefaultConfigValueCommand extends Command
1013
{
11-
public $questionHelper;
12-
13-
/**
14-
* Init command
15-
*/
1614
protected function configure()
1715
{
16+
$description = "Restore config's 'Use Default Value' if the non-global value is the same as the global value";
1817
$this
1918
->setName('eav:config:restore-use-default-value')
20-
->setDescription("
21-
Restore config's 'Use Default Value' if the non-global value is the same as the global value
22-
")
19+
->setDescription($description)
2320
->addOption('dry-run');
2421
}
2522

26-
/**
27-
* Execute Command
28-
*
29-
* @param InputInterface $input
30-
* @param OutputInterface $output
31-
*
32-
* @return void;
33-
*/
3423
public function execute(InputInterface $input, OutputInterface $output)
3524
{
3625
$isDryRun = $input->getOption('dry-run');
@@ -39,28 +28,31 @@ public function execute(InputInterface $input, OutputInterface $output)
3928
$output->writeln('WARNING: this is not a dry run. If you want to do a dry-run, add --dry-run.');
4029
$question = new ConfirmationQuestion('Are you sure you want to continue? [No] ', false);
4130

42-
$this->questionHelper = $this->getHelper('question');
43-
if (!$this->questionHelper->ask($input, $output, $question)) {
31+
if (!$this->getHelper('question')->ask($input, $output, $question)) {
4432
return;
4533
}
4634
}
47-
35+
4836
$removedConfigValues = 0;
4937

50-
/** @var \Mage_Core_Model_Resource $resource */
51-
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
52-
/** @var \Magento\Framework\App\ResourceConnection $db */
53-
$resConnection = $objectManager->get('Magento\Framework\App\ResourceConnection');
54-
$db = $resConnection->getConnection();
55-
$configData = $db->fetchAll('SELECT DISTINCT path, value FROM ' . $db->getTableName('core_config_data') . ' WHERE scope_id = 0');
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')
43+
. ' WHERE scope_id = 0');
5644
foreach ($configData as $config) {
57-
$count = $db->fetchOne('SELECT COUNT(*) FROM ' . $db->getTableName('core_config_data') .' WHERE path = ? AND BINARY value = ?', array($config['path'], $config['value']));
45+
$count = $db->fetchOne('SELECT COUNT(*) FROM ' . $db->getTableName('core_config_data')
46+
. ' WHERE path = ? AND BINARY value = ?', [$config['path'], $config['value']]);
5847
if ($count > 1) {
59-
$output->writeln('Config path ' . $config['path'] . ' with value ' . $config['value']. ' has ' . $count . ' values; deleting non-default values');
48+
$output->writeln('Config path ' . $config['path'] . ' with value ' . $config['value'] . ' has ' . $count
49+
. ' values; deleting non-default values');
6050
if (!$isDryRun) {
61-
$db->query('DELETE FROM ' . $db->getTableName('core_config_data') . ' WHERE path = ? AND BINARY value = ? AND scope_id != ?', array($config['path'], $config['value'], 0));
51+
$db->query('DELETE FROM ' . $db->getTableName('core_config_data')
52+
. ' WHERE path = ? AND BINARY value = ? AND scope_id != ?',
53+
[$config['path'], $config['value'], 0]);
6254
}
63-
$removedConfigValues += ($count-1);
55+
$removedConfigValues += ($count - 1);
6456
}
6557
}
6658
$output->writeln('Removed ' . $removedConfigValues . ' values from core_config_data table.');

0 commit comments

Comments
 (0)