@@ -28,18 +28,29 @@ class RemoveUnusedAttributesCommand extends Command
28
28
*/
29
29
private $ searchCriteriaBuilderFactory ;
30
30
31
+ /**
32
+ * Constructor
33
+ *
34
+ * @param ResourceConnection $resourceConnection
35
+ * @param AttributeRepositoryInterface $attributeRepository
36
+ * @param SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory
37
+ * @param string|null $name
38
+ */
31
39
public function __construct (
32
40
ResourceConnection $ resourceConnection ,
33
41
AttributeRepositoryInterface $ attributeRepository ,
34
42
SearchCriteriaBuilderFactory $ searchCriteriaBuilderFactory ,
35
43
string $ name = null
36
44
) {
37
45
parent ::__construct ($ name );
38
- $ this ->resourceConnection = $ resourceConnection ;
39
- $ this ->attributeRepository = $ attributeRepository ;
46
+ $ this ->resourceConnection = $ resourceConnection ;
47
+ $ this ->attributeRepository = $ attributeRepository ;
40
48
$ this ->searchCriteriaBuilderFactory = $ searchCriteriaBuilderFactory ;
41
49
}
42
50
51
+ /**
52
+ * @inheritdoc
53
+ */
43
54
protected function configure ()
44
55
{
45
56
$ this
@@ -49,76 +60,95 @@ protected function configure()
49
60
->addOption ('force ' );
50
61
}
51
62
63
+ /**
64
+ * @inheritdoc
65
+ */
52
66
public function execute (InputInterface $ input , OutputInterface $ output ): int
53
67
{
54
68
$ isDryRun = $ input ->getOption ('dry-run ' );
55
69
$ isForce = $ input ->getOption ('force ' );
56
70
57
71
if (!$ isDryRun && !$ isForce ) {
58
72
if (!$ input ->isInteractive ()) {
59
- $ output ->writeln ('ERROR: neither --dry-run nor --force options were supplied, and we are not running interactively. ' );
60
-
61
- return 1 ; // error.
73
+ $ output ->writeln (
74
+ '<error> '
75
+ //phpcs:ignore Generic.Files.LineLength.TooLong
76
+ . 'ERROR: neither --dry-run nor --force options were supplied, and we are not running interactively. '
77
+ . '</error> '
78
+ );
79
+
80
+ return Command::FAILURE ;
62
81
}
63
82
64
- $ output ->writeln ('WARNING: this is not a dry run. If you want to do a dry-run, add --dry-run. ' );
83
+ $ output ->writeln (
84
+ '<info>WARNING: this is not a dry run. If you want to do a dry-run, add --dry-run.</info> '
85
+ );
65
86
$ question = new ConfirmationQuestion ('Are you sure you want to continue? [No] ' , false );
66
87
67
88
if (!$ this ->getHelper ('question ' )->ask ($ input , $ output , $ question )) {
68
- return 1 ; // error.
89
+ return Command:: FAILURE ;
69
90
}
70
91
}
71
92
72
- $ db = $ this ->resourceConnection ->getConnection ('core_write ' );
73
- $ deleted = 0 ;
74
- $ searchCriteria = $ this ->searchCriteriaBuilderFactory ->create ()
93
+ $ db = $ this ->resourceConnection ->getConnection ('core_write ' );
94
+
95
+ $ searchCriteria = $ this ->searchCriteriaBuilderFactory ->create ()
75
96
->addFilter ('is_user_defined ' , 1 )
76
97
->addFilter ('backend_type ' , 'static ' , 'neq ' )
77
98
->create ();
78
- $ attributes = $ this ->attributeRepository
99
+ $ attributes = $ this ->attributeRepository
79
100
->getList (ProductAttributeInterface::ENTITY_TYPE_CODE , $ searchCriteria )
80
101
->getItems ();
81
- $ eavAttributeTable = $ this ->resourceConnection ->getTableName ('eav_attribute ' );
102
+ $ eavAttributeTable = $ this ->resourceConnection ->getTableName ('eav_attribute ' );
82
103
$ eavEntityAttributeTable = $ this ->resourceConnection ->getTableName ('eav_entity_attribute ' );
83
104
105
+ $ deleted = 0 ;
84
106
foreach ($ attributes as $ attribute ) {
85
- $ table = $ this ->resourceConnection ->getTableName ('catalog_product_entity_ ' . $ attribute ->getBackendType ());
107
+ $ table = $ this ->resourceConnection
108
+ ->getTableName (sprintf ('catalog_product_entity_%s ' , $ attribute ->getBackendType ()));
86
109
/* Look for attributes that have no values set in products */
87
- $ attributeValues = (int )$ db ->fetchOne ('SELECT COUNT(*) FROM ' . $ table . ' WHERE attribute_id = ? ' ,
88
- [$ attribute ->getAttributeId ()]);
110
+ $ select = $ db ->select ()
111
+ ->from ($ table , ['COUNT(*) ' ])
112
+ ->where ('attribute_id = ? ' , $ attribute ->getAttributeId ());
113
+ $ attributeValues = (int )$ db ->fetchOne ($ select );
89
114
90
115
if ($ attributeValues === 0 ) {
91
- $ output ->writeln ($ attribute ->getAttributeCode () . ' has ' . $ attributeValues
92
- . ' values; deleting attribute ' );
116
+ $ output ->writeln (
117
+ sprintf ('%s has %d values; deleting attribute ' , $ attribute ->getAttributeCode (), $ attributeValues )
118
+ );
93
119
94
120
if (!$ isDryRun ) {
95
- $ db ->query ('DELETE FROM ' . $ eavAttributeTable . ' WHERE attribute_code = ? ' ,
96
- $ attribute ->getAttributeCode ());
121
+ $ db ->delete ($ eavAttributeTable , ['attribute_code = ? ' => $ attribute ->getAttributeCode ()]);
97
122
}
98
123
99
124
$ deleted ++;
100
125
} else {
101
126
/* Look for attributes that are not assigned to attribute sets */
102
- $ attributeGroups = (int )$ db ->fetchOne ('SELECT COUNT(*) FROM ' . $ eavEntityAttributeTable
103
- . ' WHERE attribute_id = ? ' , [$ attribute ->getAttributeId ()]);
127
+ $ select = $ db ->select ()
128
+ ->from ($ eavEntityAttributeTable , ['COUNT(*) ' ])
129
+ ->where ('attribute_id = ? ' , $ attribute ->getAttributeId ());
130
+ $ attributeGroups = (int )$ db ->fetchOne ($ select );
104
131
105
132
if ($ attributeGroups === 0 ) {
106
- $ output ->writeln ($ attribute ->getAttributeCode ()
107
- . ' is not assigned to any attribute set; deleting attribute and its ' . $ attributeValues
108
- . ' orphaned value(s) ' );
133
+ $ output ->writeln (
134
+ sprintf (
135
+ '%s is not assigned to any attribute set; deleting attribute and its %d orphaned value(s) ' ,
136
+ $ attribute ->getAttributeCode (),
137
+ $ attributeValues
138
+ )
139
+ );
109
140
110
141
if (!$ isDryRun ) {
111
- $ db ->query ('DELETE FROM ' . $ eavAttributeTable . ' WHERE attribute_code = ? ' ,
112
- $ attribute ->getAttributeCode ());
142
+ $ db ->delete ($ eavAttributeTable , ['attribute_code = ? ' => $ attribute ->getAttributeCode ()]);
113
143
}
114
144
115
145
$ deleted ++;
116
146
}
117
147
}
118
148
}
119
149
120
- $ output ->writeln ('Deleted ' . $ deleted . ' attributes. ' );
150
+ $ output ->writeln (sprintf ( 'Deleted %d attributes. ' , $ deleted) );
121
151
122
- return 0 ; // success.
152
+ return Command:: SUCCESS ;
123
153
}
124
154
}
0 commit comments