Skip to content

Commit 7ee40e8

Browse files
committed
ISSUE-345: paginated templates
1 parent 027a465 commit 7ee40e8

9 files changed

+77
-179
lines changed

src/Controller/AdministratorController.php

+8-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Symfony\Component\HttpFoundation\JsonResponse;
1818
use Symfony\Component\HttpFoundation\Request;
1919
use Symfony\Component\HttpFoundation\Response;
20-
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2120
use Symfony\Component\Routing\Attribute\Route;
2221

2322
/**
@@ -100,7 +99,7 @@ public function getAdministrators(Request $request): JsonResponse
10099
{
101100
$this->requireAuthentication($request);
102101

103-
return new JsonResponse(
102+
return $this->json(
104103
$this->paginatedProvider->getPaginatedList($request, $this->normalizer, Administrator::class),
105104
Response::HTTP_OK
106105
);
@@ -141,7 +140,7 @@ public function createAdministrator(
141140
$administrator = $this->administratorManager->createAdministrator($dto);
142141
$json = $normalizer->normalize($administrator, 'json');
143142

144-
return new JsonResponse($json, Response::HTTP_CREATED);
143+
return $this->json($json, Response::HTTP_CREATED);
145144
}
146145

147146
#[Route('/{administratorId}', name: 'get_administrator', methods: ['GET'])]
@@ -178,11 +177,11 @@ public function getAdministrator(
178177
$this->requireAuthentication($request);
179178

180179
if (!$administrator) {
181-
throw new NotFoundHttpException('Administrator not found.');
180+
throw $this->createNotFoundException('Administrator not found.');
182181
}
183182
$json = $this->normalizer->normalize($administrator, 'json');
184183

185-
return new JsonResponse($json, Response::HTTP_OK);
184+
return $this->json($json, Response::HTTP_OK);
186185
}
187186

188187
#[Route('/{administratorId}', name: 'update_administrator', methods: ['PUT'])]
@@ -223,13 +222,13 @@ public function updateAdministrator(
223222
$this->requireAuthentication($request);
224223

225224
if (!$administrator) {
226-
throw new NotFoundHttpException('Administrator not found.');
225+
throw $this->createNotFoundException('Administrator not found.');
227226
}
228227
/** @var UpdateAdministratorRequest $dto */
229228
$dto = $this->validator->validate($request, UpdateAdministratorRequest::class);
230229
$this->administratorManager->updateAdministrator($administrator, $dto);
231230

232-
return new JsonResponse(null, Response::HTTP_OK);
231+
return $this->json(null, Response::HTTP_OK);
233232
}
234233

235234
#[Route('/{administratorId}', name: 'delete_administrator', methods: ['DELETE'])]
@@ -265,10 +264,10 @@ public function deleteAdministrator(
265264
$this->requireAuthentication($request);
266265

267266
if (!$administrator) {
268-
throw new NotFoundHttpException('Administrator not found.');
267+
throw $this->createNotFoundException('Administrator not found.');
269268
}
270269
$this->administratorManager->deleteAdministrator($administrator);
271270

272-
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
271+
return $this->json(null, Response::HTTP_NO_CONTENT);
273272
}
274273
}

src/Controller/CampaignController.php

+8-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Symfony\Component\HttpFoundation\JsonResponse;
1919
use Symfony\Component\HttpFoundation\Request;
2020
use Symfony\Component\HttpFoundation\Response;
21-
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2221
use Symfony\Component\Routing\Attribute\Route;
2322

2423
/**
@@ -106,7 +105,7 @@ public function getMessages(Request $request): JsonResponse
106105

107106
$filter = (new MessageFilter())->setOwner($authUer);
108107

109-
return new JsonResponse(
108+
return $this->json(
110109
$this->paginatedProvider->getPaginatedList($request, $this->normalizer, Message::class, $filter),
111110
Response::HTTP_OK
112111
);
@@ -156,10 +155,10 @@ public function getMessage(
156155
$this->requireAuthentication($request);
157156

158157
if (!$message) {
159-
throw new NotFoundHttpException('Campaign not found.');
158+
throw $this->createNotFoundException('Campaign not found.');
160159
}
161160

162-
return new JsonResponse($this->normalizer->normalize($message), Response::HTTP_OK);
161+
return $this->json($this->normalizer->normalize($message), Response::HTTP_OK);
163162
}
164163

165164
#[Route('', name: 'create_message', methods: ['POST'])]
@@ -221,7 +220,7 @@ public function createMessage(Request $request, MessageNormalizer $normalizer):
221220
$createMessageRequest = $this->validator->validate($request, CreateMessageRequest::class);
222221
$data = $this->messageManager->createMessage($createMessageRequest, $authUser);
223222

224-
return new JsonResponse($normalizer->normalize($data), Response::HTTP_CREATED);
223+
return $this->json($normalizer->normalize($data), Response::HTTP_CREATED);
225224
}
226225

227226
#[Route('/{messageId}', name: 'update_campaign', methods: ['PUT'])]
@@ -288,13 +287,13 @@ public function updateMessage(
288287
$authUser = $this->requireAuthentication($request);
289288

290289
if (!$message) {
291-
throw new NotFoundHttpException('Campaign not found.');
290+
throw $this->createNotFoundException('Campaign not found.');
292291
}
293292
/** @var UpdateMessageRequest $updateMessageRequest */
294293
$updateMessageRequest = $this->validator->validate($request, UpdateMessageRequest::class);
295294
$data = $this->messageManager->updateMessage($updateMessageRequest, $message, $authUser);
296295

