Skip to content

Commit fcc12db

Browse files
committed
ISSUE-345: split controller
1 parent 7ee40e8 commit fcc12db

File tree

5 files changed

+273
-223
lines changed

5 files changed

+273
-223
lines changed

src/Controller/AdministratorController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct(
4343
}
4444

4545
#[Route('', name: 'get_administrators', methods: ['GET'])]
46-
#[OA\Post(
46+
#[OA\Get(
4747
path: '/administrators',
4848
description: 'Get list of administrators.',
4949
summary: 'Get Administrators',
+179
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Controller;
6+
7+
use OpenApi\Attributes as OA;
8+
use PhpList\Core\Domain\Filter\SubscriberFilter;
9+
use PhpList\Core\Domain\Model\Subscription\Subscriber;
10+
use PhpList\Core\Domain\Model\Subscription\SubscriberList;
11+
use PhpList\Core\Security\Authentication;
12+
use PhpList\RestBundle\Serializer\SubscriberNormalizer;
13+
use PhpList\RestBundle\Service\Provider\PaginatedDataProvider;
14+
use PhpList\RestBundle\Validator\RequestValidator;
15+
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
16+
use Symfony\Component\HttpFoundation\JsonResponse;
17+
use Symfony\Component\HttpFoundation\Request;
18+
use Symfony\Component\HttpFoundation\Response;
19+
use Symfony\Component\Routing\Attribute\Route;
20+
21+
#[Route('/lists')]
22+
class ListMembersController extends BaseController
23+
{
24+
private SubscriberNormalizer $subscriberNormalizer;
25+
private PaginatedDataProvider $paginatedProvider;
26+
27+
public function __construct(
28+
Authentication $authentication,
29+
RequestValidator $validator,
30+
SubscriberNormalizer $subscriberNormalizer,
31+
PaginatedDataProvider $paginatedProvider,
32+
) {
33+
parent::__construct($authentication, $validator);
34+
$this->subscriberNormalizer = $subscriberNormalizer;
35+
$this->paginatedProvider = $paginatedProvider;
36+
}
37+
38+
#[Route('/{listId}/subscribers', name: 'get_subscriber_from_list', methods: ['GET'])]
39+
#[OA\Get(
40+
path: '/lists/{listId}/subscribers',
41+
description: 'Returns a JSON list of all subscribers for a subscriber list.',
42+
summary: 'Gets a list of all subscribers of a subscriber list.',
43+
tags: ['subscriptions'],
44+
parameters: [
45+
new OA\Parameter(
46+
name: 'session',
47+
description: 'Session ID obtained from authentication',
48+
in: 'header',
49+
required: true,
50+
schema: new OA\Schema(type: 'string')
51+
),
52+
new OA\Parameter(
53+
name: 'listId',
54+
description: 'List ID',
55+
in: 'path',
56+
required: true,
57+
schema: new OA\Schema(type: 'string')
58+
),
59+
new OA\Parameter(
60+
name: 'after_id',
61+
description: 'Last id (starting from 0)',
62+
in: 'query',
63+
required: false,
64+
schema: new OA\Schema(type: 'integer', default: 1, minimum: 1)
65+
),
66+
new OA\Parameter(
67+
name: 'limit',
68+
description: 'Number of results per page',
69+
in: 'query',
70+
required: false,
71+
schema: new OA\Schema(type: 'integer', default: 25, maximum: 100, minimum: 1)
72+
)
73+
],
74+
responses: [
75+
new OA\Response(
76+
response: 200,
77+
description: 'Success',
78+
content: new OA\JsonContent(
79+
properties: [
80+
new OA\Property(
81+
property: 'items',
82+
type: 'array',
83+
items: new OA\Items(ref: '#/components/schemas/Subscriber')
84+
),
85+
new OA\Property(property: 'pagination', ref: '#/components/schemas/CursorPagination')
86+
],
87+
type: 'object'
88+
)
89+
),
90+
new OA\Response(
91+
response: 403,
92+
description: 'Failure',
93+
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
94+
),
95+
new OA\Response(
96+
response: 404,
97+
description: 'Failure',
98+
content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse')
99+
)
100+
]
101+
)]
102+
public function getListMembers(
103+
Request $request,
104+
#[MapEntity(mapping: ['listId' => 'id'])] ?SubscriberList $list = null,
105+
): JsonResponse {
106+
$this->requireAuthentication($request);
107+
108+
if (!$list) {
109+
throw $this->createNotFoundException('Subscriber list not found.');
110+
}
111+
112+
return $this->json(
113+
$this->paginatedProvider->getPaginatedList(
114+
$request,
115+
$this->subscriberNormalizer,
116+
Subscriber::class,
117+
(new SubscriberFilter())->setListId($list->getId())
118+
),
119+
Response::HTTP_OK
120+
);
121+
}
122+
123+
#[Route('/{listId}/subscribers/count', name: 'get_subscribers_count_from_list', methods: ['GET'])]
124+
#[OA\Get(
125+
path: '/lists/{listId}/count',
126+
description: 'Returns a count of all subscribers in a given list.',
127+
summary: 'Gets the total number of subscribers of a list',
128+
tags: ['subscriptions'],
129+
parameters: [
130+
new OA\Parameter(
131+
name: 'session',
132+
description: 'Session ID obtained from authentication',
133+
in: 'header',
134+
required: true,
135+
schema: new OA\Schema(type: 'string')
136+
),
137+
new OA\Parameter(
138+
name: 'listId',
139+
description: 'List ID',
140+
in: 'path',
141+
required: true,
142+
schema: new OA\Schema(type: 'string')
143+
)
144+
],
145+
responses: [
146+
new OA\Response(
147+
response: 200,
148+
description: 'Success',
149+
content: new OA\JsonContent(
150+
properties: [
151+
new OA\Property(
152+
property: 'subscribers_count',
153+
type: 'integer',
154+
example: 42
155+
)
156+
],
157+
type: 'object'
158+
)
159+
),
160+
new OA\Response(
161+
response: 403,
162+
description: 'Failure',
163+
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
164+
)
165+
]
166+
)]
167+
public function getSubscribersCount(
168+
Request $request,
169+
#[MapEntity(mapping: ['listId' => 'id'])] ?SubscriberList $list = null,
170+
): JsonResponse {
171+
$this->requireAuthentication($request);
172+
173+
if (!$list) {
174+
throw $this->createNotFoundException('Subscriber list not found.');
175+
}
176+
177+
return $this->json(['subscribers_count' => count($list->getSubscribers())], Response::HTTP_OK);
178+
}
179+
}

