Issues (5)

src/Command/PopulateCurrentDataTrackersCommand.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Locastic\Loggastic\Command;
4
5
use Doctrine\Persistence\ManagerRegistry;
6
use Locastic\Loggastic\Bridge\Elasticsearch\Index\ElasticsearchIndexFactoryInterface;
7
use Locastic\Loggastic\Message\PopulateCurrentDataTrackersMessage;
8
use Locastic\Loggastic\Metadata\LoggableContext\Factory\LoggableContextCollectionFactoryInterface;
9
use Symfony\Component\Console\Attribute\AsCommand;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Question\ChoiceQuestion;
14
use Symfony\Component\Console\Question\Question;
15
use Symfony\Component\Console\Style\SymfonyStyle;
16
use Symfony\Component\Messenger\MessageBusInterface;
17
18
/**
19
 * Recreate CurrentDataTracker index
20
 * Pull all loggable objects from DB and populate currentTracker data to elastic.
21
 */
22
#[AsCommand('locastic:activity-logs:populate-current-data-trackers')]
23
final class PopulateCurrentDataTrackersCommand extends Command
24
{
25
    public function __construct(private readonly ElasticsearchIndexFactoryInterface $elasticsearchIndexFactory, private readonly LoggableContextCollectionFactoryInterface $loggableContextCollectionFactory, private readonly ManagerRegistry $managerRegistry, private readonly MessageBusInterface $bus)
26
    {
27
        parent::__construct();
28
    }
29
30
    protected function execute(InputInterface $input, OutputInterface $output): int
31
    {
32
        // List all loggable classes
33
        $loggableContextCollection = iterator_to_array($this->loggableContextCollectionFactory->create()->getIterator());
34
35
        $loggableClasses = array_keys($loggableContextCollection);
36
        $loggableClasses[] = 'ALL';
37
38
        // aks for the class
39
        $helper = $this->getHelper('question');
40
        $question = new ChoiceQuestion(
41
            'Choose loggable class to repopulate current data:',
42
            $loggableClasses,
43
            0
44
        );
45
        $question->setErrorMessage('Loggable class %s is invalid.');
46
47
        $loggableClass = $helper->ask($input, $output, $question);
0 ignored issues
show
The method ask() does not exist on Symfony\Component\Console\Helper\HelperInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
        /** @scrutinizer ignore-call */ 
48
        $loggableClass = $helper->ask($input, $output, $question);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
49
        // ask for the limit
50
        $question = new Question(
51
            'Limit number of latest objects to be populated (leave empty for no limit):',
52
            0
53
        );
54
55
        $limit = $helper->ask($input, $output, $question);
56
57
        $io = new SymfonyStyle($input, $output);
58
59
        $io->title('Creating current data log trackers ...');
60
61
        if ('ALL' !== $loggableClass) {
62
            $loggableClasses = [$loggableClass];
63
        }
64
65
        foreach ($loggableClasses as $loggableClass) {
66
            if ('ALL' === $loggableClass) {
67
                continue;
68
            }
69
70
            try {
71
                $this->elasticsearchIndexFactory->recreateCurrentDataTrackerLogIndex($loggableClass);
72
            } catch (\Exception $e) {
73
                $io->error($e->getMessage());
74
75
                return Command::FAILURE;
76
            }
77
78
            // get repository for current loggable class and pull all data from DB
79
            $manager = $this->managerRegistry->getManagerForClass($loggableClass);
80
81
            // loggable class not mapped to the db, skip
82
            if (!$manager) {
83
                continue;
84
            }
85
86
            $io->title('Processing '.$loggableClass);
87
            $repository = $manager->getRepository($loggableClass);
88
89
            $batchSize = 250;
90
            $messagesCount = 0;
91
            $count = $repository->count([]);
92
93
            if (0 === $limit || (int) $limit > $count) {
94
                $limit = $count;
95
            }
96
97
            for ($offset = 0; $offset < $limit; $offset += $batchSize) {
98
                $this->bus->dispatch(new PopulateCurrentDataTrackersMessage($offset, $batchSize, $loggableClass, $loggableContextCollection[$loggableClass]));
99
                ++$messagesCount;
100
            }
101
102
            $io->success('Dispatched '.$messagesCount.' messages for populating '.$loggableClass.' data trackers');
103
        }
104
105
        return Command::SUCCESS;
106
    }
107
}
108