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
|
17 |
|
public function items($data, Transformer $transformer): JsonResponse |
24
|
|
|
{ |
25
|
17 |
|
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
|
44 |
|
public function response($data, int $status = 200, array $headers = [], array $context = [], ?Transformer $transformer = null): JsonResponse |
38
|
|
|
{ |
39
|
44 |
|
if ($data instanceof PaginatedCollectionInterface) { |
40
|
25 |
|
$data = $this->addMetaPaginationInfo($data); |
41
|
|
|
} |
42
|
|
|
|
43
|
44 |
|
$contextWithRoles = $this->appendRolesToContextGroups($context); |
44
|
44 |
|
$response = $this->translateResponse($data, $contextWithRoles); |
45
|
44 |
|
if ($transformer !== null) { |
46
|
17 |
|
$response = $this->prepareResponseData($response, $transformer); |
47
|
|
|
} |
48
|
44 |
|
return $this->json($response, $status, $headers, $context); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getLocales() |
52
|
|
|
{ |
53
|
|
|
return $this->getParameter('locales'); |
54
|
|
|
} |
55
|
|
|
|
56
|
17 |
|
public function getGuest(): ?GuestSession |
57
|
|
|
{ |
58
|
|
|
/** @var $request Request */ |
59
|
17 |
|
$request = $this->get('request_stack')->getCurrentRequest(); |
60
|
17 |
|
$guestSessionToken = (string) $request->get('guest_api_token', ''); |
61
|
|
|
|
62
|
|
|
/** @var $guestRepository GuestRepository */ |
63
|
17 |
|
$guestRepository = $this->getDoctrine()->getRepository(GuestSession::class); |
64
|
|
|
|
65
|
17 |
|
return $guestRepository->findOneBy([ |
66
|
17 |
|
'token' => $guestSessionToken, |
67
|
|
|
]); |
68
|
|
|
} |
69
|
|
|
|
70
|
25 |
|
private function addMetaPaginationInfo(PaginatedCollectionInterface $paginatedCollection) |
71
|
|
|
{ |
72
|
|
|
return [ |
73
|
25 |
|
'data' => $paginatedCollection->getItems(), |
74
|
|
|
'paging' => [ |
75
|
25 |
|
'total' => $paginatedCollection->getTotal(), |
76
|
25 |
|
'offset' => $paginatedCollection->getOffset(), |
77
|
25 |
|
'limit' => $paginatedCollection->getLimit(), |
78
|
|
|
], |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
44 |
|
private function appendRolesToContextGroups(?array $context): array |
83
|
|
|
{ |
84
|
44 |
|
if ($this->getUser() === null) { |
85
|
31 |
|
return $context; |
86
|
|
|
} |
87
|
|
|
|
88
|
20 |
|
if ($context === null) { |
89
|
|
|
return [ |
90
|
|
|
'groups' => $this->getUser()->getRoles(), |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
|
94
|
20 |
|
if (isset($context['groups'])) { |
95
|
20 |
|
$context['groups'] = array_merge($context['groups'], $this->getUser()->getRoles()); |
96
|
|
|
} else { |
97
|
|
|
$context['groups'] = $this->getUser()->getRoles(); |
98
|
|
|
} |
99
|
|
|
|
100
|
20 |
|
return $context; |
101
|
|
|
} |
102
|
|
|
|
103
|
17 |
|
private function prepareResponseData(array $data, Transformer $transformer): array |
104
|
|
|
{ |
105
|
17 |
|
$response = []; |
106
|
17 |
|
$data = $transformer->process($data); |
107
|
17 |
|
foreach ($data as $key => $value) { |
108
|
17 |
|
if (is_array($value)) { |
109
|
|
|
//$value = $transformer->process($value); |
|
|
|
|
110
|
17 |
|
$response[$key] = $this->prepareResponseData($value, $transformer); |
111
|
17 |
|
continue; |
112
|
|
|
} |
113
|
17 |
|
$response[$key] = $value; |
114
|
|
|
} |
115
|
|
|
|
116
|
17 |
|
return $response; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.