|
1
|
|
|
<?php |
|
2
|
|
|
namespace VideoGamesRecords\CoreBundle\EventSubscriber; |
|
3
|
|
|
|
|
4
|
|
|
use ApiPlatform\Core\EventListener\EventPriorities; |
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
9
|
|
|
use Symfony\Component\HttpKernel\Event\ViewEvent; |
|
10
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
11
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
12
|
|
|
use VideoGamesRecords\CoreBundle\Entity\TeamRequest; |
|
13
|
|
|
use VideoGamesRecords\CoreBundle\Exception\PostException; |
|
14
|
|
|
|
|
15
|
|
|
final class IsValidTeamRequestSubscriber implements EventSubscriberInterface |
|
16
|
|
|
{ |
|
17
|
|
|
private EntityManagerInterface $em; |
|
18
|
|
|
private TranslatorInterface $translator; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(EntityManagerInterface $em, TranslatorInterface $translator) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->em = $em; |
|
23
|
|
|
$this->translator = $translator; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public static function getSubscribedEvents(): array |
|
27
|
|
|
{ |
|
28
|
|
|
return [ |
|
29
|
|
|
KernelEvents::VIEW => ['validate', EventPriorities::POST_VALIDATE], |
|
30
|
|
|
]; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param ViewEvent $event |
|
35
|
|
|
* @throws PostException |
|
36
|
|
|
* @throws Exception |
|
37
|
|
|
*/ |
|
38
|
|
|
public function validate(ViewEvent $event) |
|
39
|
|
|
{ |
|
40
|
|
|
$requestA = $event->getControllerResult(); |
|
41
|
|
|
$method = $event->getRequest()->getMethod(); |
|
42
|
|
|
|
|
43
|
|
|
if (($requestA instanceof TeamRequest) && ($method == Request::METHOD_POST)) { |
|
44
|
|
|
$requestB = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\TeamRequest') |
|
45
|
|
|
->findOneBy( |
|
46
|
|
|
array( |
|
47
|
|
|
'player' => $requestA->getPlayer(), |
|
48
|
|
|
'status' => TeamRequest::STATUS_ACTIVE |
|
49
|
|
|
) |
|
50
|
|
|
); |
|
51
|
|
|
if ($requestB) { |
|
52
|
|
|
throw new PostException($this->translator->trans('team.request.exists')); |
|
53
|
|
|
} |
|
54
|
|
|
if ($requestA->getPlayer()->getTeam() != null) { |
|
55
|
|
|
throw new PostException($this->translator->trans('team.request.has_team')); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|