Passed
Push — trunk ( 544851...4bb49d )
by Christian
10:04 queued 12s
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 OpenSearch\Client;
7
use Shopware\Core\Framework\Increment\Exception\IncrementGatewayNotFoundException;
8
use Shopware\Core\Framework\Increment\IncrementGatewayRegistry;
9
use Shopware\Elasticsearch\Admin\AdminElasticsearchHelper;
10
use Shopware\Elasticsearch\Admin\AdminSearchIndexingMessage;
11
use Symfony\Component\Console\Attribute\AsCommand;
12
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Console\Style\SymfonyStyle;
16
17
/**
18
 * @package system-settings
19
 *
20
 * @internal
21
 */
22
#[AsCommand(
23
    name: 'es:admin:reset',
24
    description: 'Reset Admin Elasticsearch indexing',
25
)]
26
class ElasticsearchAdminResetCommand extends Command
27
{
28
    private Client $client;
29
30
    private Connection $connection;
31
32
    private IncrementGatewayRegistry $gatewayRegistry;
33
34
    private AdminElasticsearchHelper $adminEsHelper;
35
36
    /**
37
     * @internal
38
     */
39
    public function __construct(
40
        Client $client,
41
        Connection $connection,
42
        IncrementGatewayRegistry $gatewayRegistry,
43
        AdminElasticsearchHelper $adminEsHelper
44
    ) {
45
        parent::__construct();
46
        $this->client = $client;
47
        $this->connection = $connection;
48
        $this->gatewayRegistry = $gatewayRegistry;
49
        $this->adminEsHelper = $adminEsHelper;
50
    }
51
52
    protected function execute(InputInterface $input, OutputInterface $output): int
53
    {
54
        $io = new SymfonyStyle($input, $output);
55
56
        if ($this->adminEsHelper->getEnabled() !== true) {
57
            $io->error('Admin elasticsearch is not enabled');
58
59
            return self::FAILURE;
60
        }
61
62
        $answer = $io->ask('Are you sure you want to reset the Admin Elasticsearch indexing?', 'yes');
63
64
        if ($answer !== 'yes') {
65
            $io->error('Canceled clearing indexing process');
66
67
            return self::SUCCESS;
68
        }
69
70
        $allIndices = $this->client->indices()->get(['index' => $this->adminEsHelper->getPrefix() . '*']);
71
72
        foreach ($allIndices as $index) {
73
            $this->client->indices()->delete(['index' => $index['settings']['index']['provided_name']]);
74
        }
75
76
        $this->connection->executeStatement('TRUNCATE admin_elasticsearch_index_task');
77
78
        try {
79
            $gateway = $this->gatewayRegistry->get(IncrementGatewayRegistry::MESSAGE_QUEUE_POOL);
80
            $gateway->reset('message_queue_stats', AdminSearchIndexingMessage::class);
81
        } catch (IncrementGatewayNotFoundException $exception) {
82
            // In case message_queue pool is disabled
83
        }
84
85
        $io->success('Admin Elasticsearch indices deleted and queue cleared');
86
87
        return self::SUCCESS;
88
    }
89
}
90