Search code examples
phpsymfonyrabbitmqamqp

queuing protocol in symfony - too few arguments


Hi im stuck with this problem, i wanted to make simple email sender via rabbitmq and queuing server.Main idea was when user will register he will get email that he registered. For now i was using just test values to see why it's not pushing through the queue. to be exactly precise this error:

[info] Received message App\Message\UserRegistrationEmail

[critical] Error thrown while handling message App\Message\UserRegistrationEmail. Removing from transport after 3 retries. Error: "Handling "App\Message\UserRegistrationEmail" failed: Too few arguments to function App\Message\MessageHandler\UserRegistrationEmailHandler::__invoke(), 1 passed in /vendor/symfony/messenger/Middleware/HandleMessageMiddleware.php on line 63 and exactly 2 expected"

And to be honest i dont know what im doing wrong, this is rest of files:

Handler:

<?php

namespace App\Message\MessageHandler;

use App\Message\UserRegistrationEmail;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;


class UserRegistrationEmailHandler implements MessageHandlerInterface
{
    /**
     * @param UserRegistrationEmail $orderConfirmationEmail
     */
    public function __invoke(UserRegistrationEmail $orderConfirmationEmail){

        sleep(15);
        echo('sending email right now');//test payload for queue
    }
}
class UserRegistrationEmail
{
/**
 * @var string
 */
    private $userEmail;

    public function __construct(string $userEmail){
        $this->userEmail = $userEmail;
    }

    /**
     * @return string
     */
    public function getUserEmail(): string{
        return $this->userEmail;
    }
}

and the last one controller:

<?php

namespace App\Controller;

use App\Form\UserType;
use App\Entity\User;
use App\Message\UserRegistrationEmail;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Messenger\MessageBusInterface;

class RegistrationController extends AbstractController{
    /**
     * @Route(path="/register", name="user_registration")
     * @param MailerInterface $mailer
     * @param MessageBusInterface $bus
     */
    public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder,MessageBusInterface $bus){
        $user = new User();
        $form = $this->createForm(UserType::class, $user);

        //TODO handle the submit (will only happen on POST)
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()){

            $password = $passwordEncoder->encodePassword($user, $user->getPlainPassword());
            $user->setPassword($password);
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($user);
            $entityManager->flush();

            // TODO sending mail with login info
            //$mailToPass = $user->getEmail();
            $bus->dispatch(new UserRegistrationEmail("[email protected]"));
            //return $this->redirectToRoute('/register');//TODO to edit future routes
            return new Response("user has been registered");
        }
        return $this->render(
            'registration/register.html.twig',
            array('form' => $form->createView())
        );
    }
}

thanks for and help/clues/ideas!


Solution

  • Solved it, it was amqp worker fault, something bugged but, also moved one function to __construct