Symfony 3.4 FOSCommentBundle Can not autowire service inside listener

581 Views Asked by At

Can somebody helps me with autowiring in Symfony3.4?

I'm trying to implement the following Listener, but i don't know to inject the VoteManagerInterface. Here's the error, my code and my config.Thank you for your help :)

Cannot autowire service "AppBundle\EventListener\CommentVoteListener": argument "$voteManager" of method "__construct()" references interface "FOS\CommentBundle\Model\VoteManagerInterface" but no such service exists. You should maybe alias this interface to one of these existing services: "fos_comment.manager.vote.default", "fos_comment.manager.vote.acl".

    <?php

namespace AppBundle\EventListener;

use FOS\CommentBundle\Event\VotePersistEvent;
use FOS\CommentBundle\Events;
use FOS\CommentBundle\Model\VoteManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Description of CommentVoteListener
 *
 */
class CommentVoteListener implements EventSubscriberInterface {

    private $voteManager;
    private $token_storage;

    public function __construct(VoteManagerInterface $voteManager, TokenStorageInterface $token_storage) {
        $this->voteManager = $voteManager;
        $this->token_storage = $token_storage;
    }

    /**
     * Assumptions:
     *  1) User is logged in (caught by FOSCB acl)
     *  2) VoteBlamerListener has already run which assigned the voter (this event has priority of -1)
     *
     * Make sure the user has not already voted, and make sure that the user is not the author.
     *
     * @param VotePersistEvent $event
     * @return void
     */
    public function onVotePrePersist(VotePersistEvent $event) {
        $vote = $event->getVote();
        $user = $this->token_storage->getToken()->getUser();

        // make sure user is not the author
        if ($vote->getComment()->getAuthor() === $user) {
            $this->stopPersistence($event);
        }

        // make sure user hasn't already voted
        $existingVote = $this->voteManager->findVoteBy(array('comment' => $vote->getComment(), 'voter' => $user));
        if (null !== $existingVote && $vote->getValue() === $existingVote->getValue()) {
            $this->stopPersistence($event);
        }
    }

    protected function stopPersistence(VotePersistEvent $event) {
        $event->abortPersistence();
        $event->stopPropagation();
    }

    public static function getSubscribedEvents() {
        return array(
            Events::VOTE_PRE_PERSIST => array('onVotePrePersist', -1),
        );
    }
}

}

app.listener.vote:
    class: AppBundle\EventListener\CommentVoteListener
    arguments:
        - "@fos_comment.manager.vote.acl"
        - "@security.token_storage"
    tags:
    - { name: kernel.event_listener, event: fos_comment.vote.pre_persist, method: onVotePrePersist }

or

app.listener.vote:
    class: AppBundle\EventListener\CommentVoteListener
    arguments: ["@fos_comment.manager.vote.acl", "@security.token_storage"]
    tags:
    - { name: kernel.event_listener, event: fos_comment.vote.pre_persist, method: onVotePrePersist }
1

There are 1 best solutions below

2
On

Thank you, problem solved with the fully qualified class name like Cerad suggested. Even if I wonder why it worked for the app.listener.thread case :)

AppBundle\EventListener\CommentVoteListener:
    arguments:
        - "@fos_comment.manager.vote.acl"
        - "@security.token_storage"
    tags:
    - { name: kernel.event_listener, event: fos_comment.vote.pre_persist, method: onVotePrePersist }