Passed
Push — master ( 8b92f8...a05608 )
by Bartosz
33s
created

SyncReindexRunner::addReindexFailureException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * File: IndexerProcessor.php
7
 *
8
 * @author Bartosz Kubicki [email protected]>
9
 * @author Paweł Papke <[email protected]>
10
 * @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl)
11
 */
12
13
namespace LizardMedia\AdminIndexer\Model\ReindexRunner;
14
15
use LizardMedia\AdminIndexer\Api\ReindexRunnerInterface;
16
use LizardMedia\AdminIndexer\Exception\ReindexFailureAggregateException;
17
use Magento\Framework\Indexer\IndexerRegistry;
18
19
/**
20
 * Class SyncReindexRunner
21
 * @package LizardMedia\AdminIndexer\Model\ReindexRunner
22
 */
23
class SyncReindexRunner implements ReindexRunnerInterface
24
{
25
    /**
26
     * @var ReindexFailureAggregateException
27
     */
28
    private $reindexFailureAggregateException;
29
30
    /**
31
     * @var IndexerRegistry
32
     */
33
    private $indexerRegistry;
34
35
    /**
36
     * ReindexRunner constructor.
37
     * @param IndexerRegistry $indexerRegistry
38
     */
39
    public function __construct(
40
        IndexerRegistry $indexerRegistry
41
    ) {
42
        $this->indexerRegistry = $indexerRegistry;
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48
    public function run(string ...$indexerIds): void
49
    {
50
        foreach ($indexerIds as $indexerId) {
51
            try {
52
                $indexer = $this->indexerRegistry->get($indexerId);
53
                $indexer->reindexAll();
54
            } catch (\Exception $exception) {
55
                $this->addReindexFailureException();
56
                $this->reindexFailureAggregateException->addError(
57
                    __(
58
                        'Indexing of %1 has failed: %2',
59
                        $indexerId,
60
                        $exception->getMessage()
61
                    )
62
                );
63
64
                continue;
65
            }
66
        }
67
68
        $this->handleExceptions();
69
    }
70
71
    /**
72
     * @return void
73
     */
74
    private function addReindexFailureException(): void
75
    {
76
        if (!$this->reindexFailureAggregateException instanceof ReindexFailureAggregateException) {
77
            $this->reindexFailureAggregateException = new ReindexFailureAggregateException(
78
                __('Following indexing errors has occurred: ')
79
            );
80
        }
81
    }
82
83
    /**
84
     * @throws ReindexFailureAggregateException
85
     * @return void
86
     */
87
    private function handleExceptions(): void
88
    {
89
        if ($this->reindexFailureAggregateException instanceof ReindexFailureAggregateException) {
90
            throw $this->reindexFailureAggregateException;
91
        }
92
    }
93
}
94