Skip to content

Commit cceadfc

Browse files
committed
ISSUE-345: style fix
1 parent 24c5f75 commit cceadfc

File tree

10 files changed

+513
-141
lines changed

10 files changed

+513
-141
lines changed

config/services/providers.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ services:
77
PhpList\RestBundle\Common\Service\Provider\PaginatedDataProvider:
88
autowire: true
99
autoconfigure: true
10+
11+
PhpList\RestBundle\Messaging\Service\CampaignService:
12+
autowire: true
13+
autoconfigure: true

src/Common/Controller/BaseController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Symfony\Component\HttpFoundation\Request;
1212
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
1313

14+
/** @SuppressWarnings(PHPMD.NumberOfChildren) */
1415
abstract class BaseController extends AbstractController
1516
{
1617
protected Authentication $authentication;

src/Messaging/Controller/CampaignController.php

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@
55
namespace PhpList\RestBundle\Messaging\Controller;
66

77
use OpenApi\Attributes as OA;
8-
use PhpList\Core\Domain\Identity\Model\PrivilegeFlag;
9-
use PhpList\Core\Domain\Messaging\Model\Filter\MessageFilter;
108
use PhpList\Core\Domain\Messaging\Model\Message;
11-
use PhpList\Core\Domain\Messaging\Service\MessageManager;
129
use PhpList\Core\Security\Authentication;
1310
use PhpList\RestBundle\Common\Controller\BaseController;
14-
use PhpList\RestBundle\Common\Service\Provider\PaginatedDataProvider;
1511
use PhpList\RestBundle\Common\Validator\RequestValidator;
1612
use PhpList\RestBundle\Messaging\Request\CreateMessageRequest;
1713
use PhpList\RestBundle\Messaging\Request\UpdateMessageRequest;
18-
use PhpList\RestBundle\Messaging\Serializer\MessageNormalizer;
14+
use PhpList\RestBundle\Messaging\Service\CampaignService;
1915
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
2016
use Symfony\Component\HttpFoundation\JsonResponse;
2117
use Symfony\Component\HttpFoundation\Request;
@@ -30,21 +26,15 @@
3026
#[Route('/campaigns', name: 'campaign_')]
3127
class CampaignController extends BaseController
3228
{
33-
private MessageNormalizer $normalizer;
34-
private MessageManager $messageManager;
35-
private PaginatedDataProvider $paginatedProvider;
29+
private CampaignService $campaignService;
3630

3731
public function __construct(
3832
Authentication $authentication,
3933
RequestValidator $validator,
40-
MessageNormalizer $normalizer,
41-
MessageManager $messageManager,
42-
PaginatedDataProvider $paginatedProvider,
34+
CampaignService $campaignService,
4335
) {
4436
parent::__construct($authentication, $validator);
45-
$this->normalizer = $normalizer;
46-
$this->messageManager = $messageManager;
47-
$this->paginatedProvider = $paginatedProvider;
37+
$this->campaignService = $campaignService;
4838
}
4939

5040
#[Route('', name: 'get_list', methods: ['GET'])]
@@ -104,12 +94,10 @@ public function __construct(
10494
)]
10595
public function getMessages(Request $request): JsonResponse
10696
{
107-
$authUer = $this->requireAuthentication($request);
108-
109-
$filter = (new MessageFilter())->setOwner($authUer);
97+
$authUser = $this->requireAuthentication($request);
11098

11199
return $this->json(
112-
$this->paginatedProvider->getPaginatedList($request, $this->normalizer, Message::class, $filter),
100+
$this->campaignService->getMessages($request, $authUser),
113101
Response::HTTP_OK
114102
);
115103
}
@@ -158,11 +146,7 @@ public function getMessage(
158146
): JsonResponse {
159147
$this->requireAuthentication($request);
160148

161-
if (!$message) {
162-
throw $this->createNotFoundException('Campaign not found.');
163-
}
164-
165-
return $this->json($this->normalizer->normalize($message), Response::HTTP_OK);
149+
return $this->json($this->campaignService->getMessage($message), Response::HTTP_OK);
166150
}
167151

168152
#[Route('', name: 'create', methods: ['POST'])]
@@ -217,18 +201,17 @@ public function getMessage(
217201
),
218202
]
219203
)]
220-
public function createMessage(Request $request, MessageNormalizer $normalizer): JsonResponse
204+
public function createMessage(Request $request): JsonResponse
221205
{
222206
$authUser = $this->requireAuthentication($request);
223-
if (!$authUser->getPrivileges()->has(PrivilegeFlag::Campaigns)) {
224-
throw $this->createAccessDeniedException('You are not allowed to create campaigns.');
225-
}
226207

227208
/** @var CreateMessageRequest $createMessageRequest */
228209
$createMessageRequest = $this->validator->validate($request, CreateMessageRequest::class);
229-
$data = $this->messageManager->createMessage($createMessageRequest->getDto(), $authUser);
230210

231-
return $this->json($normalizer->normalize($data), Response::HTTP_CREATED);
211+
return $this->json(
212+
$this->campaignService->createMessage($createMessageRequest, $authUser),
213+
Response::HTTP_CREATED
214+
);
232215
}
233216

