|
5 | 5 | namespace PhpList\RestBundle\Controller; |
6 | 6 |
|
7 | 7 | use OpenApi\Attributes as OA; |
8 | | -use PhpList\Core\Domain\Filter\SubscriberFilter; |
9 | | -use PhpList\Core\Domain\Model\Subscription\Subscriber; |
10 | 8 | use PhpList\Core\Domain\Model\Subscription\SubscriberList; |
11 | 9 | use PhpList\Core\Security\Authentication; |
12 | 10 | use PhpList\RestBundle\Entity\Request\SubscriptionRequest; |
13 | | -use PhpList\RestBundle\Serializer\SubscriberNormalizer; |
14 | 11 | use PhpList\RestBundle\Serializer\SubscriptionNormalizer; |
15 | 12 | use PhpList\RestBundle\Service\Manager\SubscriptionManager; |
16 | | -use PhpList\RestBundle\Service\Provider\PaginatedDataProvider; |
17 | 13 | use PhpList\RestBundle\Validator\RequestValidator; |
18 | 14 | use Symfony\Bridge\Doctrine\Attribute\MapEntity; |
19 | 15 | use Symfony\Component\HttpFoundation\JsonResponse; |
|
30 | 26 | class SubscriptionController extends BaseController |
31 | 27 | { |
32 | 28 | private SubscriptionManager $subscriptionManager; |
33 | | - private SubscriberNormalizer $subscriberNormalizer; |
34 | 29 | private SubscriptionNormalizer $subscriptionNormalizer; |
35 | | - private PaginatedDataProvider $paginatedProvider; |
36 | 30 |
|
37 | 31 | public function __construct( |
38 | 32 | Authentication $authentication, |
39 | 33 | RequestValidator $validator, |
40 | 34 | SubscriptionManager $subscriptionManager, |
41 | | - SubscriberNormalizer $subscriberNormalizer, |
42 | 35 | SubscriptionNormalizer $subscriptionNormalizer, |
43 | | - PaginatedDataProvider $paginatedProvider, |
44 | 36 | ) { |
45 | 37 | parent::__construct($authentication, $validator); |
46 | 38 | $this->subscriptionManager = $subscriptionManager; |
47 | | - $this->subscriberNormalizer = $subscriberNormalizer; |
48 | 39 | $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); |
192 | 40 | } |
193 | 41 |
|
194 | 42 | #[Route('/{listId}/subscribers', name: 'create_subscription', methods: ['POST'])] |
|
0 commit comments