1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Locastic\Loggastic\DataProcessor; |
4
|
|
|
|
5
|
|
|
use Locastic\Loggastic\Bridge\Elasticsearch\Context\ElasticsearchContextFactoryInterface; |
6
|
|
|
use Locastic\Loggastic\Bridge\Elasticsearch\Context\Traits\ElasticNormalizationContextTrait; |
7
|
|
|
use Locastic\Loggastic\Bridge\Elasticsearch\ElasticsearchService; |
8
|
|
|
use Locastic\Loggastic\Factory\ActivityLogInputFactoryInterface; |
9
|
|
|
use Locastic\Loggastic\Factory\CurrentDataTrackerInputFactoryInterface; |
10
|
|
|
use Locastic\Loggastic\Message\CreateActivityLogMessageInterface; |
11
|
|
|
use Locastic\Loggastic\Message\DeleteActivityLogMessageInterface; |
12
|
|
|
use Locastic\Loggastic\Message\UpdateActivityLogMessageInterface; |
13
|
|
|
use Locastic\Loggastic\Metadata\LoggableContext\Factory\LoggableContextFactoryInterface; |
14
|
|
|
use Locastic\Loggastic\Model\Output\CurrentDataTrackerInterface; |
15
|
|
|
use Locastic\Loggastic\Util\ArraysComparer; |
16
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
17
|
|
|
|
18
|
|
|
final class ActivityLogProcessor implements ActivityLogProcessorInterface |
19
|
|
|
{ |
20
|
|
|
use ElasticNormalizationContextTrait; |
21
|
|
|
|
22
|
|
|
public function __construct( |
23
|
|
|
private readonly ElasticsearchContextFactoryInterface $elasticsearchContextFactory, |
24
|
|
|
private readonly NormalizerInterface $objectNormalizer, |
25
|
|
|
private readonly ElasticsearchService $elasticService, |
26
|
|
|
private readonly ActivityLogInputFactoryInterface $activityLogInputFactory, |
27
|
|
|
private readonly CurrentDataTrackerInputFactoryInterface $currentDataTrackerInputFactory, |
28
|
|
|
private readonly LoggableContextFactoryInterface $loggableContextFactory |
29
|
|
|
) { |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function processCreatedItem(CreateActivityLogMessageInterface $message): void |
33
|
|
|
{ |
34
|
|
|
$loggableContext = $this->loggableContextFactory->create($message->getClassName()); |
35
|
|
|
|
36
|
|
|
if (!$loggableContext) { |
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$normalizedItem = $this->objectNormalizer->normalize($message->getItem(), 'activityLog', $this->getNormalizationContext($loggableContext)); |
41
|
|
|
|
42
|
|
|
$elasticContext = $this->elasticsearchContextFactory->create($message->getClassName()); |
43
|
|
|
|
44
|
|
|
// create log to save full item data for later comparison |
45
|
|
|
$currentDataTracker = $this->currentDataTrackerInputFactory->create($message->getItem(), $normalizedItem); |
46
|
|
|
$this->elasticService->createItem($currentDataTracker, $elasticContext->getCurrentDataTrackerIndex(), ['current_data_tracker']); |
47
|
|
|
|
48
|
|
|
// create log for item creation |
49
|
|
|
$activityLog = $this->activityLogInputFactory->createFromActivityLogMessage($message); |
50
|
|
|
$this->elasticService->createItem($activityLog, $elasticContext->getActivityLogIndex(), ['activity_log']); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function processUpdatedItem(UpdateActivityLogMessageInterface $message, CurrentDataTrackerInterface $currentDataTracker): void |
54
|
|
|
{ |
55
|
|
|
$loggableContext = $this->loggableContextFactory->create($message->getClassName()); |
56
|
|
|
|
57
|
|
|
if (!$loggableContext) { |
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$updatedData = null !== $message->getUpdatedItem() ? |
62
|
|
|
$this->objectNormalizer->normalize($message->getUpdatedItem(), 'activityLog', $this->getNormalizationContext($loggableContext)) : |
63
|
|
|
$message->getNormalizedItem(); |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
// no loggable fields were updated |
67
|
|
|
if (empty($updatedData) && !$message->isCreateLogWithoutChanges()) { |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$changes = ArraysComparer::getCompared($updatedData, $currentDataTracker->getData()); |
72
|
|
|
|
73
|
|
|
if (!$changes && !$message->isCreateLogWithoutChanges()) { |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$elasticContext = $this->elasticsearchContextFactory->create($message->getClassName()); |
78
|
|
|
|
79
|
|
|
// create log |
80
|
|
|
$activityLog = $this->activityLogInputFactory->createFromActivityLogMessage($message, $changes); |
81
|
|
|
|
82
|
|
|
$this->elasticService->createItem($activityLog, $elasticContext->getActivityLogIndex(), ['activity_log']); |
83
|
|
|
|
84
|
|
|
//update full data log |
85
|
|
|
$currentDataTrackerInput = $this->currentDataTrackerInputFactory->createFromCurrentDataTracker($currentDataTracker); |
86
|
|
|
$currentDataTrackerInput->setData(json_encode($updatedData, JSON_THROW_ON_ERROR)); |
87
|
|
|
|
88
|
|
|
$this->elasticService->updateItem($currentDataTracker->getId(), $currentDataTrackerInput, $elasticContext->getCurrentDataTrackerIndex(), ['current_data_tracker']); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function processDeletedItem(DeleteActivityLogMessageInterface $message): void |
92
|
|
|
{ |
93
|
|
|
$loggableContext = $this->loggableContextFactory->create($message->getClassName()); |
94
|
|
|
|
95
|
|
|
if (!$loggableContext) { |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$activityLog = $this->activityLogInputFactory->createFromActivityLogMessage($message); |
100
|
|
|
|
101
|
|
|
$elasticContext = $this->elasticsearchContextFactory->create($message->getClassName()); |
102
|
|
|
$this->elasticService->createItem($activityLog, $elasticContext->getActivityLogIndex(), ['activity_log']); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|