Skip to content

Commit

Permalink
Merge pull request #47 from smartbooster/improve_history_logger
Browse files Browse the repository at this point in the history
Improve HistoryLogger
  • Loading branch information
mathieu-ducrot authored Mar 2, 2023
2 parents b5ea50a + d5acf22 commit 4aa3741
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
CHANGELOG for 1.x
===================
## v1.4.4 - (2023-03-01)

### Added

- Add new logic check when use HistoryLogger method
- Saving only different value on save diff in HistoryLogger
- Add overriding possibility of display value in diff show of history

## v1.4.3 - (2023-01-27)

### Fixed
Expand Down
55 changes: 48 additions & 7 deletions src/Logger/HistoryLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public function __construct(EntityManagerInterface $entityManager)
}

/**
* Set entity data before processing for comparison during update history log
* Set entity data before processing for comparison during update history log.
* Define the checked differences of the entity
*/
public function init(array $entityData): void
{
Expand All @@ -42,10 +43,19 @@ public function init(array $entityData): void

/**
* Call this method with the data after update processing and use the return bool to condition the log call
* @param array $entityData array of data to compare. Additional key are removed.
*/
public function hasDiff(array $entityData): bool
{
$this->afterHandleEntityData = $entityData;
if (empty($this->beforeHandleEntityData)) {
throw new \LogicException('method init must be call and not empty before hasDiff');
}

$this->afterHandleEntityData = array_intersect_key($entityData, $this->beforeHandleEntityData);

if (empty($this->afterHandleEntityData)) {
throw new \LogicException('$entityData has no comparable data with init');
}

return $this->beforeHandleEntityData !== $this->afterHandleEntityData;
}
Expand All @@ -55,16 +65,37 @@ public function hasDiff(array $entityData): bool
*/
public function log(HistorizableInterface $entity, string $code, array $data = []): void
{
$isUpdate = $code === self::ENTITY_UPDATED_CODE;

if ($isUpdate && empty($this->beforeHandleEntityData)) {
throw new \LogicException('method init must be call and not empty before log on code ' . self::ENTITY_UPDATED_CODE);
}

if ($isUpdate && empty($this->afterHandleEntityData)) {
throw new \LogicException('method hasDiff must be call and not empty before log on code ' . self::ENTITY_UPDATED_CODE);
}

$history = array_merge([
'code' => $code,
'date' => time(),
], $data);

if ($history['code'] === self::ENTITY_UPDATED_CODE && !empty($this->afterHandleEntityData)) {
$history['history_updated_diff'] = [
'before' => $this->beforeHandleEntityData,
'after' => $this->afterHandleEntityData,
if ($isUpdate) {
$diffs = [
'before' => [],
'after' => [],
];

foreach ($this->afterHandleEntityData as $key => $value) {
if ($this->beforeHandleEntityData[$key] === $this->afterHandleEntityData[$key]) {
continue;
}

$diffs['before'][$key] = $this->beforeHandleEntityData[$key];
$diffs['after'][$key] = $this->afterHandleEntityData[$key];
}

$history['history_updated_diff'] = $diffs;
}

$entity->addHistory($history);
Expand All @@ -81,8 +112,18 @@ public function log(HistorizableInterface $entity, string $code, array $data = [
*/
public function logDiff(HistorizableInterface $entity, array $initialData, array $data = []): void
{
if (empty($initialData)) {
throw new \InvalidArgumentException('initialData must not be empty');
}

$dataForHistoryDiff = $entity->getDataForHistoryDiff();

if (empty($dataForHistoryDiff)) {
throw new \LogicException('entity->getDataForHistoryDiff() must not be empty');
}

$this->init($initialData);
if ($this->hasDiff(array_intersect_key($entity->getDataForHistoryDiff(), $initialData))) {
if ($this->hasDiff($dataForHistoryDiff)) {
$this->log($entity, self::ENTITY_UPDATED_CODE, $data);
}
}
Expand Down
14 changes: 11 additions & 3 deletions templates/admin/base_field/timeline_history_field.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,19 @@
{% endif %}
{% if row.history_updated_diff is defined %}
<ul>
{% for field, value in row.history_updated_diff.after %}
{% for field, after_value in row.history_updated_diff.after %}
<li>
<b>{{ ('field.label_' ~ field) |trans }}</b>
<span class="text-muted">{{ row.history_updated_diff.before[field] ?? null }}</span> >
<span>{{ value }}</span>
{% set value = row.history_updated_diff.before[field] ?? null %}
<span class="text-muted">
{% block render_value %}
{{ value }}
{% endblock %}
</span> >
{% set value = after_value %}
<span>
{{ block('render_value') }}
</span>
</li>
{% endfor %}
</ul>
Expand Down

0 comments on commit 4aa3741

Please sign in to comment.