|
1
|
|
|
<?php |
|
2
|
|
|
namespace VideoGamesRecords\CoreBundle\EventSubscriber\Badge; |
|
3
|
|
|
|
|
4
|
|
|
use ApiPlatform\Symfony\EventListener\EventPriorities as EventPrioritiesAlias; |
|
5
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
7
|
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent; |
|
8
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
9
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
10
|
|
|
use VideoGamesRecords\CoreBundle\Contracts\BadgeInterface; |
|
11
|
|
|
use VideoGamesRecords\CoreBundle\Entity\PlayerBadge; |
|
12
|
|
|
use VideoGamesRecords\CoreBundle\Manager\BadgeManager; |
|
13
|
|
|
|
|
14
|
|
|
final class SetBadgeTitleSubscriber implements EventSubscriberInterface, BadgeInterface |
|
15
|
|
|
{ |
|
16
|
|
|
private TranslatorInterface $translator; |
|
17
|
|
|
private BadgeManager $badgeManager; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(TranslatorInterface $translator, BadgeManager $badgeManager) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->translator = $translator; |
|
22
|
|
|
$this->badgeManager = $badgeManager; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public static function getSubscribedEvents(): array |
|
26
|
|
|
{ |
|
27
|
|
|
return [ |
|
28
|
|
|
KernelEvents::REQUEST => ['setTitle', EventPrioritiesAlias::POST_READ], |
|
29
|
|
|
]; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param RequestEvent $event |
|
34
|
|
|
*/ |
|
35
|
|
|
public function setTitle(RequestEvent $event) |
|
36
|
|
|
{ |
|
37
|
|
|
$data = $event->getRequest()->attributes->get('data'); |
|
38
|
|
|
$method = $event->getRequest()->getMethod(); |
|
39
|
|
|
|
|
40
|
|
|
if ($method == Request::METHOD_GET && is_array($data) && $data[0] instanceof PlayerBadge) { |
|
41
|
|
|
foreach ($data as $playerBadge) { |
|
42
|
|
|
$playerBadge->getBadge()->setTitle( |
|
43
|
|
|
sprintf( |
|
44
|
|
|
'%s %s %s', |
|
45
|
|
|
$this->badgeManager->getStrategy($playerBadge->getBadge())->getTitle($playerBadge->getBadge()), |
|
46
|
|
|
$this->translator->trans('badge.earnedOn'), |
|
47
|
|
|
$playerBadge->getCreatedAt()->format('Y-m-d') |
|
48
|
|
|
) |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|