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