Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem when creating password token in eventSubscriber #146

Open
D3xime opened this issue Nov 27, 2024 · 0 comments
Open

Problem when creating password token in eventSubscriber #146

D3xime opened this issue Nov 27, 2024 · 0 comments

Comments

@D3xime
Copy link

D3xime commented Nov 27, 2024

Hello everyone, i encounter some problems. I want to genrate a token when an user change status. To achieve that i used an eventSubscriber like below. But when i try to do it i got a fatal Error because i used too much RAM. Any idea how I can achieve that ? I am pretty new with this bundle and any help will be pleased. If i miss something feel free to say it to me

<br /> <b>Fatal error</b>: Allowed memory size of 536870912 bytes exhausted (tried to allocate 20480 bytes) in <b>/var/www/app/vendor/doctrine/doctrine-bundle/src/Middleware/BacktraceDebugDataHolder.php</b> on line <b>45</b><br /> <br /> <b>Fatal error</b>: Allowed memory size of 536870912 bytes exhausted (tried to allocate 65536 bytes) in <b>/var/www/app/vendor/symfony/var-dumper/Cloner/Data.php</b> on line <b>177</b><br />
<?php

namespace App\EventSubscriber;

use App\Entity\Recruiter;
use App\Service\MailService;
use CoopTilleuls\ForgotPasswordBundle\Manager\PasswordTokenManager;
use CoopTilleuls\ForgotPasswordBundle\Provider\ProviderInterface;
use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
use Doctrine\ORM\Events;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

class RecruiterValidationSubscriber implements EventSubscriberInterface
{
    private const SUBJECTS = [
        1 => 'SomeAdvice',
        -1 => 'SomeAdvice,
        0 => 'SomeAdvice'
    ];

    public function __construct(
        private readonly PasswordTokenManager $forgotPasswordManager,
        private readonly ProviderInterface $provider,
        private readonly ParameterBagInterface $params,
        private readonly MailService $mailService
    ) {
    }

    public function getSubscribedEvents(): array
    {
        return [
            Events::preUpdate,
        ];
    }

    public function preUpdate(LifecycleEventArgs $args): void
    {
        $recruiter = $args->getObject();
        if (!$recruiter instanceof Recruiter) {
            return;
        }

        $changes = $args->getObjectManager()->getUnitOfWork()->getEntityChangeSet($recruiter);
        if (!isset($changes['isValidated']) || $changes['isValidated'][0] === $changes['isValidated'][1]) {
            return;
        }

        $status = $changes['isValidated'][1];
        $this->mailService->sendEmail(
            $recruiter->getEmail(),
            self::SUBJECTS[$status] ?? self::SUBJECTS[0],
            'mailTemplate',
            $this->getEmailData($recruiter, $status, $changes['refusalReason'][1] ?? null)
        );
    }

    private function getEmailData(Recruiter $recruiter, int $status, ?string $refusalReason): array
    {
        $data = [
            'recruiter' => $recruiter,
            'status' => match($status) {
                1 => 'approved',
                -1 => 'refused',
                default => 'pending'
            },
            'validationStatus' => $status,
            'refusalReason' => $refusalReason
        ];

        if ($status === 1) {
            $token = $this->forgotPasswordManager->createPasswordToken($recruiter);
            $data['resetUrl'] = sprintf('%s/app/forgot-password?token=%s&provider=%s',
                rtrim($this->params->get('app.base_url'), '/'),
                $token->getToken(),
                $this->provider->getName()
            );
        }

        return $data;
    }
}

Thanks for reading.
Déxime French Developper

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant