1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace App\Controller; |
5
|
|
|
|
6
|
|
|
use App\Guests\Entity\GuestSession; |
7
|
|
|
use App\Guests\Repository\GuestRepository; |
8
|
|
|
use App\Pagination\PaginatedCollectionInterface; |
9
|
|
|
use App\Translation\TranslatedResponseTrait; |
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
11
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class BaseController |
16
|
|
|
* @package App\Controller |
17
|
|
|
*/ |
18
|
|
|
abstract class BaseController extends Controller implements ControllerInterface |
19
|
|
|
{ |
20
|
|
|
use TranslatedResponseTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param $data |
24
|
|
|
* @param int $status |
25
|
|
|
* @param array $headers |
26
|
|
|
* @param array $context |
27
|
|
|
* @return JsonResponse |
28
|
|
|
*/ |
29
|
18 |
|
protected function response($data, int $status = 200, array $headers = [], array $context = []): JsonResponse |
30
|
|
|
{ |
31
|
18 |
|
if ($data instanceof PaginatedCollectionInterface) { |
32
|
5 |
|
$data = $this->addMetaPaginationInfo($data); |
33
|
|
|
} |
34
|
|
|
|
35
|
18 |
|
$contextWithRoles = $this->appendRolesToContextGroups($context); |
36
|
18 |
|
$translatedContent = $this->translateResponse($data, $contextWithRoles); |
37
|
|
|
|
38
|
18 |
|
return $this->json($translatedContent, $status, $headers, $context); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function getLocales() |
42
|
|
|
{ |
43
|
|
|
return $this->getParameter('locales'); |
44
|
|
|
} |
45
|
|
|
|
46
|
3 |
|
protected function getGuest(): ?GuestSession |
47
|
|
|
{ |
48
|
|
|
/** @var $request Request */ |
49
|
3 |
|
$request = $this->get('request_stack')->getCurrentRequest(); |
50
|
3 |
|
$guestSessionToken = (string)$request->get('guest_api_token', ''); |
51
|
|
|
|
52
|
|
|
/** @var $guestRepository GuestRepository */ |
53
|
3 |
|
$guestRepository = $this->getDoctrine()->getRepository(GuestSession::class); |
54
|
|
|
|
55
|
3 |
|
return $guestRepository->findOneBy([ |
56
|
3 |
|
'token' => $guestSessionToken |
57
|
|
|
]); |
58
|
|
|
} |
59
|
|
|
|
60
|
5 |
|
private function addMetaPaginationInfo(PaginatedCollectionInterface $paginatedCollection) |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
5 |
|
'data' => $paginatedCollection->getItems(), |
64
|
|
|
'paging' => [ |
65
|
5 |
|
'total' => $paginatedCollection->getTotal(), |
66
|
5 |
|
'offset' => $paginatedCollection->getOffset(), |
67
|
5 |
|
'limit' => $paginatedCollection->getLimit(), |
68
|
|
|
] |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
18 |
|
private function appendRolesToContextGroups(?array $context): array |
73
|
|
|
{ |
74
|
18 |
|
if ($this->getUser() === null) return $context; |
75
|
|
|
|
76
|
5 |
|
if ($context === null) { |
77
|
|
|
return [ |
78
|
|
|
'groups' => $this->getUser()->getRoles(), |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
5 |
|
if (isset($context['groups'])) { |
83
|
5 |
|
$context['groups'] = array_merge($context['groups'], $this->getUser()->getRoles()); |
84
|
|
|
} else { |
85
|
|
|
$context['groups'] = $this->getUser()->getRoles(); |
86
|
|
|
} |
87
|
|
|
|
88
|
5 |
|
return $context; |
89
|
|
|
} |
90
|
|
|
} |