1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Locastic\Loggastic\MessageHandler; |
4
|
|
|
|
5
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
6
|
|
|
use Locastic\Loggastic\Bridge\Elasticsearch\Context\ElasticsearchContextFactoryInterface; |
7
|
|
|
use Locastic\Loggastic\Bridge\Elasticsearch\Context\Traits\ElasticNormalizationContextTrait; |
8
|
|
|
use Locastic\Loggastic\Bridge\Elasticsearch\ElasticsearchService; |
9
|
|
|
use Locastic\Loggastic\Factory\CurrentDataTrackerInputFactoryInterface; |
10
|
|
|
use Locastic\Loggastic\Message\PopulateCurrentDataTrackersMessage; |
11
|
|
|
use Symfony\Component\Messenger\Attribute\AsMessageHandler; |
12
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
13
|
|
|
|
14
|
|
|
#[AsMessageHandler] |
15
|
|
|
final class PopulateCurrentDataTrackersHandler |
16
|
|
|
{ |
17
|
|
|
use ElasticNormalizationContextTrait; |
18
|
|
|
|
19
|
|
|
public function __construct(private readonly ManagerRegistry $managerRegistry, private readonly CurrentDataTrackerInputFactoryInterface $currentDataTrackerInputFactory, private readonly NormalizerInterface $objectNormalizer, private readonly ElasticsearchService $elasticService, private readonly ElasticsearchContextFactoryInterface $elasticsearchContextFactory) |
20
|
|
|
{ |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function __invoke(PopulateCurrentDataTrackersMessage $message): void |
24
|
|
|
{ |
25
|
|
|
$loggableContext = $message->getLoggableContext(); |
26
|
|
|
|
27
|
|
|
$manager = $this->managerRegistry->getManagerForClass($message->getLoggableClass()); |
28
|
|
|
$repository = $manager->getRepository($message->getLoggableClass()); |
29
|
|
|
|
30
|
|
|
$args = []; |
31
|
|
|
if (method_exists($message->getLoggableClass(), 'getCreatedAt')) { //todo move to config or command args |
32
|
|
|
$args = ['createdAt' => 'DESC']; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
//todo move order to config or command |
36
|
|
|
$data = $repository->findBy([], $args, $message->getBatchSize(), $message->getOffset()); |
37
|
|
|
|
38
|
|
|
echo "\r\n"; |
39
|
|
|
echo 'Creating '.$message->getBatchSize().' current data trackers for '.$message->getLoggableClass().' ...'."\r\n"; |
40
|
|
|
echo "\r\n"; |
41
|
|
|
|
42
|
|
|
$currentDataTrackers = []; |
43
|
|
|
foreach ($data as $item) { |
44
|
|
|
echo 'Processing object '.$item->getId()."\r\n"; |
45
|
|
|
|
46
|
|
|
$normalizedItem = $this->objectNormalizer->normalize($item, 'activityLog', $this->getNormalizationContext($loggableContext)); |
47
|
|
|
$currentDataTrackers[] = $this->currentDataTrackerInputFactory->create($item, $normalizedItem); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$elasticContext = $this->elasticsearchContextFactory->create($message->getLoggableClass()); |
51
|
|
|
$this->elasticService->bulkCreate($currentDataTrackers, $elasticContext->getCurrentDataTrackerIndex(), ['current_data_tracker']); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|