234217
#[Route('/{messageId}', name: 'update', requirements: ['messageId' => '\d+'], methods: ['PUT'])]
@@ -294,18 +277,14 @@ public function updateMessage(
294277
#[MapEntity(mapping: ['messageId' => 'id'])] ?Message $message = null,
295278
): JsonResponse {
296279
$authUser = $this->requireAuthentication($request);
297-
if (!$authUser->getPrivileges()->has(PrivilegeFlag::Campaigns)) {
298-
throw $this->createAccessDeniedException('You are not allowed to update campaigns.');
299-
}
300280

301-
if (!$message) {
302-
throw $this->createNotFoundException('Campaign not found.');
303-
}
304281
/** @var UpdateMessageRequest $updateMessageRequest */
305282
$updateMessageRequest = $this->validator->validate($request, UpdateMessageRequest::class);
306-
$data = $this->messageManager->updateMessage($updateMessageRequest->getDto(), $message, $authUser);
307283

308-
return $this->json($this->normalizer->normalize($data), Response::HTTP_OK);
284+
return $this->json(
285+
$this->campaignService->updateMessage($updateMessageRequest, $authUser, $message),
286+
Response::HTTP_OK
287+
);
309288
}
310289

311290
#[Route('/{messageId}', name: 'delete', requirements: ['messageId' => '\d+'], methods: ['DELETE'])]
@@ -356,15 +335,8 @@ public function deleteMessage(
356335
#[MapEntity(mapping: ['messageId' => 'id'])] ?Message $message = null
357336
): JsonResponse {
358337
$authUser = $this->requireAuthentication($request);
359-
if (!$authUser->getPrivileges()->has(PrivilegeFlag::Campaigns)) {
360-
throw $this->createAccessDeniedException('You are not allowed to delete campaigns.');
361-
}
362-
363-
if (!$message) {
364-
throw $this->createNotFoundException('Campaign not found.');
365-
}
366338

367-
$this->messageManager->delete($message);
339+
$this->campaignService->deleteMessage($authUser, $message);
368340

369341
return $this->json(null, Response::HTTP_NO_CONTENT);
370342
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Messaging\Service;
6+
7+
use PhpList\Core\Domain\Identity\Model\Administrator;
8+
use PhpList\Core\Domain\Identity\Model\PrivilegeFlag;
9+
use PhpList\Core\Domain\Messaging\Model\Filter\MessageFilter;
10+
use PhpList\Core\Domain\Messaging\Model\Message;
11+
use PhpList\Core\Domain\Messaging\Service\MessageManager;
12+
use PhpList\RestBundle\Common\Service\Provider\PaginatedDataProvider;
13+
use PhpList\RestBundle\Messaging\Request\CreateMessageRequest;
14+
use PhpList\RestBundle\Messaging\Request\UpdateMessageRequest;
15+
use PhpList\RestBundle\Messaging\Serializer\MessageNormalizer;
16+
use Symfony\Component\HttpFoundation\Request;
17+
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
18+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
19+
20+
class CampaignService
21+
{
22+
public function __construct(
23+
private readonly MessageManager $messageManager,
24+
private readonly PaginatedDataProvider $paginatedProvider,
25+
private readonly MessageNormalizer $normalizer,
26+
) {
27+
}
28+
29+
public function getMessages(Request $request, Administrator $administrator): array
30+
{
31+
$filter = (new MessageFilter())->setOwner($administrator);
32+
33+
return $this->paginatedProvider->getPaginatedList($request, $this->normalizer, Message::class, $filter);
34+
}
35+
36+
public function getMessage(Message $message = null): array
37+
{
38+
if (!$message) {
39+
throw new NotFoundHttpException('Campaign not found.');
40+
}
41+
42+
return $this->normalizer->normalize($message);
43+
}
44+
45+
public function createMessage(CreateMessageRequest $createMessageRequest, Administrator $administrator): array
46+
{
47+
if (!$administrator->getPrivileges()->has(PrivilegeFlag::Campaigns)) {
48+
throw new AccessDeniedHttpException('You are not allowed to create campaigns.');
49+
}
50+
51+
$data = $this->messageManager->createMessage($createMessageRequest->getDto(), $administrator);
52+
53+
return $this->normalizer->normalize($data);
54+
}
55+
56+
public function updateMessage(
57+
UpdateMessageRequest $updateMessageRequest,
58+
Administrator $administrator,
59+
Message $message = null
60+
): array {
61+
if (!$administrator->getPrivileges()->has(PrivilegeFlag::Campaigns)) {
62+
throw new AccessDeniedHttpException('You are not allowed to update campaigns.');
63+
}
64+
65+
if (!$message) {
66+
throw new NotFoundHttpException('Campaign not found.');
67+
}
68+
69+
$data = $this->messageManager->updateMessage(
70+
$updateMessageRequest->getDto(),
71+
$message,
72+
$administrator
73+
);
74+
75+
return $this->normalizer->normalize($data);
76+
}
77+
78+
public function deleteMessage(Administrator $administrator, Message $message = null): void
79+
{
80+
if (!$administrator->getPrivileges()->has(PrivilegeFlag::Campaigns)) {
81+
throw new AccessDeniedHttpException('You are not allowed to delete campaigns.');
82+
}
83+
84+
if (!$message) {
85+
throw new NotFoundHttpException('Campaign not found.');
86+
}
87+
88+
$this->messageManager->delete($message);
89+
}
90+
}

src/Statistics/Controller/AnalyticsController.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,27 @@ class AnalyticsController extends BaseController
2525
{
2626
public const BATCH_SIZE = 20;
2727
private AnalyticsService $analyticsService;
28-
private CampaignStatisticsNormalizer $campaignStatisticsNormalizer;
29-
private ViewOpensStatisticsNormalizer $viewOpensStatisticsNormalizer;
28+
private CampaignStatisticsNormalizer $campaignStatsNormalizer;
29+
private ViewOpensStatisticsNormalizer $viewOpensStatsNormalizer;
3030

3131
public function __construct(
3232
Authentication $authentication,
3333
RequestValidator $validator,
3434
AnalyticsService $analyticsService,
35-
CampaignStatisticsNormalizer $campaignStatisticsNormalizer,
36-
ViewOpensStatisticsNormalizer $viewOpensStatisticsNormalizer
35+
CampaignStatisticsNormalizer $campaignStatsNormalizer,
36+
ViewOpensStatisticsNormalizer $viewOpensStatsNormalizer
3737
) {
3838
parent::__construct($authentication, $validator);
3939
$this->analyticsService = $analyticsService;
40-
$this->campaignStatisticsNormalizer = $campaignStatisticsNormalizer;
41-
$this->viewOpensStatisticsNormalizer = $viewOpensStatisticsNormalizer;
40+
$this->campaignStatsNormalizer = $campaignStatsNormalizer;
41+
$this->viewOpensStatsNormalizer = $viewOpensStatsNormalizer;
4242
}
4343

4444
#[Route('/campaigns', name: 'campaign_statistics', methods: ['GET'])]
4545
#[OA\Get(
4646
path: '/analytics/campaigns',
47-
description: 'Returns statistics overview for campaigns.',
47+
description: '🚧 **Status: Beta** – This method is under development. Avoid using in production. ' .
48+
'Returns statistics overview for campaigns.',
4849
summary: 'Gets campaign statistics.',
4950
tags: ['analytics'],
5051
parameters: [
@@ -106,7 +107,7 @@ public function getCampaignStatistics(Request $request): JsonResponse
106107
$lastId = (int) $request->query->get('after_id', 0);
107108

108109
$data = $this->analyticsService->getCampaignStatistics($limit, $lastId);
109-
$normalizedData = $this->campaignStatisticsNormalizer->normalize($data, null, [
110+
$normalizedData = $this->campaignStatsNormalizer->normalize($data, null, [
110111
'limit' => $limit,
111112
'campaign_statistics' => true,
112113
]);
@@ -117,7 +118,8 @@ public function getCampaignStatistics(Request $request): JsonResponse
117118
#[Route('/view-opens', name: 'view_opens_statistics', methods: ['GET'])]
118119
#[OA\Get(
119120
path: '/analytics/view-opens',
120-
description: 'Returns statistics for view opens.',
121+
description: '🚧 **Status: Beta** – This method is under development. Avoid using in production. ' .
122+
'Returns statistics for view opens.',
121123
summary: 'Gets view opens statistics.',
122124
tags: ['analytics'],
123125
parameters: [
@@ -179,7 +181,7 @@ public function getViewOpensStatistics(Request $request): JsonResponse
179181
$lastId = (int) $request->query->get('after_id', 0);
180182

181183
$data = $this->analyticsService->getViewOpensStatistics($limit, $lastId);
182-
$normalizedData = $this->viewOpensStatisticsNormalizer->normalize($data, null, [
184+
$normalizedData = $this->viewOpensStatsNormalizer->normalize($data, null, [
183185
'view_opens_statistics' => true,
184186
'limit' => $limit
185187
]);
@@ -190,7 +192,8 @@ public function getViewOpensStatistics(Request $request): JsonResponse
190192
#[Route('/domains/top', name: 'top_domains', methods: ['GET'])]
191193
#[OA\Get(
192194
path: '/analytics/domains/top',
193-
description: 'Returns statistics for the top domains with more than 5 subscribers.',
195+
description: '🚧 **Status: Beta** – This method is under development. Avoid using in production. ' .
196+
'Returns statistics for the top domains with more than 5 subscribers.',
194197
summary: 'Gets top domains statistics.',
195198
tags: ['analytics'],
196199
parameters: [
@@ -265,7 +268,8 @@ public function getTopDomains(Request $request): JsonResponse
265268
#[Route('/domains/confirmation', name: 'domain_confirmation_statistics', methods: ['GET'])]
266269
#[OA\Get(
267270
path: '/analytics/domains/confirmation',
268-
description: 'Returns statistics for domains showing confirmation status.',
271+
description: '🚧 **Status: Beta** – This method is under development. Avoid using in production. ' .
272+
'Returns statistics for domains showing confirmation status.',
269273
summary: 'Gets domain confirmation statistics.',
270274
tags: ['analytics'],
271275
parameters: [
@@ -363,7 +367,8 @@ public function getDomainConfirmationStatistics(Request $request): JsonResponse
363367
#[Route('/local-parts/top', name: 'top_local_parts', methods: ['GET'])]
364368
#[OA\Get(
365369
path: '/analytics/local-parts/top',
366-
description: 'Returns statistics for the top local-parts of email addresses.',
370+
description: '🚧 **Status: Beta** – This method is under development. Avoid using in production. ' .
371+
'Returns statistics for the top local-parts of email addresses.',
367372
summary: 'Gets top local-parts statistics.',
368373
tags: ['analytics'],
369374
parameters: [

0 commit comments

Comments
 (0)