feat: listen to user life cycle events
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXM\ProviderLocalChrono\Listeners;
|
||||
|
||||
use KTXC\User\Event\UserCreatedEvent;
|
||||
use KTXC\User\Event\UserDeletingEvent;
|
||||
use KTXF\Resource\Identifier\CollectionIdentifier;
|
||||
use KTXM\ProviderLocalChrono\Providers\Personal\PersonalService;
|
||||
use KTXM\ProviderLocalChrono\Store\Personal\Store;
|
||||
|
||||
final class UserEventListener
|
||||
{
|
||||
private const PERSONAL_CALENDAR_LABEL = 'Personal';
|
||||
|
||||
public function __construct(
|
||||
private readonly Store $store,
|
||||
private readonly PersonalService $service,
|
||||
) {
|
||||
}
|
||||
|
||||
public function onUserCreated(UserCreatedEvent $event): void
|
||||
{
|
||||
$tenantId = $event->tenantIdentifier();
|
||||
$userId = $event->userIdentifier();
|
||||
|
||||
// A replayed lifecycle event must not create another default collection.
|
||||
if ($this->store->collectionList($tenantId, $userId) !== []) {
|
||||
return;
|
||||
}
|
||||
|
||||
$collection = $this->store->collectionFresh()->setEnabled(true);
|
||||
$collection->getProperties()
|
||||
->setLabel(self::PERSONAL_CALENDAR_LABEL)
|
||||
->setVisibility(true);
|
||||
$this->store->collectionCreate($tenantId, $userId, $collection);
|
||||
}
|
||||
|
||||
public function onUserDeleting(UserDeletingEvent $event): void
|
||||
{
|
||||
$service = $this->service->initialize($event->tenantIdentifier(), $event->userIdentifier());
|
||||
|
||||
foreach ($service->collectionList(null) as $collection) {
|
||||
$identifier = $collection->identifier();
|
||||
if ($identifier === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$service->collectionDelete(new CollectionIdentifier(
|
||||
$collection->provider(),
|
||||
(string) $collection->service(),
|
||||
(string) $identifier,
|
||||
), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,15 +3,21 @@
|
||||
namespace KTXM\ProviderLocalChrono;
|
||||
|
||||
use KTXC\Resource\ProviderManager;
|
||||
use KTXC\User\Event\UserCreatedEvent;
|
||||
use KTXC\User\Event\UserDeletingEvent;
|
||||
use KTXF\Event\DeliveryMode;
|
||||
use KTXF\Event\EventListenerRegistrarInterface;
|
||||
use KTXF\Module\ModuleBrowserInterface;
|
||||
use KTXF\Module\ModuleInstanceAbstract;
|
||||
use KTXF\Resource\Provider\ProviderInterface;
|
||||
use KTXM\ProviderLocalChrono\Listeners\UserEventListener;
|
||||
use KTXM\ProviderLocalChrono\Providers\Provider;
|
||||
|
||||
class Module extends ModuleInstanceAbstract implements ModuleBrowserInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ProviderManager $providerManager,
|
||||
private readonly EventListenerRegistrarInterface $events,
|
||||
) {}
|
||||
|
||||
public function handle(): string
|
||||
@@ -52,6 +58,10 @@ class Module extends ModuleInstanceAbstract implements ModuleBrowserInterface
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
// Register listeners
|
||||
$this->events->listen($this->handle(), UserCreatedEvent::class, UserEventListener::class, 'onUserCreated', DeliveryMode::Deferred);
|
||||
$this->events->listen($this->handle(), UserDeletingEvent::class, UserEventListener::class, 'onUserDeleting', DeliveryMode::Deferred);
|
||||
// Register providers
|
||||
$this->providerManager->register(ProviderInterface::TYPE_CHRONO, 'default', Provider::class);
|
||||
}
|
||||
|
||||
|
||||
@@ -282,7 +282,11 @@ class PersonalService implements ServiceBaseInterface, ServiceCollectionMutableI
|
||||
throw new InvalidParameterException("Invalid: Collection identifier '$id' does not exist or does not belong to user '{$this->serviceUserId}'");
|
||||
}
|
||||
// destroy collection in store
|
||||
$this->store->collectionDestroyById($this->serviceTenantId, $this->serviceUserId, $id);
|
||||
$collection = $this->store->collectionFetch($this->serviceTenantId, $this->serviceUserId, $id);
|
||||
if ($collection === null) {
|
||||
throw new InvalidParameterException("Invalid: Collection identifier '$id' could not be retrieved");
|
||||
}
|
||||
$this->store->collectionDestroy($this->serviceTenantId, $this->serviceUserId, $collection);
|
||||
unset($this->serviceCollectionCache[$id]);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -308,7 +308,12 @@ class Store {
|
||||
if ($identifier === null) {
|
||||
return $entity;
|
||||
}
|
||||
return $this->collectionDestroyById($tenantId, $userId, (string) $identifier) ? $entity : $entity;
|
||||
|
||||
$this->entityDestroyByCollectionId($tenantId, $userId, [(string) $identifier]);
|
||||
$this->chronicleExpungeByCollectionId($tenantId, [(string) $identifier]);
|
||||
$this->collectionDestroyById($tenantId, $userId, (string) $identifier);
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -592,6 +597,21 @@ class Store {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete entities belonging to specific collections from the data store.
|
||||
*
|
||||
* @param string $tenantId tenant identifier
|
||||
* @param string $userId user identifier
|
||||
* @param array $identifiers collection identifiers
|
||||
*/
|
||||
private function entityDestroyByCollectionId(string $tenantId, string $userId, array $identifiers): void {
|
||||
$this->_store->selectCollection($this->_EntityTable)->deleteMany([
|
||||
'tid' => $tenantId,
|
||||
'uid' => $userId,
|
||||
'cid' => ['$in' => $identifiers],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* chronicle a operation to an entity to the data store
|
||||
*
|
||||
@@ -758,11 +778,13 @@ class Store {
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @param string $tenantId tenant identifier
|
||||
* @param array $identifiers collection of identifiers
|
||||
*/
|
||||
private function chronicleExpungeByCollectionId(array $identifiers): void {
|
||||
private function chronicleExpungeByCollectionId(string $tenantId, array $identifiers): void {
|
||||
// Delete chronicle entries for the specified collection identifiers
|
||||
$this->_store->selectCollection($this->_ChronicleTable)->deleteMany([
|
||||
'tid' => $tenantId,
|
||||
'cid' => ['$in' => $identifiers]
|
||||
]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user