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\Transformer\Transformer; |
11
|
|
|
use App\Translation\TranslatedResponseTrait; |
12
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
13
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class BaseController. |
18
|
|
|
*/ |
19
|
|
|
abstract class BaseController extends Controller implements ControllerInterface |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
use TranslatedResponseTrait; |
22
|
|
|
|
23
|
36 |
|
public function items($data, Transformer $transformer): JsonResponse |
24
|
|
|
{ |
25
|
36 |
|
return $this->response($data, 200, [], [], $transformer); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param $data |
30
|
|
|
* @param int $status |
31
|
|
|
* @param array $headers |
32
|
|
|
* @param array $context |
33
|
|
|
* @param Transformer $transformer |
34
|
|
|
* |
35
|
|
|
* @return JsonResponse |
36
|
|
|
*/ |
37
|
66 |
|
public function response($data, int $status = 200, array $headers = [], array $context = [], ?Transformer $transformer = null): JsonResponse |
38
|
|
|
{ |
39
|
66 |
|
if ($data instanceof PaginatedCollectionInterface) { |
40
|
52 |
|
$data = $this->addMetaPaginationInfo($data); |
41
|
|
|
} |
42
|
|
|
|
43
|
66 |
|
$contextWithRoles = $this->appendRolesToContextGroups($context); |
44
|
66 |
|
$response = $this->translateResponse($data, $contextWithRoles); |
45
|
66 |
|
if ($transformer !== null) { |
46
|
36 |
|
$response = $this->prepareResponseData($response, $transformer); |
47
|
|
|
} |
48
|
|
|
|
49
|
66 |
|
return $this->json($response, $status, $headers, $context); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getLocales() |
53
|
|
|
{ |
54
|
|
|
return $this->getParameter('locales'); |
55
|
|
|
} |
56
|
|
|
|
57
|
34 |
|
public function getGuest(): ?GuestSession |
58
|
|
|
{ |
59
|
|
|
/** @var $request Request */ |
60
|
34 |
|
$request = $this->get('request_stack')->getCurrentRequest(); |
61
|
34 |
|
$guestSessionToken = (string) $request->get('guest_api_token', ''); |
62
|
|
|
|
63
|
|
|
/** @var $guestRepository GuestRepository */ |
64
|
34 |
|
$guestRepository = $this->getDoctrine()->getRepository(GuestSession::class); |
65
|
|
|
|
66
|
34 |
|
return $guestRepository->findOneBy([ |
67
|
34 |
|
'token' => $guestSessionToken, |
68
|
|
|
]); |
69
|
|
|
} |
70
|
|
|
|
71
|
52 |
|
private function addMetaPaginationInfo(PaginatedCollectionInterface $paginatedCollection) |
72
|
|
|
{ |
73
|
|
|
return [ |
74
|
52 |
|
'data' => $paginatedCollection->getItems(), |
75
|
|
|
'paging' => [ |
76
|
52 |
|
'total' => $paginatedCollection->getTotal(), |
77
|
52 |
|
'offset' => $paginatedCollection->getOffset(), |
78
|
52 |
|
'limit' => $paginatedCollection->getLimit(), |
79
|
|
|
], |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
66 |
|
private function appendRolesToContextGroups(?array $context): array |
84
|
|
|
{ |
85
|
66 |
|
if ($this->getUser() === null) { |
86
|
51 |
|
return $context; |
87
|
|
|
} |
88
|
|
|
|
89
|
22 |
|
if ($context === null) { |
90
|
|
|
return [ |
91
|
|
|
'groups' => $this->getUser()->getRoles(), |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
22 |
|
if (isset($context['groups'])) { |
96
|
18 |
|
$context['groups'] = array_merge($context['groups'], $this->getUser()->getRoles()); |
97
|
|
|
} else { |
98
|
4 |
|
$context['groups'] = $this->getUser()->getRoles(); |
99
|
|
|
} |
100
|
|
|
|
101
|
22 |
|
return $context; |
102
|
|
|
} |
103
|
|
|
|
104
|
36 |
|
private function prepareResponseData(array $data, Transformer $transformer): array |
105
|
|
|
{ |
106
|
36 |
|
$response = []; |
107
|
36 |
|
$data = $transformer->process($data); |
108
|
36 |
|
foreach ($data as $key => $value) { |
109
|
36 |
|
if (\is_array($value)) { |
110
|
|
|
//$value = $transformer->process($value); |
111
|
36 |
|
$response[$key] = $this->prepareResponseData($value, $transformer); |
112
|
36 |
|
continue; |
113
|
|
|
} |
114
|
36 |
|
$response[$key] = $value; |
115
|
|
|
} |
116
|
|
|
|
117
|
36 |
|
return $response; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.