Skip to content

Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️

1.3 KB
Raw Blame
<?php

namespace HiEvents\Http\Actions\Users;

use HiEvents\Http\Actions\BaseAction;
use HiEvents\Http\Attributes\AllowedForReadOnlyUsers;
use HiEvents\Services\Application\Handlers\User\ResendEmailConfirmationHandler;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cache;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;

#[AllowedForReadOnlyUsers]
class ResendEmailConfirmationAction extends BaseAction
{
    public function __construct(
        private readonly ResendEmailConfirmationHandler $resendEmailConfirmationHandler,
    ) {}

    public function __invoke(int $userId): Response
    {
        $user = $this->getAuthenticatedUser();
        $cacheKey = 'resend_email_confirmation:'.$user->getId();

        if (Cache::has($cacheKey)) {
            $remainingSeconds = Cache::get($cacheKey) - now()->timestamp;
            throw new TooManyRequestsHttpException($remainingSeconds, __(
                'Please wait :seconds seconds before requesting another code.', [
                    'seconds' => $remainingSeconds,
                ]));
        }

        Cache::put($cacheKey, now()->addSeconds(30)->timestamp, 30);

        $this->resendEmailConfirmationHandler->handle($user, $this->getAuthenticatedAccountId());

        return $this->noContentResponse();
    }
}