ernestwisniewski/kbin

View on GitHub
src/Controller/Api/Magazine/Admin/MagazineUpdateThemeApi.php

Summary

Maintainability
A
45 mins
Test Coverage
<?php

// SPDX-FileCopyrightText: 2023 /kbin contributors <https://kbin.pub/>
//
// SPDX-License-Identifier: AGPL-3.0-only

declare(strict_types=1);

namespace App\Controller\Api\Magazine\Admin;

use App\Controller\Api\Magazine\MagazineBaseApi;
use App\DTO\ImageUploadDto;
use App\Entity\Magazine;
use App\Factory\ImageFactory;
use App\Kbin\Magazine\DTO\MagazineThemeDto;
use App\Kbin\Magazine\DTO\MagazineThemeRequestDto;
use App\Kbin\Magazine\DTO\MagazineThemeResponseDto;
use App\Kbin\Magazine\Factory\MagazineFactory;
use App\Kbin\Magazine\MagazineThemeChange;
use App\Service\ImageManager;
use Nelmio\ApiDocBundle\Annotation\Model;
use Nelmio\ApiDocBundle\Annotation\Security;
use OpenApi\Attributes as OA;
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Validator\Validator\ValidatorInterface;

class MagazineUpdateThemeApi extends MagazineBaseApi
{
    #[OA\Response(
        response: 201,
        description: 'Theme updated',
        content: new Model(type: MagazineThemeResponseDto::class),
        headers: [
            new OA\Header(
                header: 'X-RateLimit-Remaining',
                schema: new OA\Schema(type: 'integer'),
                description: 'Number of requests left until you will be rate limited'
            ),
            new OA\Header(
                header: 'X-RateLimit-Retry-After',
                schema: new OA\Schema(type: 'integer'),
                description: 'Unix timestamp to retry the request after'
            ),
            new OA\Header(
                header: 'X-RateLimit-Limit',
                schema: new OA\Schema(type: 'integer'),
                description: 'Number of requests available'
            ),
        ]
    )]
    #[OA\Response(
        response: 400,
        description: 'Invalid parameters',
        content: new OA\JsonContent(ref: new Model(type: \App\Schema\Errors\BadRequestErrorSchema::class))
    )]
    #[OA\Response(
        response: 401,
        description: 'Permission denied due to missing or expired token',
        content: new OA\JsonContent(ref: new Model(type: \App\Schema\Errors\UnauthorizedErrorSchema::class))
    )]
    #[OA\Response(
        response: 403,
        description: 'You do not have permission to update this magazine',
        content: new OA\JsonContent(ref: new Model(type: \App\Schema\Errors\ForbiddenErrorSchema::class))
    )]
    #[OA\Response(
        response: 404,
        description: 'Magazine not found',
        content: new OA\JsonContent(ref: new Model(type: \App\Schema\Errors\NotFoundErrorSchema::class))
    )]
    #[OA\Response(
        response: 429,
        description: 'You are being rate limited',
        content: new OA\JsonContent(ref: new Model(type: \App\Schema\Errors\TooManyRequestsErrorSchema::class)),
        headers: [
            new OA\Header(
                header: 'X-RateLimit-Remaining',
                schema: new OA\Schema(type: 'integer'),
                description: 'Number of requests left until you will be rate limited'
            ),
            new OA\Header(
                header: 'X-RateLimit-Retry-After',
                schema: new OA\Schema(type: 'integer'),
                description: 'Unix timestamp to retry the request after'
            ),
            new OA\Header(
                header: 'X-RateLimit-Limit',
                schema: new OA\Schema(type: 'integer'),
                description: 'Number of requests available'
            ),
        ]
    )]
    #[OA\Parameter(
        name: 'magazine_id',
        in: 'path',
        description: 'The id of the magazine to update',
        schema: new OA\Schema(type: 'integer'),
    )]
    #[OA\RequestBody(content: new OA\MediaType(
        'multipart/form-data',
        schema: new OA\Schema(
            ref: new Model(
                type: MagazineThemeRequestDto::class,
                groups: [
                    ImageUploadDto::IMAGE_UPLOAD_NO_ALT,
                    'common',
                ]
            )
        ),
        encoding: [
            'imageUpload' => [
                'contentType' => ImageManager::IMAGE_MIMETYPE_STR,
            ],
        ]
    ))]
    #[OA\Tag(name: 'moderation/magazine/owner')]
    #[Security(name: 'oauth2', scopes: ['moderate:magazine_admin:theme'])]
    #[IsGranted('ROLE_OAUTH2_MODERATE:MAGAZINE_ADMIN:THEME')]
    #[IsGranted('edit', subject: 'magazine')]
    /**
     * Update the magazine's theme.
     */
    public function __invoke(
        #[MapEntity(id: 'magazine_id')]
        Magazine $magazine,
        MagazineThemeChange $magazineThemeChange,
        MagazineFactory $magazineFactory,
        ImageFactory $imageFactory,
        ValidatorInterface $validator,
        RateLimiterFactory $apiModerateLimiter
    ): JsonResponse {
        $headers = $this->rateLimit($apiModerateLimiter);

        $dto = new MagazineThemeDto($magazine);
        $dto = $this->deserializeThemeFromForm($dto);

        try {
            $icon = $this->handleUploadedImage();
            $dto->icon = $imageFactory->createDto($icon);
        } catch (BadRequestHttpException $e) {
            $dto->icon = null; // Todo: add an API to remove the icon rather than only replace
        }

        $errors = $validator->validate($dto);
        if (0 < \count($errors)) {
            throw new BadRequestHttpException((string) $errors);
        }

        $magazineThemeChange($dto);

        $imageDto = $magazine->icon ? $this->imageFactory->createDto($magazine->icon) : null;
        $dto = MagazineThemeResponseDto::create(
            $magazineFactory->createDto($magazine),
            $magazine->customCss,
            $imageDto
        );

        return new JsonResponse(
            $dto,
            headers: $headers
        );
    }
}