Skip to content

Commit 6a58f7e

Browse files
author
Viktor Kopin
committed
#29478: refactoring, test coverage
1 parent e9359f1 commit 6a58f7e

File tree

5 files changed

+333
-145
lines changed

5 files changed

+333
-145
lines changed

app/code/Magento/Indexer/Console/Command/IndexerReindexCommand.php

Lines changed: 14 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Magento\Indexer\Console\Command;
88

9+
use Magento\Framework\App\ObjectManager;
910
use Magento\Framework\App\ObjectManagerFactory;
1011
use Magento\Framework\Console\Cli;
1112
use Magento\Framework\Exception\LocalizedException;
@@ -14,11 +15,13 @@
1415
use Magento\Framework\Indexer\IndexerInterface;
1516
use Magento\Framework\Indexer\IndexerRegistry;
1617
use Magento\Framework\Indexer\StateInterface;
18+
use Magento\Indexer\Model\Processor\MakeSharedIndexValid;
1719
use Symfony\Component\Console\Input\InputInterface;
1820
use Symfony\Component\Console\Output\OutputInterface;
1921

2022
/**
2123
* Command to run indexers
24+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2225
*/
2326
class IndexerReindexCommand extends AbstractIndexerManageCommand
2427
{
@@ -42,18 +45,26 @@ class IndexerReindexCommand extends AbstractIndexerManageCommand
4245
*/
4346
private $dependencyInfoProvider;
4447

48+
/**
49+
* @var MakeSharedIndexValid|null
50+
*/
51+
private $makeSharedValid;
52+
4553
/**
4654
* @param ObjectManagerFactory $objectManagerFactory
4755
* @param IndexerRegistry|null $indexerRegistry
4856
* @param DependencyInfoProvider|null $dependencyInfoProvider
57+
* @param MakeSharedIndexValid|null $makeSharedValid
4958
*/
5059
public function __construct(
5160
ObjectManagerFactory $objectManagerFactory,
5261
IndexerRegistry $indexerRegistry = null,
53-
DependencyInfoProvider $dependencyInfoProvider = null
62+
DependencyInfoProvider $dependencyInfoProvider = null,
63+
MakeSharedIndexValid $makeSharedValid = null
5464
) {
5565
$this->indexerRegistry = $indexerRegistry;
5666
$this->dependencyInfoProvider = $dependencyInfoProvider;
67+
$this->makeSharedValid = $makeSharedValid ?: ObjectManager::getInstance()->get(MakeSharedIndexValid::class);
5768
parent::__construct($objectManagerFactory);
5869
}
5970

@@ -88,8 +99,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
8899
// Skip indexers having shared index that was already complete
89100
if (!in_array($sharedIndex, $this->sharedIndexesComplete)) {
90101
$indexer->reindexAll();
91-
if ($sharedIndex) {
92-
$this->validateSharedIndex($sharedIndex);
102+
if (!empty($sharedIndex) && $this->makeSharedValid->execute($sharedIndex)) {
103+
$this->sharedIndexesComplete[] = $sharedIndex;
93104
}
94105
}
95106
$resultTime = microtime(true) - $startTime;
@@ -214,54 +225,6 @@ private function validateIndexerStatus(IndexerInterface $indexer)
214225
}
215226
}
216227

