diff --git a/app/modules/Events/Application/EventsBootloader.php b/app/modules/Events/Application/EventsBootloader.php index dad5e5e..0ac3400 100644 --- a/app/modules/Events/Application/EventsBootloader.php +++ b/app/modules/Events/Application/EventsBootloader.php @@ -6,6 +6,7 @@ use App\Application\Broadcasting\EventMapperRegistryInterface; use App\Application\Persistence\DriverEnum; +use Cycle\Database\DatabaseInterface; use Cycle\ORM\EntityManagerInterface; use Cycle\ORM\ORMInterface; use Cycle\ORM\Select; @@ -38,7 +39,12 @@ public function defineSingletons(): array EventRepository::class => static fn( ORMInterface $orm, EntityManagerInterface $manager, - ): EventRepository => new EventRepository($manager, new Select($orm, Event::class)), + DatabaseInterface $db + ): EventRepository => new EventRepository( + em: $manager, + db: $db, + select: new Select($orm, Event::class) + ), ]; } diff --git a/app/modules/Events/Domain/Event.php b/app/modules/Events/Domain/Event.php index a07e4f3..b80df75 100644 --- a/app/modules/Events/Domain/Event.php +++ b/app/modules/Events/Domain/Event.php @@ -13,23 +13,34 @@ use Modules\Projects\Domain\ValueObject\Key; #[Entity( + role: Event::ROLE, repository: EventRepositoryInterface::class, + table: Event::TABLE_NAME, )] #[Index(columns: ['type'])] #[Index(columns: ['project'])] class Event { + public const TABLE_NAME = 'events'; + public const ROLE = 'event'; + + public const UUID = 'uuid'; + public const TYPE = 'type'; + public const PAYLOAD = 'payload'; + public const TIMESTAMP = 'timestamp'; + public const PROJECT = 'project'; + /** @internal */ public function __construct( - #[Column(type: 'string(36)', primary: true, typecast: 'uuid')] + #[Column(type: 'string(36)', name: self::UUID, primary: true, typecast: 'uuid')] private Uuid $uuid, - #[Column(type: 'string(50)')] + #[Column(type: 'string(50)', name: self::TYPE)] private string $type, - #[Column(type: 'json', typecast: Json::class)] + #[Column(type: 'json', name: self::PAYLOAD, typecast: Json::class)] private Json $payload, - #[Column(type: 'string(25)', typecast: Timestamp::class)] + #[Column(type: 'string(25)', name: self::TIMESTAMP, typecast: Timestamp::class)] private Timestamp $timestamp, - #[Column(type: 'string', nullable: true, typecast: Key::class)] + #[Column(type: 'string', name: self::PROJECT, nullable: true, typecast: Key::class)] private ?Key $project = null, ) {} diff --git a/app/modules/Events/Integration/CycleOrm/EventRepository.php b/app/modules/Events/Integration/CycleOrm/EventRepository.php index fe4b367..4ab99e0 100644 --- a/app/modules/Events/Integration/CycleOrm/EventRepository.php +++ b/app/modules/Events/Integration/CycleOrm/EventRepository.php @@ -4,6 +4,7 @@ namespace Modules\Events\Integration\CycleOrm; +use Cycle\Database\DatabaseInterface; use Cycle\ORM\EntityManagerInterface; use Cycle\ORM\Select; use Cycle\ORM\Select\Repository; @@ -18,6 +19,7 @@ final class EventRepository extends Repository implements EventRepositoryInterfa { public function __construct( private readonly EntityManagerInterface $em, + private readonly DatabaseInterface $db, Select $select, ) { parent::__construct($select); @@ -39,36 +41,17 @@ public function store(Event $event): bool public function deleteAll(array $scope = []): void { - $events = $this->select() + $this->db + ->delete(Event::TABLE_NAME) ->where($this->buildScope($scope)) - ->fetchAll(); - - $batchSize = 100; - - $batch = 0; - foreach ($events as $event) { - $this->em->delete($event); - - if (++$batch % $batchSize === 0) { - $this->em->run(); - $batch = 0; - } - } - - $this->em->run(); + ->run(); } public function deleteByPK(string $uuid): bool { - $event = $this->findByPK($uuid); - - if ($event === null) { - return false; - } - - $this->em->delete($event)->run(); - - return true; + return $this->db->delete(Event::TABLE_NAME) + ->where(Event::UUID, $uuid) + ->run() > 0; } public function countAll(array $scope = []): int diff --git a/app/modules/Events/Interfaces/Commands/ClearEventsHandler.php b/app/modules/Events/Interfaces/Commands/ClearEventsHandler.php index b9859a2..9442717 100644 --- a/app/modules/Events/Interfaces/Commands/ClearEventsHandler.php +++ b/app/modules/Events/Interfaces/Commands/ClearEventsHandler.php @@ -7,6 +7,8 @@ use App\Application\Commands\ClearEvents; use Modules\Events\Domain\EventRepositoryInterface; use Modules\Events\Domain\Events\EventsWasClear; +use Modules\Projects\Domain\Project; +use Modules\Projects\Domain\ValueObject\Key; use Psr\EventDispatcher\EventDispatcherInterface; use Spiral\Cqrs\Attribute\CommandHandler; @@ -20,7 +22,7 @@ public function __construct( #[CommandHandler] public function __invoke(ClearEvents $command): void { - $scope = ['project' => $command->project]; + $scope = ['project' => $command->project ?? (string) Key::create(Project::DEFAULT_KEY)]; if ($command->type) { $scope['type'] = $command->type; diff --git a/app/modules/Events/Interfaces/Commands/DeleteEventHandler.php b/app/modules/Events/Interfaces/Commands/DeleteEventHandler.php index 71aec81..f24eafb 100644 --- a/app/modules/Events/Interfaces/Commands/DeleteEventHandler.php +++ b/app/modules/Events/Interfaces/Commands/DeleteEventHandler.php @@ -29,7 +29,7 @@ public function __invoke(DeleteEvent $command): void $this->dispatcher->dispatch( new EventWasDeleted( uuid: $command->uuid, - project: $event->getProject()?->value, + project: $event->getProject()?->__toString(), ), ); }