|
| 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\Repository\Messaging\TemplateRepository; |
| 9 | +use PhpList\Core\Security\Authentication; |
| 10 | +use PhpList\RestBundle\Controller\Traits\AuthenticationTrait; |
| 11 | +use PhpList\RestBundle\Serializer\TemplateNormalizer; |
| 12 | +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 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 | +/** |
| 19 | + * This controller provides REST API to manage templates. |
| 20 | + * |
| 21 | + * @author Tatevik Grigoryan <[email protected]> |
| 22 | + */ |
| 23 | +#[Route('/templates')] |
| 24 | +class TemplateController extends AbstractController |
| 25 | +{ |
| 26 | + use AuthenticationTrait; |
| 27 | + |
| 28 | + private TemplateRepository $templateRepository; |
| 29 | + private TemplateNormalizer $normalizer; |
| 30 | + |
| 31 | + public function __construct( |
| 32 | + Authentication $authentication, |
| 33 | + TemplateRepository $templateRepository, |
| 34 | + TemplateNormalizer $normalizer, |
| 35 | + ) { |
| 36 | + $this->authentication = $authentication; |
| 37 | + $this->templateRepository = $templateRepository; |
| 38 | + $this->normalizer = $normalizer; |
| 39 | + } |
| 40 | + |
| 41 | + #[Route('', name: 'get_templates', methods: ['GET'])] |
| 42 | + #[OA\Get( |
| 43 | + path: '/templates', |
| 44 | + description: 'Returns a JSON list of all templates.', |
| 45 | + summary: 'Gets a list of all templates.', |
| 46 | + tags: ['templates'], |
| 47 | + parameters: [ |
| 48 | + new OA\Parameter( |
| 49 | + name: 'session', |
| 50 | + description: 'Session ID obtained from authentication', |
| 51 | + in: 'header', |
| 52 | + required: true, |
| 53 | + schema: new OA\Schema( |
| 54 | + type: 'string' |
| 55 | + ) |
| 56 | + ) |
| 57 | + ], |
| 58 | + responses: [ |
| 59 | + new OA\Response( |
| 60 | + response: 200, |
| 61 | + description: 'Success', |
| 62 | + content: new OA\JsonContent( |
| 63 | + type: 'array', |
| 64 | + items: new OA\Items(ref: '#/components/schemas/Template') |
| 65 | + ) |
| 66 | + ), |
| 67 | + new OA\Response( |
| 68 | + response: 403, |
| 69 | + description: 'Failure', |
| 70 | + content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse') |
| 71 | + ) |
| 72 | + ] |
| 73 | + )] |
| 74 | + public function getTemplates(Request $request): JsonResponse |
| 75 | + { |
| 76 | + $this->requireAuthentication($request); |
| 77 | + $data = $this->templateRepository->findAll(); |
| 78 | + |
| 79 | + $normalized = array_map(function ($item) { |
| 80 | + return $this->normalizer->normalize($item); |
| 81 | + }, $data); |
| 82 | + |
| 83 | + return new JsonResponse($normalized, Response::HTTP_OK); |
| 84 | + } |
| 85 | +} |
0 commit comments