217-
/**
218-
* Get indexer ids that have common shared index
219-
*
220-
* @param string $sharedIndex
221-
* @return array
222-
*/
223-
private function getIndexerIdsBySharedIndex($sharedIndex)
224-
{
225-
$indexers = $this->getConfig()->getIndexers();
226-
$result = [];
227-
foreach ($indexers as $indexerConfig) {
228-
if ($indexerConfig['shared_index'] == $sharedIndex) {
229-
$result[] = $indexerConfig['indexer_id'];
230-
}
231-
}
232-
return $result;
233-
}
234-
235-
/**
236-
* Validate indexers by shared index ID
237-
*
238-
* @param string $sharedIndex
239-
* @return $this
240-
*/
241-
private function validateSharedIndex($sharedIndex)
242-
{
243-
if (empty($sharedIndex)) {
244-
throw new \InvalidArgumentException(
245-
'The sharedIndex is an invalid shared index identifier. Verify the identifier and try again.'
246-
);
247-
}
248-
$indexerIds = $this->getIndexerIdsBySharedIndex($sharedIndex);
249-
if (empty($indexerIds)) {
250-
return $this;
251-
}
252-
foreach ($indexerIds as $indexerId) {
253-
$indexer = $this->getIndexerRegistry()->get($indexerId);
254-
/** @var \Magento\Indexer\Model\Indexer\State $state */
255-
$state = $indexer->getState();
256-
$state->setStatus(StateInterface::STATUS_WORKING);
257-
$state->save();
258-
$state->setStatus(StateInterface::STATUS_VALID);
259-
$state->save();
260-
}
261-
$this->sharedIndexesComplete[] = $sharedIndex;
262-
return $this;
263-
}
264-
265228
/**
266229
* Get config
267230
*
@@ -276,20 +239,6 @@ private function getConfig()
276239
return $this->config;
277240
}
278241

279-
/**
280-
* Get indexer registry
281-
*
282-
* @return IndexerRegistry
283-
* @deprecated 100.2.0
284-
*/
285-
private function getIndexerRegistry()
286-
{
287-
if (!$this->indexerRegistry) {
288-
$this->indexerRegistry = $this->getObjectManager()->get(IndexerRegistry::class);
289-
}
290-
return $this->indexerRegistry;
291-
}
292-
293242
/**
294243
* Get dependency info provider
295244
*

app/code/Magento/Indexer/Model/Processor.php

Lines changed: 16 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
*/
66
namespace Magento\Indexer\Model;
77

8+
use Magento\Framework\App\ObjectManager;
89
use Magento\Framework\Indexer\ConfigInterface;
910
use Magento\Framework\Indexer\IndexerInterface;
1011
use Magento\Framework\Indexer\IndexerInterfaceFactory;
11-
use Magento\Framework\Indexer\StateInterface;
12+
use Magento\Framework\Mview\ProcessorInterface;
13+
use Magento\Indexer\Model\Processor\MakeSharedIndexValid;
1214

1315
/**
1416
* Indexer processor
@@ -36,26 +38,34 @@ class Processor
3638
protected $indexersFactory;
3739

3840
/**
39-
* @var \Magento\Framework\Mview\ProcessorInterface
41+
* @var ProcessorInterface
4042
*/
4143
protected $mviewProcessor;
4244

45+
/**
46+
* @var MakeSharedIndexValid
47+
*/
48+
protected $makeSharedValid;
49+
4350
/**
4451
* @param ConfigInterface $config
4552
* @param IndexerInterfaceFactory $indexerFactory
4653
* @param Indexer\CollectionFactory $indexersFactory
47-
* @param \Magento\Framework\Mview\ProcessorInterface $mviewProcessor
54+
* @param ProcessorInterface $mviewProcessor
55+
* @param MakeSharedIndexValid|null $makeSharedValid
4856
*/
4957
public function __construct(
5058
ConfigInterface $config,
5159
IndexerInterfaceFactory $indexerFactory,
5260
Indexer\CollectionFactory $indexersFactory,
53-
\Magento\Framework\Mview\ProcessorInterface $mviewProcessor
61+
ProcessorInterface $mviewProcessor,
62+
MakeSharedIndexValid $makeSharedValid = null
5463
) {
5564
$this->config = $config;
5665
$this->indexerFactory = $indexerFactory;
5766
$this->indexersFactory = $indexersFactory;
5867
$this->mviewProcessor = $mviewProcessor;
68+
$this->makeSharedValid = $makeSharedValid ?: ObjectManager::getInstance()->get(MakeSharedIndexValid::class);
5969
}
6070

