Skip to content

Commit 38ea45a

Browse files
committed
ISSUE-345: fix 404 + template controller test
1 parent ef86452 commit 38ea45a

14 files changed

+301
-109
lines changed

config/services/providers.yml

-9
This file was deleted.

src/Controller/CampaignController.php

+17-7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\HttpFoundation\JsonResponse;
2020
use Symfony\Component\HttpFoundation\Request;
2121
use Symfony\Component\HttpFoundation\Response;
22+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2223
use Symfony\Component\Routing\Attribute\Route;
2324
use Symfony\Component\Serializer\SerializerInterface;
2425

@@ -32,20 +33,17 @@ class CampaignController extends AbstractController
3233
{
3334
use AuthenticationTrait;
3435

35-
private MessageProvider $messageProvider;
3636
private RequestValidator $validator;
3737
private MessageNormalizer $normalizer;
3838
private MessageManager $messageManager;
3939

4040
public function __construct(
4141
Authentication $authentication,
42-
MessageProvider $messageProvider,
4342
RequestValidator $validator,
4443
MessageNormalizer $normalizer,
4544
MessageManager $messageManager
4645
) {
4746
$this->authentication = $authentication;
48-
$this->messageProvider = $messageProvider;
4947
$this->validator = $validator;
5048
$this->normalizer = $normalizer;
5149
$this->messageManager = $messageManager;
@@ -87,7 +85,7 @@ public function __construct(
8785
public function getMessages(Request $request): JsonResponse
8886
{
8987
$authUer = $this->requireAuthentication($request);
90-
$data = $this->messageProvider->getMessagesByOwner($authUer);
88+
$data = $this->messageManager->getMessagesByOwner($authUer);
9189

9290
$normalized = array_map(function ($item) {
9391
return $this->normalizer->normalize($item);
@@ -135,10 +133,14 @@ public function getMessages(Request $request): JsonResponse
135133
)]
136134
public function getMessage(
137135
Request $request,
138-
#[MapEntity(mapping: ['messageId' => 'id'])] Message $message
136+
#[MapEntity(mapping: ['messageId' => 'id'])] ?Message $message = null
139137
): JsonResponse {
140138
$this->requireAuthentication($request);
141139

140+
if (!$message) {
141+
throw new NotFoundHttpException('Campaign not found.');
142+
}
143+
142144
return new JsonResponse($this->normalizer->normalize($message), Response::HTTP_OK);
143145
}
144146

@@ -263,11 +265,15 @@ public function createMessage(Request $request, MessageNormalizer $normalizer):
263265
)]
264266
public function updateMessage(
265267
Request $request,
266-
#[MapEntity(mapping: ['messageId' => 'id'])] Message $message,
267268
SerializerInterface $serializer,
269+
#[MapEntity(mapping: ['messageId' => 'id'])] ?Message $message = null,
268270
): JsonResponse {
269271
$authUser = $this->requireAuthentication($request);
270272

273+
if (!$message) {
274+
throw new NotFoundHttpException('Campaign not found.');
275+
}
276+
271277
/** @return UpdateMessageRequest $updateMessageRequest */
272278
$updateMessageRequest = $serializer->deserialize($request->getContent(), UpdateMessageRequest::class, 'json');
273279
$updateMessageRequest->messageId = $message->getId();
@@ -325,10 +331,14 @@ public function updateMessage(
325331
)]
326332
public function deleteMessage(
327333
Request $request,
328-
#[MapEntity(mapping: ['messageId' => 'id'])] Message $message
334+
#[MapEntity(mapping: ['messageId' => 'id'])] ?Message $message = null
329335
): JsonResponse {
330336
$this->requireAuthentication($request);
331337

338+
if (!$message) {
339+
throw new NotFoundHttpException('Campaign not found.');
340+
}
341+
332342
$this->messageManager->delete($message);
333343

334344
return new JsonResponse(null, Response::HTTP_NO_CONTENT);

src/Controller/ListController.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
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;
2021
use Symfony\Component\Routing\Attribute\Route;
2122

2223
/**
@@ -176,10 +177,14 @@ public function getLists(Request $request): JsonResponse
176177
)]
177178
public function getList(
178179
Request $request,
179-
#[MapEntity(mapping: ['listId' => 'id'])] SubscriberList $list
180+
#[MapEntity(mapping: ['listId' => 'id'])] ?SubscriberList $list = null
180181
): JsonResponse {
181182
$this->requireAuthentication($request);
182183

184+
if (!$list) {
185+
throw new NotFoundHttpException('Subscriber list not found.');
186+
}
187+
183188
return new JsonResponse($this->normalizer->normalize($list), Response::HTTP_OK);
184189
}
185190

@@ -224,10 +229,14 @@ public function getList(
224229
)]
225230
public function deleteList(
226231
Request $request,
227-
#[MapEntity(mapping: ['listId' => 'id'])] SubscriberList $list
232+
#[MapEntity(mapping: ['listId' => 'id'])] ?SubscriberList $list = null
228233
): JsonResponse {
229234
$this->requireAuthentication($request);
230235

236+
if (!$list) {
237+
throw new NotFoundHttpException('Subscriber list not found.');
238+
}
239+
231240
$this->subscriberListManager->delete($list);
232241

233242
return new JsonResponse(null, Response::HTTP_NO_CONTENT);

src/Controller/SessionController.php

+8-10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\HttpFoundation\Request;
1919
use Symfony\Component\HttpFoundation\Response;
2020
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
21+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2122
use Symfony\Component\Routing\Attribute\Route;
2223

2324
/**
@@ -104,6 +105,7 @@ public function createSession(
104105
* This action may only be called for sessions that are owned by the authenticated administrator.
105106
*
106107
* @throws AccessDeniedHttpException
108+
* @throws NotFoundHttpException
107109
*/
108110
#[Route('/{sessionId}', name: 'delete_session', methods: ['DELETE'])]
109111
#[OA\Delete(
@@ -133,23 +135,19 @@ public function createSession(
133135
new OA\Response(
134136
response: 404,
135137
description: 'Failure',
136-
content: new OA\JsonContent(
137-
properties: [
138-
new OA\Property(
139-
property: 'message',
140-
type: 'string',
141-
example: 'There is no session with that ID.'
142-
)
143-
]
144-
)
138+
content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse')
145139
)
146140
]
147141
)]
148142
public function deleteSession(
149143
Request $request,
150-
#[MapEntity(mapping: ['sessionId' => 'id'])] AdministratorToken $token
144+
#[MapEntity(mapping: ['sessionId' => 'id'])] ?AdministratorToken $token = null
151145
): JsonResponse {
152146
$administrator = $this->requireAuthentication($request);
147+
148+
if (!$token) {
149+
throw new NotFoundHttpException('Token not found.');
150+
}
153151
if ($token->getAdministrator() !== $administrator) {
154152
throw new AccessDeniedHttpException('You do not have access to this session.', null, 1519831644);
155153
}

src/Controller/SubscriberController.php

+16-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
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;
2122
use Symfony\Component\Routing\Attribute\Route;
2223
use Symfony\Component\Serializer\SerializerInterface;
2324

@@ -164,19 +165,24 @@ public function createSubscriber(
164165
),
165166
new OA\Response(
166167
response: 404,
167-
description: 'Not Found',
168+
description: 'Failure',
169+
content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse')
168170
)
169171
]
170172
)]
171173
public function updateSubscriber(
172174
Request $request,
173-
#[MapEntity(mapping: ['subscriberId' => 'id'])] Subscriber $subscriber,
174175
SerializerInterface $serializer,
175176
RequestValidator $validator,
176177
SubscriberNormalizer $subscriberNormalizer,
178+
#[MapEntity(mapping: ['subscriberId' => 'id'])] ?Subscriber $subscriber = null,
177179
): JsonResponse {
178180
$this->requireAuthentication($request);
179181

182+
if (!$subscriber) {
183+
throw new NotFoundHttpException('Subscriber not found.');
184+
}
185+
180186
/** @var UpdateSubscriberRequest $dto */
181187
$dto = $serializer->deserialize($request->getContent(), UpdateSubscriberRequest::class, 'json');
182188
$dto->subscriberId = $subscriber->getId();
@@ -221,7 +227,8 @@ public function updateSubscriber(
221227
),
222228
new OA\Response(
223229
response: 404,
224-
description: 'Not Found',
230+
description: 'Failure',
231+
content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse')
225232
)
226233
]
227234
)]
@@ -268,16 +275,20 @@ public function getSubscriber(Request $request, int $subscriberId, SubscriberNor
268275
),
269276
new OA\Response(
270277
response: 404,
271-
description: 'Not Found',
278+
description: 'Failure',
279+
content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse')
272280
)
273281
]
274282
)]
275283
public function deleteSubscriber(
276284
Request $request,
277-
#[MapEntity(mapping: ['subscriberId' => 'id'])] Subscriber $subscriber,
285+
#[MapEntity(mapping: ['subscriberId' => 'id'])] ?Subscriber $subscriber = null,
278286
): JsonResponse {
279287
$this->requireAuthentication($request);
280288

289+
if (!$subscriber) {
290+
throw new NotFoundHttpException('Subscriber not found.');
291+
}
281292
$this->subscriberManager->deleteSubscriber($subscriber);
282293

283294
return new JsonResponse(null, Response::HTTP_NO_CONTENT);

src/Controller/SubscriptionController.php

+36-9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
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;
2122
use Symfony\Component\Routing\Attribute\Route;
2223

2324
/**
@@ -78,16 +79,25 @@ public function __construct(
7879
response: 403,
7980
description: 'Failure',
8081
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
82+
),
83+
new OA\Response(
84+
response: 404,
85+
description: 'Failure',
86+
content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse')
8187
)
8288
]
8389
)]
8490
public function getListMembers(
8591
Request $request,
86-
#[MapEntity(mapping: ['listId' => 'id'])] SubscriberList $list,
87-
SubscriberNormalizer $normalizer
92+
SubscriberNormalizer $normalizer,
93+
#[MapEntity(mapping: ['listId' => 'id'])] ?SubscriberList $list = null,
8894
): JsonResponse {
8995
$this->requireAuthentication($request);
9096

97+
if (!$list) {
98+
throw new NotFoundHttpException('Subscriber list not found.');
99+
}
100+
91101
$subscribers = $this->subscriptionManager->getSubscriberListMembers($list);
92102
$normalized = array_map(function ($item) use ($normalizer) {
93103
return $normalizer->normalize($item);
@@ -142,10 +152,14 @@ public function getListMembers(
142152
)]
143153
public function getSubscribersCount(
144154
Request $request,
145-
#[MapEntity(mapping: ['listId' => 'id'])] SubscriberList $list
155+
#[MapEntity(mapping: ['listId' => 'id'])] ?SubscriberList $list = null,
146156
): JsonResponse {
147157
$this->requireAuthentication($request);
148158

159+
if (!$list) {
160+
throw new NotFoundHttpException('Subscriber list not found.');
161+
}
162+
149163
return new JsonResponse(['subscribers_count' => count($list->getSubscribers())], Response::HTTP_OK);
150164
}
151165

@@ -195,15 +209,20 @@ public function getSubscribersCount(
195209
items: new OA\Items(ref: '#/components/schemas/Subscription')
196210
)
197211
),
212+
new OA\Response(
213+
response: 400,
214+
description: 'Failure',
215+
content: new OA\JsonContent(ref: '#/components/schemas/BadRequestResponse')
216+
),
198217
new OA\Response(
199218
response: 403,
200219
description: 'Failure',
201220
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
202221
),
203222
new OA\Response(
204-
response: 400,
223+
response: 404,
205224
description: 'Failure',
206-
content: new OA\JsonContent(ref: '#/components/schemas/BadRequestResponse')
225+
content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse')
207226
),
208227
new OA\Response(
209228
response: 409,
@@ -219,11 +238,15 @@ public function getSubscribersCount(
219238
)]
220239
public function createSubscription(
221240
Request $request,
222-
#[MapEntity(mapping: ['listId' => 'id'])] SubscriberList $list,
223-
SubscriptionNormalizer $serializer
241+
SubscriptionNormalizer $serializer,
242+
#[MapEntity(mapping: ['listId' => 'id'])] ?SubscriberList $list = null,
224243
): JsonResponse {
225244
$this->requireAuthentication($request);
226245

246+
if (!$list) {
247+
throw new NotFoundHttpException('Subscriber list not found.');
248+
}
249+
227250
/** @var SubscriptionRequest $subscriptionRequest */
228251
$subscriptionRequest = $this->validator->validate($request, SubscriptionRequest::class);
229252
$subscriptions = $this->subscriptionManager->createSubscriptions($list, $subscriptionRequest->emails);
@@ -276,15 +299,19 @@ public function createSubscription(
276299
),
277300
new OA\Response(
278301
response: 404,
279-
description: 'Subscriber or subscription not found.'
302+
description: 'Failure',
303+
content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse')
280304
)
281305
]
282306
)]
283307
public function deleteSubscriptions(
284308
Request $request,
285-
#[MapEntity(mapping: ['listId' => 'id'])] SubscriberList $list,
309+
#[MapEntity(mapping: ['listId' => 'id'])] ?SubscriberList $list = null,
286310
): JsonResponse {
287311
$this->requireAuthentication($request);
312+
if (!$list) {
313+
throw new NotFoundHttpException('Subscriber list not found.');
314+
}
288315
$subscriptionRequest = new SubscriptionRequest();
289316
$subscriptionRequest->emails = $request->query->all('emails');
290317

0 commit comments

Comments
 (0)