Passed
Push — 6.4 ( be76f2...e19c4d )
by Christian
14:08 queued 13s
created

ElasticsearchAdminResetCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 11
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Elasticsearch\Framework\Command;
4
5
use Doctrine\DBAL\Connection;
6
use Elasticsearch\Client;
7
use Shopware\Core\Framework\Increment\Exception\IncrementGatewayNotFoundException;
8
use Shopware\Core\Framework\Increment\IncrementGatewayRegistry;
9
use Shopware\Core\Framework\Log\Package;
10
use Shopware\Elasticsearch\Admin\AdminElasticsearchHelper;
11
use Shopware\Elasticsearch\Admin\AdminSearchIndexingMessage;
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Console\Style\SymfonyStyle;
16
17
/**
18
 * @internal
19
 */
20
#[Package('system-settings')]
21
class ElasticsearchAdminResetCommand extends Command
22
{
23
    protected static $defaultName = 'es:admin:reset';
24
25
    protected static $defaultDescription = 'Reset Admin Elasticsearch indexing';
26
27
    private Client $client;
28
29
    private Connection $connection;
30
31
    private IncrementGatewayRegistry $gatewayRegistry;
32
33
    private AdminElasticsearchHelper $adminEsHelper;
34
35
    /**
36
     * @internal
37
     */
38
    public function __construct(
39
        Client $client,
40
        Connection $connection,
41
        IncrementGatewayRegistry $gatewayRegistry,
42
        AdminElasticsearchHelper $adminEsHelper
43
    ) {
44
        parent::__construct();
45
        $this->client = $client;
46
        $this->connection = $connection;
47
        $this->gatewayRegistry = $gatewayRegistry;
48
        $this->adminEsHelper = $adminEsHelper;
49
    }
50
51
    protected function execute(InputInterface $input, OutputInterface $output): int
52
    {
53
        $io = new SymfonyStyle($input, $output);
54
55
        if ($this->adminEsHelper->getEnabled() !== true) {
56
            $io->error('Admin elasticsearch is not enabled');
57
58
            return self::FAILURE;
59
        }
60
61
        $answer = $io->ask('Are you sure you want to reset the Admin Elasticsearch indexing?', 'yes');
62
63
        if ($answer !== 'yes') {
64
            $io->error('Canceled clearing indexing process');
65
66
            return self::SUCCESS;
67
        }
68
69
        $allIndices = $this->client->indices()->get(['index' => $this->adminEsHelper->getPrefix() . '*']);
70
71
        foreach ($allIndices as $index) {
72
            $this->client->indices()->delete(['index' => $index['settings']['index']['provided_name']]);
73
        }
74
75
        $this->connection->executeStatement('TRUNCATE admin_elasticsearch_index_task');
76
77
        try {
78
            $gateway = $this->gatewayRegistry->get(IncrementGatewayRegistry::MESSAGE_QUEUE_POOL);
79
            $gateway->reset('message_queue_stats', AdminSearchIndexingMessage::class);
80
        } catch (IncrementGatewayNotFoundException $exception) {
81
            // In case message_queue pool is disabled
82
        }
83
84
        $io->success('Admin Elasticsearch indices deleted and queue cleared');
85
86
        return self::SUCCESS;
87
    }
88
}
89