Skip to content

Commit

Permalink
Allow timezone to be configured in package configuration (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
MylesKingsnorth authored and DamienHarper committed Aug 19, 2019
1 parent 4846d24 commit 6d951ab
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@ dh_doctrine_audit:
table_suffix: '_audit'
```

### Timezone

You can configure the timezone the audit `created_at` is generated in. This by default is 'UTC'.

```yaml
// app/config/config.yml (symfony < 3.4)
// config/packages/dh_doctrine_audit.yaml (symfony >= 3.4)
dh_doctrine_audit:
timezone: 'Europe/London'
```

### Creating audit tables

Open a command console, enter your project directory and execute the
Expand Down
16 changes: 16 additions & 0 deletions src/DoctrineAuditBundle/AuditConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class AuditConfiguration
*/
private $tableSuffix;

/**
* @var string
*/
private $timezone;

/**
* @var array
*/
Expand Down Expand Up @@ -57,6 +62,7 @@ public function __construct(array $config, UserProviderInterface $userProvider,

$this->tablePrefix = $config['table_prefix'];
$this->tableSuffix = $config['table_suffix'];
$this->timezone = $config['timezone'];
$this->ignoredColumns = $config['ignored_columns'];

if (isset($config['entities']) && !empty($config['entities'])) {
Expand Down Expand Up @@ -226,6 +232,16 @@ public function getTableSuffix(): string
return $this->tableSuffix;
}

/**
* Get the value of timezone
*
* @return string
*/
public function getTimezone(): string
{
return $this->timezone;
}

/**
* Get the value of excludedColumns.
*
Expand Down
2 changes: 1 addition & 1 deletion src/DoctrineAuditBundle/AuditManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ private function audit(EntityManager $em, array $data): void

$statement = $em->getConnection()->prepare($query);

$dt = new \DateTime('now', new \DateTimeZone('UTC'));
$dt = new \DateTime('now', new \DateTimeZone($this->getConfiguration()->getTimezone()));
$statement->bindValue('type', $data['action']);
$statement->bindValue('object_id', (string) $data['id']);
$statement->bindValue('diffs', json_encode($data['diff']));
Expand Down
4 changes: 4 additions & 0 deletions src/DoctrineAuditBundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public function getConfigTreeBuilder()
->prototype('scalar')->end()
->end()

->scalarNode('timezone')
->defaultValue('UTC')
->end()

->arrayNode('entities')
->canBeUnset()
->prototype('array')
Expand Down
17 changes: 17 additions & 0 deletions tests/DoctrineAuditBundle/AuditConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,22 @@ public function testDisableAuditFor(): void
$this->assertFalse($configuration->isAudited(Post::class), 'entity "'.Post::class.'" is not audited.');
}

public function testDefaultTimezone(): void
{
$configuration = $this->getAuditConfiguration();

$this->assertSame('UTC', $configuration->getTimezone(), 'timezone is UTC by default.');
}

public function testCustomTimezone(): void
{
$configuration = $this->getAuditConfiguration([
'timezone' => 'Europe/London',
]);

$this->assertSame('Europe/London', $configuration->getTimezone(), 'custom timezone is "Europe/London".');
}

protected function getAuditConfiguration(array $options = []): AuditConfiguration
{
$container = new ContainerBuilder();
Expand All @@ -371,6 +387,7 @@ protected function getAuditConfiguration(array $options = []): AuditConfiguratio
array_merge([
'table_prefix' => '',
'table_suffix' => '_audit',
'timezone' => 'UTC',
'ignored_columns' => [],
'entities' => [],
'enabled' => true,
Expand Down
1 change: 1 addition & 0 deletions tests/DoctrineAuditBundle/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ protected function createAuditConfiguration(array $options = []): AuditConfigura
array_merge([
'table_prefix' => '',
'table_suffix' => '_audit',
'timezone' => 'UTC',
'ignored_columns' => [],
'entities' => [],
], $options),
Expand Down
1 change: 1 addition & 0 deletions tests/DoctrineAuditBundle/CoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ protected function createAuditConfiguration(array $options = []): AuditConfigura
array_merge([
'table_prefix' => '',
'table_suffix' => '_audit',
'timezone' => 'UTC',
'ignored_columns' => [],
'entities' => [],
], $options),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function testItSetsDefaultParameters(): void
$this->assertContainerBuilderHasParameter('dh_doctrine_audit.configuration', [
'table_prefix' => '',
'table_suffix' => '_audit',
'timezone' => 'UTC',
'ignored_columns' => [],
'entities' => [],
]);
Expand Down
4 changes: 4 additions & 0 deletions tests/DoctrineAuditBundle/Helper/AuditHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ public function testDiffHonorsGloballyIgnoredColumns(): void
[
'table_prefix' => '',
'table_suffix' => '_audit',
'timezone' => 'UTC',
'ignored_columns' => [
'created_at',
'updated_at',
Expand Down Expand Up @@ -258,6 +259,7 @@ public function testDiffHonorsLocallyIgnoredColumns(): void
[
'table_prefix' => '',
'table_suffix' => '_audit',
'timezone' => 'UTC',
'ignored_columns' => [],
'entities' => [
Post::class => [
Expand Down Expand Up @@ -378,6 +380,7 @@ public function testBlameWhenNoRequest(): void
[
'table_prefix' => '',
'table_suffix' => '_audit',
'timezone' => 'UTC',
'ignored_columns' => [],
'entities' => [],
],
Expand All @@ -404,6 +407,7 @@ public function testBlameWhenNoUser(): void
[
'table_prefix' => '',
'table_suffix' => '_audit',
'timezone' => 'UTC',
'ignored_columns' => [],
'entities' => [],
],
Expand Down

0 comments on commit 6d951ab

Please sign in to comment.