src/Controller/SubscriptionController.php

-152
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,11 @@
55
namespace PhpList\RestBundle\Controller;
66

77
use OpenApi\Attributes as OA;
8-
use PhpList\Core\Domain\Filter\SubscriberFilter;
9-
use PhpList\Core\Domain\Model\Subscription\Subscriber;
108
use PhpList\Core\Domain\Model\Subscription\SubscriberList;
119
use PhpList\Core\Security\Authentication;
1210
use PhpList\RestBundle\Entity\Request\SubscriptionRequest;
13-
use PhpList\RestBundle\Serializer\SubscriberNormalizer;
1411
use PhpList\RestBundle\Serializer\SubscriptionNormalizer;
1512
use PhpList\RestBundle\Service\Manager\SubscriptionManager;
16-
use PhpList\RestBundle\Service\Provider\PaginatedDataProvider;
1713
use PhpList\RestBundle\Validator\RequestValidator;
1814
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
1915
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -30,165 +26,17 @@
3026
class SubscriptionController extends BaseController
3127
{
3228
private SubscriptionManager $subscriptionManager;
33-
private SubscriberNormalizer $subscriberNormalizer;
3429
private SubscriptionNormalizer $subscriptionNormalizer;
35-
private PaginatedDataProvider $paginatedProvider;
3630

3731
public function __construct(
3832
Authentication $authentication,
3933
RequestValidator $validator,
4034
SubscriptionManager $subscriptionManager,
41-
SubscriberNormalizer $subscriberNormalizer,
4235
SubscriptionNormalizer $subscriptionNormalizer,
43-
PaginatedDataProvider $paginatedProvider,
4436
) {
4537
parent::__construct($authentication, $validator);
4638
$this->subscriptionManager = $subscriptionManager;
47-
$this->subscriberNormalizer = $subscriberNormalizer;
4839
$this->subscriptionNormalizer = $subscriptionNormalizer;
49-
$this->paginatedProvider = $paginatedProvider;
50-
}
51-
52-
#[Route('/{listId}/subscribers', name: 'get_subscriber_from_list', methods: ['GET'])]
53-
#[OA\Get(
54-
path: '/lists/{listId}/subscribers',
55-
description: 'Returns a JSON list of all subscribers for a subscriber list.',
56-
summary: 'Gets a list of all subscribers of a subscriber list.',
57-
tags: ['subscriptions'],
58-
parameters: [
59-
new OA\Parameter(
60-
name: 'session',
61-
description: 'Session ID obtained from authentication',
62-
in: 'header',
63-
required: true,
64-
schema: new OA\Schema(type: 'string')
65-
),
66-
new OA\Parameter(
67-
name: 'listId',
68-
description: 'List ID',
69-
in: 'path',
70-
required: true,
71-
schema: new OA\Schema(type: 'string')
72-
),
73-
new OA\Parameter(
74-
name: 'after_id',
75-
description: 'Last id (starting from 0)',
76-
in: 'query',
77-
required: false,
78-
schema: new OA\Schema(type: 'integer', default: 1, minimum: 1)
79-
),
80-
new OA\Parameter(
81-
name: 'limit',
82-
description: 'Number of results per page',
83-
in: 'query',
84-
required: false,
85-
schema: new OA\Schema(type: 'integer', default: 25, maximum: 100, minimum: 1)
86-
)
87-
],
88-
responses: [
89-
new OA\Response(
90-
response: 200,
91-
description: 'Success',
92-
content: new OA\JsonContent(
93-
properties: [
94-
new OA\Property(
95-
property: 'items',
96-
type: 'array',
97-
items: new OA\Items(ref: '#/components/schemas/Subscriber')
98-
),
99-
new OA\Property(property: 'pagination', ref: '#/components/schemas/CursorPagination')
100-
],
101-
type: 'object'
102-
)
103-
),
104-
new OA\Response(
105-
response: 403,
106-
description: 'Failure',
107-
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
108-
),
109-
new OA\Response(
110-
response: 404,
111-
description: 'Failure',
112-
content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse')
113-
)
114-
]
115-
)]
116-
public function getListMembers(
117-
Request $request,
118-
#[MapEntity(mapping: ['listId' => 'id'])] ?SubscriberList $list = null,
119-
): JsonResponse {
120-
$this->requireAuthentication($request);
121-
122-
if (!$list) {
123-
throw $this->createNotFoundException('Subscriber list not found.');
124-
}
125-
126-
return $this->json(
127-
$this->paginatedProvider->getPaginatedList(
128-
$request,
129-
$this->subscriberNormalizer,
130-
Subscriber::class,
131-
(new SubscriberFilter())->setListId($list->getId())
132-
),
133-
Response::HTTP_OK
134-
);
135-
}
136-
137-
#[Route('/{listId}/subscribers/count', name: 'get_subscribers_count_from_list', methods: ['GET'])]
138-
#[OA\Get(
139-
path: '/lists/{listId}/count',
140-
description: 'Returns a count of all subscribers in a given list.',
141-
summary: 'Gets the total number of subscribers of a list',
142-
tags: ['subscriptions'],
143-
parameters: [
144-
new OA\Parameter(
145-
name: 'session',
146-
description: 'Session ID obtained from authentication',
147-
in: 'header',
148-
required: true,
149-
schema: new OA\Schema(type: 'string')
150-
),
151-
new OA\Parameter(
152-
name: 'listId',
153-
description: 'List ID',
154-
in: 'path',
155-
required: true,
156-
schema: new OA\Schema(type: 'string')
157-
)
158-
],
159-
responses: [
160-
new OA\Response(
161-
response: 200,
162-
description: 'Success',
163-
content: new OA\JsonContent(
164-
properties: [
165-
new OA\Property(
166-
property: 'subscribers_count',
167-
type: 'integer',
168-
example: 42
169-
)
170-
],
171-
type: 'object'
172-
)
173-
),
174-
new OA\Response(
175-
response: 403,
176-
description: 'Failure',
177-
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
178-
)
179-
]
180-
)]
181-
public function getSubscribersCount(
182-
Request $request,
183-
#[MapEntity(mapping: ['listId' => 'id'])] ?SubscriberList $list = null,
184-
): JsonResponse {
185-
$this->requireAuthentication($request);
186-
187-
if (!$list) {
188-
throw $this->createNotFoundException('Subscriber list not found.');
189-
}
190-
191-
return $this->json(['subscribers_count' => count($list->getSubscribers())], Response::HTTP_OK);
19240
}
19341

19442
#[Route('/{listId}/subscribers', name: 'create_subscription', methods: ['POST'])]

0 commit comments

Comments
 (0)