297-
return new JsonResponse($this->normalizer->normalize($data), Response::HTTP_OK);
296+
return $this->json($this->normalizer->normalize($data), Response::HTTP_OK);
298297
}
299298

300299
#[Route('/{messageId}', name: 'delete_campaign', methods: ['DELETE'])]
@@ -346,11 +345,11 @@ public function deleteMessage(
346345
$this->requireAuthentication($request);
347346

348347
if (!$message) {
349-
throw new NotFoundHttpException('Campaign not found.');
348+
throw $this->createNotFoundException('Campaign not found.');
350349
}
351350

352351
$this->messageManager->delete($message);
353352

354-
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
353+
return $this->json(null, Response::HTTP_NO_CONTENT);
355354
}
356355
}

src/Controller/ListController.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\Component\HttpFoundation\JsonResponse;
1717
use Symfony\Component\HttpFoundation\Request;
1818
use Symfony\Component\HttpFoundation\Response;
19-
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2019
use Symfony\Component\Routing\Attribute\Route;
2120

2221
/**
@@ -104,7 +103,7 @@ public function getLists(Request $request): JsonResponse
104103
{
105104
$this->requireAuthentication($request);
106105

107-
return new JsonResponse(
106+
return $this->json(
108107
$this->paginatedDataProvider->getPaginatedList($request, $this->normalizer, SubscriberList::class),
109108
Response::HTTP_OK
110109
);
@@ -166,10 +165,10 @@ public function getList(
166165
$this->requireAuthentication($request);
167166

168167
if (!$list) {
169-
throw new NotFoundHttpException('Subscriber list not found.');
168+
throw $this->createNotFoundException('Subscriber list not found.');
170169
}
171170

172-
return new JsonResponse($this->normalizer->normalize($list), Response::HTTP_OK);
171+
return $this->json($this->normalizer->normalize($list), Response::HTTP_OK);
173172
}
174173

175174
#[Route('/{listId}', name: 'delete_list', methods: ['DELETE'])]
@@ -218,12 +217,12 @@ public function deleteList(
218217
$this->requireAuthentication($request);
219218

220219
if (!$list) {
221-
throw new NotFoundHttpException('Subscriber list not found.');
220+
throw $this->createNotFoundException('Subscriber list not found.');
222221
}
223222

224223
$this->subscriberListManager->delete($list);
225224

226-
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
225+
return $this->json(null, Response::HTTP_NO_CONTENT);
227226
}
228227

229228
#[Route('', name: 'create_list', methods: ['POST'])]
@@ -282,6 +281,6 @@ public function createList(Request $request, SubscriberListNormalizer $normalize
282281
$subscriberListRequest = $this->validator->validate($request, CreateSubscriberListRequest::class);
283282
$data = $this->subscriberListManager->createSubscriberList($subscriberListRequest, $authUser);
284283

285-
return new JsonResponse($normalizer->normalize($data), Response::HTTP_CREATED);
284+
return $this->json($normalizer->normalize($data), Response::HTTP_CREATED);
286285
}
287286
}

src/Controller/SessionController.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\Component\HttpFoundation\Request;
1717
use Symfony\Component\HttpFoundation\Response;
1818
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
19-
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2019
use Symfony\Component\Routing\Attribute\Route;
2120

2221
/**
@@ -95,7 +94,7 @@ public function createSession(
9594

9695
$json = $normalizer->normalize($token, 'json');
9796

98-
return new JsonResponse($json, Response::HTTP_CREATED);
97+
return $this->json($json, Response::HTTP_CREATED);
9998
}
10099

101100
/**
@@ -104,7 +103,6 @@ public function createSession(
104103
* This action may only be called for sessions that are owned by the authenticated administrator.
105104
*
106105
* @throws AccessDeniedHttpException
107-
* @throws NotFoundHttpException
108106
*/
109107
#[Route('/{sessionId}', name: 'delete_session', methods: ['DELETE'])]
110108
#[OA\Delete(
@@ -145,14 +143,14 @@ public function deleteSession(
145143
$administrator = $this->requireAuthentication($request);
146144

147145
if (!$token) {
148-
throw new NotFoundHttpException('Token not found.');
146+
throw $this->createNotFoundException('Token not found.');
149147
}
150148
if ($token->getAdministrator() !== $administrator) {
151149
throw new AccessDeniedHttpException('You do not have access to this session.', null, 1519831644);
152150
}
153151

154152
$this->sessionManager->deleteSession($token);
155153

156-
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
154+
return $this->json(null, Response::HTTP_NO_CONTENT);
157155
}
158156
}

