|
1
|
|
|
<?php |
|
2
|
|
|
namespace VideoGamesRecords\CoreBundle\EventSubscriber; |
|
3
|
|
|
|
|
4
|
|
|
use ApiPlatform\Core\EventListener\EventPriorities; |
|
5
|
|
|
use Doctrine\ORM\Exception\ORMException; |
|
6
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
8
|
|
|
use Symfony\Component\HttpKernel\Event\ViewEvent; |
|
9
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
10
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
11
|
|
|
use VideoGamesRecords\CoreBundle\Contracts\VgrCoreInterface; |
|
12
|
|
|
use VideoGamesRecords\CoreBundle\Entity\ProofRequest; |
|
13
|
|
|
use VideoGamesRecords\CoreBundle\Exception\PostException; |
|
14
|
|
|
use VideoGamesRecords\CoreBundle\Security\UserProvider; |
|
15
|
|
|
use VideoGamesRecords\CoreBundle\DataProvider\CanAskProofProvider; |
|
16
|
|
|
|
|
17
|
|
|
final class CanAskProofSubscriber implements EventSubscriberInterface, VgrCoreInterface |
|
18
|
|
|
{ |
|
19
|
|
|
private TranslatorInterface $translator; |
|
20
|
|
|
private CanAskProofProvider $canAskProofProvider; |
|
21
|
|
|
private UserProvider $userProvider; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct( |
|
24
|
|
|
TranslatorInterface $translator, |
|
25
|
|
|
CanAskProofProvider $canAskProofProvider, |
|
26
|
|
|
UserProvider $userProvider |
|
27
|
|
|
) { |
|
28
|
|
|
$this->translator = $translator; |
|
29
|
|
|
$this->canAskProofProvider = $canAskProofProvider; |
|
30
|
|
|
$this->userProvider = $userProvider; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public static function getSubscribedEvents(): array |
|
34
|
|
|
{ |
|
35
|
|
|
return [ |
|
36
|
|
|
KernelEvents::VIEW => ['setPlayerRequesting', EventPriorities::POST_VALIDATE], |
|
37
|
|
|
]; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param ViewEvent $event |
|
42
|
|
|
* @return void |
|
43
|
|
|
* @throws PostException |
|
44
|
|
|
* @throws ORMException |
|
45
|
|
|
*/ |
|
46
|
|
|
public function setPlayerRequesting(ViewEvent $event) |
|
47
|
|
|
{ |
|
48
|
|
|
$request = $event->getControllerResult(); |
|
49
|
|
|
$method = $event->getRequest()->getMethod(); |
|
50
|
|
|
|
|
51
|
|
|
if (($request instanceof ProofRequest) && ($method == Request::METHOD_POST)) { |
|
52
|
|
|
$player = $this->userProvider->getPlayer(); |
|
53
|
|
|
|
|
54
|
|
|
if (false === $this->canAskProofProvider->load($player)) { |
|
55
|
|
|
throw new PostException( |
|
56
|
|
|
sprintf( |
|
57
|
|
|
$this->translator->trans('proof.request.send.refuse'), |
|
58
|
|
|
self::MAX_PROOF_REQUEST_DAY |
|
59
|
|
|
) |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$request->setPlayerRequesting($player); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|