Skip to content

Commit

Permalink
Merge pull request #37 from smartbooster/history_diff_fields_to_skip
Browse files Browse the repository at this point in the history
Release v1.10 with History diff fields to skip management
  • Loading branch information
mathieu-ducrot authored Aug 28, 2024
2 parents cf5f118 + e1ba8be commit abb9925
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 14 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
CHANGELOG for 1.x
===================
## v1.10.0 - (2024-08-28)
### Added
- `ArrayUtils::hasDuplicateValue` + tests (@lfortunier)
- `StringUtils::fillPrefix` Fill a prefix to value until specified length + tests (@lfortunier)
- `DateUtils::addWorkingDays` to calculate a date based on working days + tests (@lfortunier)
- `HistorizableInterface::getHistoryDiffFieldsToSkip` to manage fields to skip logging on history (@mathieu-ducrot)

### Changed
- `README.md` update : add scripts to the list of scripts that can be executed in `script-src` Nelmio Security recommendations config. (@lfortunier)
- `README.md` update : Add missing sentry settings configuration (@lfortunier)
- `RequestUtils::getContextFromHost` Add .sso subdomain for proper detection on localhost (@mathieu-ducrot)
- `HistoryLogger::log` skip history log if it's an update without data (ex: diff detected in EntityChangeSet but the targetted fields are actually skip from getHistoryDiffFieldsToSkip @mathieu-ducrot)

## v1.9.0 - (2024-07-11)
### Added
- `FileableInterface`, `ImageableInterface`, `PdfInterface` and their trait to demonstrate how to properly configure VichUploader annotations/attributes
Expand Down
3 changes: 0 additions & 3 deletions CHANGELOG_add_duplicate_value_fill_prefix.md

This file was deleted.

3 changes: 0 additions & 3 deletions CHANGELOG_add_nelmio_config.md

This file was deleted.

2 changes: 0 additions & 2 deletions CHANGELOG_add_open_days.md

This file was deleted.

9 changes: 9 additions & 0 deletions src/Entity/Log/HistorizableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ public function addHistory(array $history): self;
* Allows you to activate or not the history on prePersist/preUpdate doctrine events for diffs
*/
public function isDoctrineListenerEnable(): bool;

/**
* @return array Indexed array which contains field to skip storing on the history JSON by the HistoryDoctrineListener when doing update
* Example :
* [
* "progressionData" => true,
* ]
*/
public function getHistoryDiffFieldsToSkip(): array;
}
5 changes: 5 additions & 0 deletions src/Entity/Log/HistorizableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ public function isDoctrineListenerEnable(): bool
{
return true;
}

public function getHistoryDiffFieldsToSkip(): array
{
return [];
}
}
9 changes: 8 additions & 1 deletion src/EventListener/HistoryDoctrineListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,15 @@ private function handleHistory(LifecycleEventArgs $args, string $code): void

$statusDiff = null;
$isHistorizableStatus = $entity instanceof HistorizableStatusInterface;
$diffFieldsToSkip = $entity->getHistoryDiffFieldsToSkip();
foreach ($entityData as $field => $change) {
if ($field === 'history' || $field === 'updatedAt' || $field === 'updatedAtMonth' || $field === 'updatedAtYear') {
if (
$field === 'history'
|| $field === 'updatedAt'
|| $field === 'updatedAtMonth'
|| $field === 'updatedAtYear'
|| isset($diffFieldsToSkip[$field])
) {
unset($entityData[$field]);
continue;
}
Expand Down
10 changes: 10 additions & 0 deletions src/Logger/HistoryLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ public function log(HistorizableInterface $entity, ?string $code = null, array $
}
}

// If the history log is an update but without data we don't log it to the database.
if (
$code === self::UPDATED_CODE
&& !isset($history[self::DIFF_PROPERTY])
&& $this->title === null
&& $this->comment === null
&& $this->description === null
) {
return;
}
$entity->addHistory($history);

if ($this->flushLog) {
Expand Down
11 changes: 6 additions & 5 deletions src/Utils/RequestUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ public static function getContextFromHost(string $host, ?string $domain = null):
// MDT fallback in case there is no subdomain and the host is different from the domain
$toReturn = 'app';
if (
str_starts_with($host, 'app.') ||
str_starts_with($host, 'admin.') ||
str_starts_with($host, 'api.') ||
str_starts_with($host, 'extranet.') ||
substr_count($host, '.') > 1
str_starts_with($host, 'app.')
|| str_starts_with($host, 'admin.')
|| str_starts_with($host, 'api.')
|| str_starts_with($host, 'extranet.')
|| str_starts_with($host, 'sso.')
|| substr_count($host, '.') > 1
) {
$toReturn = substr($host, 0, (int) strpos($host, '.'));
}
Expand Down

0 comments on commit abb9925

Please sign in to comment.