|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\RestBundle\Subscription\Controller; |
| 6 | + |
| 7 | +use OpenApi\Attributes as OA; |
| 8 | +use PhpList\Core\Domain\Identity\Model\PrivilegeFlag; |
| 9 | +use PhpList\Core\Domain\Subscription\Service\Manager\SubscribePageManager; |
| 10 | +use PhpList\Core\Security\Authentication; |
| 11 | +use PhpList\RestBundle\Common\Controller\BaseController; |
| 12 | +use PhpList\RestBundle\Common\Validator\RequestValidator; |
| 13 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 14 | +use Symfony\Component\HttpFoundation\Request; |
| 15 | +use Symfony\Component\HttpFoundation\Response; |
| 16 | +use Symfony\Component\Routing\Attribute\Route; |
| 17 | + |
| 18 | +#[Route('/subscribe-pages', name: 'subscribe_pages_')] |
| 19 | +class SubscribePageController extends BaseController |
| 20 | +{ |
| 21 | + public function __construct( |
| 22 | + Authentication $authentication, |
| 23 | + RequestValidator $validator, |
| 24 | + private readonly SubscribePageManager $subscribePageManager, |
| 25 | + ) { |
| 26 | + parent::__construct($authentication, $validator); |
| 27 | + } |
| 28 | + |
| 29 | + #[Route('/{id}', name: 'get', requirements: ['id' => '\\d+'], methods: ['GET'])] |
| 30 | + #[OA\Get( |
| 31 | + path: '/api/v2/subscribe-pages/{id}', |
| 32 | + description: '🚧 **Status: Beta** – This method is under development. Avoid using in production.', |
| 33 | + summary: 'Get subscribe page', |
| 34 | + tags: ['subscriptions'], |
| 35 | + parameters: [ |
| 36 | + new OA\Parameter( |
| 37 | + name: 'php-auth-pw', |
| 38 | + description: 'Session key obtained from login', |
| 39 | + in: 'header', |
| 40 | + required: true, |
| 41 | + schema: new OA\Schema(type: 'string') |
| 42 | + ), |
| 43 | + new OA\Parameter( |
| 44 | + name: 'id', |
| 45 | + description: 'Subscribe page ID', |
| 46 | + in: 'path', |
| 47 | + required: true, |
| 48 | + schema: new OA\Schema(type: 'integer') |
| 49 | + ) |
| 50 | + ], |
| 51 | + responses: [ |
| 52 | + new OA\Response( |
| 53 | + response: 200, |
| 54 | + description: 'Success', |
| 55 | + content: new OA\JsonContent( |
| 56 | + properties: [ |
| 57 | + new OA\Property(property: 'id', type: 'integer'), |
| 58 | + new OA\Property(property: 'title', type: 'string'), |
| 59 | + new OA\Property(property: 'active', type: 'boolean'), |
| 60 | + new OA\Property(property: 'owner_id', type: 'integer', nullable: true), |
| 61 | + ] |
| 62 | + ), |
| 63 | + ), |
| 64 | + new OA\Response( |
| 65 | + response: 403, |
| 66 | + description: 'Failure', |
| 67 | + content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse') |
| 68 | + ), |
| 69 | + new OA\Response( |
| 70 | + response: 404, |
| 71 | + description: 'Not Found', |
| 72 | + content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse') |
| 73 | + ), |
| 74 | + ] |
| 75 | + )] |
| 76 | + public function getPage(Request $request, int $id): JsonResponse |
| 77 | + { |
| 78 | + $admin = $this->requireAuthentication($request); |
| 79 | + if (!$admin->getPrivileges()->has(PrivilegeFlag::Subscribers)) { |
| 80 | + throw $this->createAccessDeniedException('You are not allowed to view subscribe pages.'); |
| 81 | + } |
| 82 | + |
| 83 | + $page = $this->subscribePageManager->getPage($id); |
| 84 | + |
| 85 | + return $this->json([ |
| 86 | + 'id' => $page->getId(), |
| 87 | + 'title' => $page->getTitle(), |
| 88 | + 'active' => $page->isActive(), |
| 89 | + 'owner_id' => $page->getOwner()?->getId(), |
| 90 | + ], Response::HTTP_OK); |
| 91 | + } |
| 92 | + |
| 93 | + #[Route('', name: 'create', methods: ['POST'])] |
| 94 | + #[OA\Post( |
| 95 | + path: '/api/v2/subscribe-pages', |
| 96 | + description: '🚧 **Status: Beta** – This method is under development. Avoid using in production.', |
| 97 | + summary: 'Create subscribe page', |
| 98 | + requestBody: new OA\RequestBody( |
| 99 | + required: true, |
| 100 | + content: new OA\JsonContent( |
| 101 | + properties: [ |
| 102 | + new OA\Property(property: 'title', type: 'string'), |
| 103 | + new OA\Property(property: 'active', type: 'boolean', nullable: true), |
| 104 | + ] |
| 105 | + ) |
| 106 | + ), |
| 107 | + tags: ['subscriptions'], |
| 108 | + parameters: [ |
| 109 | + new OA\Parameter( |
| 110 | + name: 'php-auth-pw', |
| 111 | + description: 'Session key obtained from login', |
| 112 | + in: 'header', |
| 113 | + required: true, |
| 114 | + schema: new OA\Schema(type: 'string') |
| 115 | + ) |
| 116 | + ], |
| 117 | + responses: [ |
| 118 | + new OA\Response( |
| 119 | + response: 201, |
| 120 | + description: 'Created', |
| 121 | + content: new OA\JsonContent( |
| 122 | + properties: [ |
| 123 | + new OA\Property(property: 'id', type: 'integer'), |
| 124 | + new OA\Property(property: 'title', type: 'string'), |
| 125 | + new OA\Property(property: 'active', type: 'boolean'), |
| 126 | + new OA\Property(property: 'owner_id', type: 'integer', nullable: true), |
| 127 | + ] |
| 128 | + ) |
| 129 | + ), |
| 130 | + new OA\Response( |
| 131 | + response: 403, |
| 132 | + description: 'Failure', |
| 133 | + content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse') |
| 134 | + ), |
| 135 | + new OA\Response( |
| 136 | + response: 422, |
| 137 | + description: 'Validation failed', |
| 138 | + content: new OA\JsonContent(ref: '#/components/schemas/ValidationErrorResponse') |
| 139 | + ) |
| 140 | + ] |
| 141 | + )] |
| 142 | + public function createPage(Request $request): JsonResponse |
| 143 | + { |
| 144 | + $admin = $this->requireAuthentication($request); |
| 145 | + if (!$admin->getPrivileges()->has(PrivilegeFlag::Subscribers)) { |
| 146 | + throw $this->createAccessDeniedException('You are not allowed to create subscribe pages.'); |
| 147 | + } |
| 148 | + |
| 149 | + $data = json_decode($request->getContent(), true) ?: []; |
| 150 | + $title = isset($data['title']) ? trim((string)$data['title']) : ''; |
| 151 | + $active = isset($data['active']) ? (bool)$data['active'] : false; |
| 152 | + |
| 153 | + if ($title === '') { |
| 154 | + return $this->json([ |
| 155 | + 'errors' => [ |
| 156 | + ['field' => 'title', 'message' => 'This field is required.'] |
| 157 | + ] |
| 158 | + ], Response::HTTP_UNPROCESSABLE_ENTITY); |
| 159 | + } |
| 160 | + |
| 161 | + $page = $this->subscribePageManager->createPage($title, $active, $admin); |
| 162 | + |
| 163 | + return $this->json([ |
| 164 | + 'id' => $page->getId(), |
| 165 | + 'title' => $page->getTitle(), |
| 166 | + 'active' => $page->isActive(), |
| 167 | + 'owner_id' => $page->getOwner()?->getId(), |
| 168 | + ], Response::HTTP_CREATED); |
| 169 | + } |
| 170 | +} |
0 commit comments