src/Controller/SubscriberController.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\Component\HttpFoundation\JsonResponse;
1717
use Symfony\Component\HttpFoundation\Request;
1818
use Symfony\Component\HttpFoundation\Response;
19-
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2019
use Symfony\Component\Routing\Attribute\Route;
2120

2221
/**
@@ -101,7 +100,7 @@ public function createSubscriber(Request $request): JsonResponse
101100
$subscriberRequest = $this->validator->validate($request, CreateSubscriberRequest::class);
102101
$subscriber = $this->subscriberManager->createSubscriber($subscriberRequest);
103102

104-
return new JsonResponse(
103+
return $this->json(
105104
$this->subscriberNormalizer->normalize($subscriber, 'json'),
106105
Response::HTTP_CREATED
107106
);
@@ -174,13 +173,13 @@ public function updateSubscriber(
174173
$this->requireAuthentication($request);
175174

176175
if (!$subscriber) {
177-
throw new NotFoundHttpException('Subscriber not found.');
176+
throw $this->createNotFoundException('Subscriber not found.');
178177
}
179178
/** @var UpdateSubscriberRequest $dto */
180179
$dto = $this->validator->validate($request, UpdateSubscriberRequest::class);
181180
$subscriber = $this->subscriberManager->updateSubscriber($dto);
182181

183-
return new JsonResponse($this->subscriberNormalizer->normalize($subscriber, 'json'), Response::HTTP_OK);
182+
return $this->json($this->subscriberNormalizer->normalize($subscriber, 'json'), Response::HTTP_OK);
184183
}
185184

186185
#[Route('/{subscriberId}', name: 'get_subscriber_by_id', methods: ['GET'])]
@@ -229,7 +228,7 @@ public function getSubscriber(Request $request, int $subscriberId): JsonResponse
229228

230229
$subscriber = $this->subscriberManager->getSubscriber($subscriberId);
231230

232-
return new JsonResponse($this->subscriberNormalizer->normalize($subscriber), Response::HTTP_OK);
231+
return $this->json($this->subscriberNormalizer->normalize($subscriber), Response::HTTP_OK);
233232
}
234233

235234
#[Route('/{subscriberId}', name: 'delete_subscriber', requirements: ['subscriberId' => '\d+'], methods: ['DELETE'])]
@@ -278,10 +277,10 @@ public function deleteSubscriber(
278277
$this->requireAuthentication($request);
279278

280279
if (!$subscriber) {
281-
throw new NotFoundHttpException('Subscriber not found.');
280+
throw $this->createNotFoundException('Subscriber not found.');
282281
}
283282
$this->subscriberManager->deleteSubscriber($subscriber);
284283

285-
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
284+
return $this->json(null, Response::HTTP_NO_CONTENT);
286285
}
287286
}

src/Controller/SubscriptionController.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function getListMembers(
123123
throw $this->createNotFoundException('Subscriber list not found.');
124124
}
125125

126-
return new JsonResponse(
126+
return $this->json(
127127
$this->paginatedProvider->getPaginatedList(
128128
$request,
129129
$this->subscriberNormalizer,
@@ -188,7 +188,7 @@ public function getSubscribersCount(
188188
throw $this->createNotFoundException('Subscriber list not found.');
189189
}
190190

191-
return new JsonResponse(['subscribers_count' => count($list->getSubscribers())], Response::HTTP_OK);
191+
return $this->json(['subscribers_count' => count($list->getSubscribers())], Response::HTTP_OK);
192192
}
193193

194194
#[Route('/{listId}/subscribers', name: 'create_subscription', methods: ['POST'])]
@@ -279,7 +279,7 @@ public function createSubscription(
279279
$subscriptions = $this->subscriptionManager->createSubscriptions($list, $subscriptionRequest->emails);
280280
$normalized = array_map(fn($item) => $this->subscriptionNormalizer->normalize($item), $subscriptions);
281281

282-
return new JsonResponse($normalized, Response::HTTP_CREATED);
282+
return $this->json($normalized, Response::HTTP_CREATED);
283283
}
284284

285285
#[Route('/{listId}/subscribers', name: 'delete_subscription', methods: ['DELETE'])]
@@ -343,6 +343,6 @@ public function deleteSubscriptions(
343343
$subscriptionRequest = $this->validator->validateDto($subscriptionRequest);
344344
$this->subscriptionManager->deleteSubscriptions($list, $subscriptionRequest->emails);
345345

346-
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
346+
return $this->json(null, Response::HTTP_NO_CONTENT);
347347
}
348348
}

0 commit comments

Comments
 (0)