Skip to content

Commit

Permalink
[Timeline] inject registered_at date for event items
Browse files Browse the repository at this point in the history
  • Loading branch information
ottaviano committed May 1, 2024
1 parent e1f96e0 commit cfaec40
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/JeMengage/Timeline/FeedProcessor/EventProcessor.php
Expand Up @@ -7,21 +7,28 @@
use App\Event\EventCleaner;
use App\Event\EventVisibilityEnum;
use App\JeMengage\Timeline\TimelineFeedTypeEnum;
use App\Repository\EventRegistrationRepository;
use Symfony\Component\Security\Core\Security;

class EventProcessor extends AbstractFeedProcessor
{
private const CONTEXT_CLEANER_ENABLED_KEY = 'event:cleaner_enabled';

private ?Adherent $currentUser = null;

public function __construct(
private readonly EventCleaner $eventCleaner,
private readonly Security $security,
private readonly EventRegistrationRepository $eventRegistrationRepository,
) {
}

public function process(array $item, array &$context): array
{
return $this->cleanEventDataIfNeed($item, $context);
$item = $this->cleanEventDataIfNeed($item, $context);
$item = $this->appendEventRegistrationDate($item);

return $item;
}

public function supports(array $item): bool
Expand All @@ -34,12 +41,11 @@ private function cleanEventDataIfNeed(array $item, array &$context): array
$needClean = $context[self::CONTEXT_CLEANER_ENABLED_KEY] ?? null;

if (null === $needClean) {
$user = $this->security->getUser();
$user = $this->getCurrentUser();
$needClean = $context[self::CONTEXT_CLEANER_ENABLED_KEY] =
EventVisibilityEnum::isForAdherent($visibility = $item['visibility'] ?? EventVisibilityEnum::ADHERENT_DUES->value)
&& (
!$user instanceof Adherent
|| (EventVisibilityEnum::ADHERENT->value === $visibility && !$user->hasTag(TagEnum::ADHERENT))
(EventVisibilityEnum::ADHERENT->value === $visibility && !$user->hasTag(TagEnum::ADHERENT))
|| (EventVisibilityEnum::ADHERENT_DUES->value === $visibility && !$user->hasTag(TagEnum::getAdherentYearTag()))
);
}
Expand All @@ -53,4 +59,23 @@ private function cleanEventDataIfNeed(array $item, array &$context): array

return $item;
}

private function getCurrentUser(): Adherent
{
if ($this->currentUser) {
return $this->currentUser;
}

return $this->currentUser = $this->security->getUser();
}

private function appendEventRegistrationDate(array $item): array
{
$item['user_registered_at'] = $this->eventRegistrationRepository->findAdherentRegistration(
$item['objectID'],
$this->getCurrentUser()->getUuidAsString()
)?->getCreatedAt();

return $item;
}
}

0 comments on commit cfaec40

Please sign in to comment.