Skip to content

Commit

Permalink
feat: add clock component
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristoforo Cervino committed Oct 12, 2023
1 parent c6aa449 commit bc466ca
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
}
],
"require": {
"php": "^8.0",
"php": "^8.1",
"symfony/framework-bundle": "^5.0 | ^6.0",
"doctrine/common": "^2.13 || ^3.0",
"doctrine/event-manager": "^1.2 | ^2.0"
"doctrine/event-manager": "^1.2 | ^2.0",
"symfony/clock": "^6.3"
},
"require-dev": {
"ext-json": "*",
Expand Down
28 changes: 27 additions & 1 deletion src/DependencyInjection/Compiler/DoctrineEventSubscriberPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,48 @@
namespace Andante\TimestampableBundle\DependencyInjection\Compiler;

use Andante\TimestampableBundle\EventSubscriber\TimestampableEventSubscriber;
use Composer\InstalledVersions;
use Doctrine\ORM\Events;
use Psr\Clock\ClockInterface;
use Symfony\Component\Clock\Clock;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class DoctrineEventSubscriberPass implements CompilerPassInterface
{
public const TIMESTAMPABLE_SUBSCRIBER_SERVICE_ID = 'andante_timestampable.doctrine.timestampable_subscriber';
public const CLOCK_SERVICE_ID = 'andante_timestampable.clock';

public function process(ContainerBuilder $container): void
{
$container
$subscriberDefinition = $container
->register(
self::TIMESTAMPABLE_SUBSCRIBER_SERVICE_ID,
TimestampableEventSubscriber::class
)
->addArgument(new Reference('andante_timestampable.configuration'))
->addTag('doctrine.event_subscriber');
$symfonyDoctrineBridgeVersion = InstalledVersions::getVersion('symfony/doctrine-bridge');
if (null !== $symfonyDoctrineBridgeVersion && \version_compare($symfonyDoctrineBridgeVersion, '6.3', '>=')) {
$subscriberDefinition->addTag('doctrine.event_listener', [
'event' => Events::prePersist,
]);
$subscriberDefinition->addTag('doctrine.event_listener', [
'event' => Events::preUpdate,
]);
$subscriberDefinition->addTag('doctrine.event_listener', [
'event' => Events::loadClassMetadata,
]);
} else {
$subscriberDefinition->addTag('doctrine.event_subscriber');
}

if ($container->has(ClockInterface::class)) {
$subscriberDefinition->addArgument(new Reference(ClockInterface::class));
} else {
$container->register(self::CLOCK_SERVICE_ID, Clock::class);
$subscriberDefinition->addArgument(new Reference(self::CLOCK_SERVICE_ID));
}
}
}
13 changes: 9 additions & 4 deletions src/EventSubscriber/TimestampableEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Psr\Clock\ClockInterface;

class TimestampableEventSubscriber implements EventSubscriber
{
private Configuration $configuration;
private ClockInterface $clock;

public function __construct(Configuration $configuration)
{
public function __construct(
Configuration $configuration,
ClockInterface $clock
) {
$this->configuration = $configuration;
$this->clock = $clock;
}

public function getSubscribedEvents(): array
Expand All @@ -35,15 +40,15 @@ public function prePersist(LifecycleEventArgs $onFlushEventArgs): void
{
$entity = $onFlushEventArgs->getObject();
if ($entity instanceof CreatedAtTimestampableInterface && null === $entity->getCreatedAt()) {
$entity->setCreatedAt(new \DateTimeImmutable());
$entity->setCreatedAt($this->clock->now());
}
}

public function preUpdate(LifecycleEventArgs $onFlushEventArgs): void
{
$entity = $onFlushEventArgs->getObject();
if ($entity instanceof UpdatedAtTimestampableInterface) {
$entity->setUpdatedAt(new \DateTimeImmutable());
$entity->setUpdatedAt($this->clock->now());
}
}

Expand Down

0 comments on commit bc466ca

Please sign in to comment.