Skip to content

Commit

Permalink
Debug QuitTransport
Browse files Browse the repository at this point in the history
  • Loading branch information
Korbeil committed May 17, 2024
1 parent f68e83d commit 40f2dc3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/src/Discord/Command/Helper/UserHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Afup\Hermes\Discord\Command\Helper;

use Afup\Hermes\Entity\User;
use Afup\Hermes\Repository\User\FindOrCreateUserDebug;
use Discord\Parts\Interactions\Interaction;
use Discord\Parts\User\User as DiscordUser;

Expand All @@ -16,6 +17,10 @@ public function getUserForInteraction(Interaction $interaction): User
$discordUser = $interaction->user;
$userId = (int) $discordUser->id;

if ($this->findOrCreateUser instanceof FindOrCreateUserDebug) {
return ($this->findOrCreateUser)($userId, $interaction);
}

return ($this->findOrCreateUser)($userId);
}
}
8 changes: 7 additions & 1 deletion app/src/Discord/Command/QuitTransportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Afup\Hermes\Repository\Event\FindEventByChannel;
use Afup\Hermes\Repository\Traveler\GetTravelerListForUserAndEvent;
use Afup\Hermes\Repository\User\FindOrCreateUser;
use Afup\Hermes\Repository\User\FindOrCreateUserDebug;
use Discord\Builders\CommandBuilder;
use Discord\Builders\Components\ActionRow;
use Discord\Builders\Components\Button;
Expand All @@ -34,7 +35,7 @@

public function __construct(
private TranslatorInterface $translator,
private FindOrCreateUser $findOrCreateUser,
private FindOrCreateUserDebug $findOrCreateUser,
private FindEventByChannel $findEventByChannel,
private GetTravelerListForUserAndEvent $getTravelerListForUserAndEvent,
private EntityManagerInterface $entityManager,
Expand All @@ -59,15 +60,18 @@ public function callback(Discord $discord): void
return;
}

$interaction->respondWithMessage(MessageBuilder::new()->setContent('A'));
$user = $this->getUserForInteraction($interaction);

$interaction->respondWithMessage(MessageBuilder::new()->setContent('B'));
$travelers = ($this->getTravelerListForUserAndEvent)($user, $event, TravelerType::PASSENGER);
if (0 === \count($travelers)) {
$interaction->respondWithMessage(MessageBuilder::new()->setContent($this->translator->trans('discord.quit_transport.error.no_transport')), true);

return;
}

$interaction->respondWithMessage(MessageBuilder::new()->setContent('C'));
if (1 === \count($travelers)) {
$this->validateRemoval($discord, $interaction, $travelers[0]);
} else {
Expand All @@ -89,13 +93,15 @@ public function callback(Discord $discord): void
$message->addComponent($chooseAction);
}

$interaction->respondWithMessage(MessageBuilder::new()->setContent('D'));
$interaction->respondWithMessage($message, true);
}
});
}

private function validateRemoval(Discord $discord, Interaction $interaction, Traveler $traveler): void
{
$interaction->respondWithMessage(MessageBuilder::new()->setContent('E'));
$embed = new Embed($discord);
$embed->setTitle($this->translator->trans('discord.quit_transport.confirmation'));

Expand Down
36 changes: 36 additions & 0 deletions app/src/Repository/User/FindOrCreateUserDebug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Afup\Hermes\Repository\User;

use Afup\Hermes\Entity\User;
use Discord\Builders\MessageBuilder;
use Discord\Parts\Interactions\Interaction;
use Doctrine\ORM\EntityManagerInterface;

final readonly class FindOrCreateUserDebug
{
public function __construct(
private EntityManagerInterface $entityManager,
) {
}

public function __invoke(int $userId, Interaction $interaction): User
{
$userRepository = $this->entityManager->getRepository(User::class);
$interaction->respondWithMessage(MessageBuilder::new()->setContent('Repository'));

/** @var User|null $user */
$user = $userRepository->findOneBy(['userId' => $userId]);
$interaction->respondWithMessage(MessageBuilder::new()->setContent($user instanceof User ? 'OK' : 'KO'));
if (null === $user) {
$user = new User($userId);
$this->entityManager->persist($user);
$this->entityManager->flush();
}
$interaction->respondWithMessage(MessageBuilder::new()->setContent('return'));

return $user;
}
}

0 comments on commit 40f2dc3

Please sign in to comment.