77use Ramsey \Uuid \Uuid ;
88use SpeedPuzzling \Web \FormData \PuzzleSolvingTimeFormData ;
99use SpeedPuzzling \Web \Query \GetManufacturers ;
10+ use SpeedPuzzling \Web \Results \PlayerProfile ;
11+ use SpeedPuzzling \Web \Services \CheckFirstTryAvailability ;
1012use SpeedPuzzling \Web \Services \RetrieveLoggedUserProfile ;
1113use Symfony \Component \Form \AbstractType ;
1214use Symfony \Component \Form \Extension \Core \Type \CheckboxType ;
@@ -35,6 +37,7 @@ public function __construct(
3537 readonly private RetrieveLoggedUserProfile $ retrieveLoggedUserProfile ,
3638 readonly private TranslatorInterface $ translator ,
3739 readonly private UrlGeneratorInterface $ urlGenerator ,
40+ readonly private CheckFirstTryAvailability $ checkFirstTryAvailability ,
3841 ) {
3942 }
4043
@@ -179,12 +182,12 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
179182 'required ' => false ,
180183 ]);
181184
182- $ builder ->addEventListener (FormEvents::POST_SUBMIT , function (FormEvent $ event ): void {
185+ $ builder ->addEventListener (FormEvents::POST_SUBMIT , function (FormEvent $ event ) use ( $ userProfile ) : void {
183186 $ form = $ event ->getForm ();
184187 $ data = $ event ->getData ();
185188 assert ($ data instanceof PuzzleSolvingTimeFormData);
186189
187- $ this ->applyDynamicRules ($ form , $ data );
190+ $ this ->applyDynamicRules ($ form , $ data, $ userProfile );
188191 });
189192 }
190193
@@ -201,6 +204,7 @@ public function configureOptions(OptionsResolver $resolver): void
201204 private function applyDynamicRules (
202205 FormInterface $ form ,
203206 PuzzleSolvingTimeFormData $ data ,
207+ PlayerProfile $ playerProfile ,
204208 ): void {
205209 // TODO: Should check if the puzzle exists in database as well
206210 if (is_string ($ data ->puzzle ) && Uuid::isValid ($ data ->puzzle ) === false ) {
@@ -212,5 +216,14 @@ private function applyDynamicRules(
212216 $ form ->get ('puzzlePhoto ' )->addError (new FormError ($ this ->translator ->trans ('forms.puzzle_photo_is_required ' )));
213217 }
214218 }
219+
220+ if (is_string ($ data ->puzzle ) && Uuid::isValid ($ data ->puzzle ) === true && $ data ->firstAttempt === true ) {
221+ $ firstTry = $ this ->checkFirstTryAvailability ->forPlayer ($ playerProfile ->playerId , $ data ->puzzle );
222+
223+ if ($ firstTry === false ) {
224+ $ form ->get ('firstAttempt ' )->addError (new FormError ($ this ->translator ->trans ('Píčo tvůj first attempt už byl tady: ... ' )));
225+ $ form ->addError (new FormError ('ASdfda fasdf asdf asdf asdf as ' ));
226+ }
227+ }
215228 }
216229}
0 commit comments