6171
/**
@@ -70,78 +80,21 @@ public function reindexAllInvalid()
7080
$indexer = $this->indexerFactory->create();
7181
$indexer->load($indexerId);
7282
$indexerConfig = $this->config->getIndexer($indexerId);
73-
$sharedIndex = $indexerConfig['shared_index'];
7483

7584
if ($indexer->isInvalid()) {
7685
// Skip indexers having shared index that was already complete
7786
$sharedIndex = $indexerConfig['shared_index'] ?? null;
7887
if (!in_array($sharedIndex, $this->sharedIndexesComplete)) {
7988
$indexer->reindexAll();
8089

81-
if ($sharedIndex) {
82-
$this->validateSharedIndex($sharedIndex);
90+
if (!empty($sharedIndex) && $this->makeSharedValid->execute($sharedIndex)) {
91+
$this->sharedIndexesComplete[] = $sharedIndex;
8392
}
8493
}
8594
}
8695
}
8796
}
8897

89-
/**
90-
* Get indexer ids that have common shared index
91-
*
92-
* @param string $sharedIndex
93-
* @return array
94-
*/
95-
private function getIndexerIdsBySharedIndex(string $sharedIndex): array
96-
{
97-
$indexers = $this->config->getIndexers();
98-
99-
$result = [];
100-
foreach ($indexers as $indexerConfig) {
101-
if ($indexerConfig['shared_index'] == $sharedIndex) {
102-
$result[] = $indexerConfig['indexer_id'];
103-
}
104-
}
105-
106-
return $result;
107-
}
108-
109-
/**
110-
* Validate indexers by shared index ID
111-
*
112-
* @param string $sharedIndex
113-
* @return $this
114-
*/
115-
private function validateSharedIndex(string $sharedIndex): self
116-
{
117-
if (empty($sharedIndex)) {
118-
throw new \InvalidArgumentException(
119-
'The sharedIndex is an invalid shared index identifier. Verify the identifier and try again.'
120-
);
121-
}
122-
123-
$indexerIds = $this->getIndexerIdsBySharedIndex($sharedIndex);
124-
if (empty($indexerIds)) {
125-
return $this;
126-
}
127-
128-
foreach ($indexerIds as $indexerId) {
129-
/** @var \Magento\Indexer\Model\Indexer $indexer */
130-
$indexer = $this->indexerFactory->create();
131-
$indexer->load($indexerId);
132-
/** @var \Magento\Indexer\Model\Indexer\State $state */
133-
$state = $indexer->getState();
134-
$state->setStatus(StateInterface::STATUS_WORKING);
135-
$state->save();
136-
$state->setStatus(StateInterface::STATUS_VALID);
137-
$state->save();
138-
}
139-
140-
$this->sharedIndexesComplete[] = $sharedIndex;
141-
142-
return $this;
143-
}
144-
14598
/**
14699
* Regenerate indexes for all indexers
147100
*
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Indexer\Model\Processor;
10+
11+
use Magento\Framework\Indexer\ConfigInterface;
12+
use Magento\Framework\Indexer\IndexerRegistry;
13+
use Magento\Framework\Indexer\StateInterface;
14+
use Magento\Indexer\Model\Indexer\State;
15+
16+
/**
17+
* Class processor makes indexers valid by shared index ID
18+
*/
19+
class MakeSharedIndexValid
20+
{
21+
/**
22+
* @var ConfigInterface
23+
*/
24+
private $config;
25+
26+
/**
27+
* @var IndexerRegistry
28+
*/
29+
private $indexerRegistry;
30+
31+
/**
32+
* ValidateSharedIndex constructor.
33+
*
34+
* @param ConfigInterface $config
35+
* @param IndexerRegistry $indexerRegistry
36+
*/
37+
public function __construct(ConfigInterface $config, IndexerRegistry $indexerRegistry)
38+
{
39+
$this->config = $config;
40+
$this->indexerRegistry = $indexerRegistry;
41+
}
42+
43+
/**
44+
* Validate indexers by shared index ID
45+
*
46+
* @param string $sharedIndex
47+
* @return bool
48+
* @throws \Exception
49+
*/
50+
public function execute(string $sharedIndex): bool
51+
{
52+
if (empty($sharedIndex)) {
53+
throw new \InvalidArgumentException(
54+
"The '{$sharedIndex}' is an invalid shared index identifier. Verify the identifier and try again.",
55+
);
56+
}
57+
58+
$indexerIds = $this->getIndexerIdsBySharedIndex($sharedIndex);
59+
if (empty($indexerIds)) {
60+
return false;
61+
}
62+
63+
foreach ($indexerIds as $indexerId) {
64+
$indexer = $this->indexerRegistry->get($indexerId);
65+
/** @var State $state */
66+
$state = $indexer->getState();
67+
$state->setStatus(StateInterface::STATUS_WORKING);
68+
$state->save();
69+
$state->setStatus(StateInterface::STATUS_VALID);
70+
$state->save();
71+
}
72+
73+
return true;
74+
}
75+
76+
/**
77+
* Get indexer ids that have common shared index
78+
*
79+
* @param string $sharedIndex
80+
* @return array
81+
*/
82+
private function getIndexerIdsBySharedIndex(string $sharedIndex): array
83+
{
84+
$indexers = $this->config->getIndexers();
85+
86+
$result = [];
87+
foreach ($indexers as $indexerConfig) {
88+
if ($indexerConfig['shared_index'] == $sharedIndex) {
89+
$result[] = $indexerConfig['indexer_id'];
90+
}
91+
}
92+
93+
return $result;
94+
}
95+
}

0 commit comments

Comments
 (0)