Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/FormType/PuzzleSolvingTimeFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Ramsey\Uuid\Uuid;
use SpeedPuzzling\Web\FormData\PuzzleSolvingTimeFormData;
use SpeedPuzzling\Web\Query\GetManufacturers;
use SpeedPuzzling\Web\Results\PlayerProfile;
use SpeedPuzzling\Web\Services\CheckFirstTryAvailability;
use SpeedPuzzling\Web\Services\RetrieveLoggedUserProfile;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
Expand Down Expand Up @@ -35,6 +37,7 @@ public function __construct(
readonly private RetrieveLoggedUserProfile $retrieveLoggedUserProfile,
readonly private TranslatorInterface $translator,
readonly private UrlGeneratorInterface $urlGenerator,
readonly private CheckFirstTryAvailability $checkFirstTryAvailability,
) {
}

Expand Down Expand Up @@ -179,12 +182,12 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'required' => false,
]);

$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event): void {
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use ($userProfile): void {
$form = $event->getForm();
$data = $event->getData();
assert($data instanceof PuzzleSolvingTimeFormData);

$this->applyDynamicRules($form, $data);
$this->applyDynamicRules($form, $data, $userProfile);
});
}

Expand All @@ -201,6 +204,7 @@ public function configureOptions(OptionsResolver $resolver): void
private function applyDynamicRules(
FormInterface $form,
PuzzleSolvingTimeFormData $data,
PlayerProfile $playerProfile,
): void {
// TODO: Should check if the puzzle exists in database as well
if (is_string($data->puzzle) && Uuid::isValid($data->puzzle) === false) {
Expand All @@ -212,5 +216,14 @@ private function applyDynamicRules(
$form->get('puzzlePhoto')->addError(new FormError($this->translator->trans('forms.puzzle_photo_is_required')));
}
}

if (is_string($data->puzzle) && Uuid::isValid($data->puzzle) === true && $data->firstAttempt === true) {
$firstTry = $this->checkFirstTryAvailability->forPlayer($playerProfile->playerId, $data->puzzle);

if ($firstTry === false) {
$form->get('firstAttempt')->addError(new FormError($this->translator->trans('Píčo tvůj first attempt už byl tady: ...')));
$form->addError(new FormError('ASdfda fasdf asdf asdf asdf as'));
}
}
}
}
13 changes: 13 additions & 0 deletions src/Services/CheckFirstTryAvailability.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace SpeedPuzzling\Web\Services;

readonly final class CheckFirstTryAvailability
{
public function forPlayer(string $playerId, string $puzzleId): bool
{
return false;
}
}