Maxime Magné
Maxime Magné

Reputation: 1

symfony[2.8] how to implement an interface in a service

Here is my code for my class listener :

<?php

namespace AppBundle\EventSubscriber;

use Lolautruche\PaylineBundle\Event\PaylineEvents;
use Lolautruche\PaylineBundle\Event\ResultEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class PaymentListener implements EventSubscriberInterface
{
    private $logger;

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    public static function getSubscribedEvents()
    {
        return [
            PaylineEvents::WEB_TRANSACTION_VERIFY => 'onTransactionVerify',
        ];
    }

    public function onTransactionVerify(ResultEvent $event)
    {   break;
        // You can access to the result object from the transaction verification.
        /** @var \Lolautruche\PaylineBundle\Payline\PaylineResult $paylineResult */
        $paylineResult = $event->getResult();
        $transactionId = $paylineResult->getItem('[transaction][id]');

        if (!$paylineResult->isSuccessful()) {
            break;
            if ($paylineResult->isCanceled()){
                $this->logger->info("Transaction #$transactionId was canceled by user", ['paylineResult' => $paylineResult->getResultHash()]);
            }
            elseif ($paylineResult->isDuplicate()){
                $this->logger->warning("Transaction #$transactionId is a duplicate", ['paylineResult' => $paylineResult->getResultHash()]);
            }
            else {
                $this->logger->error("Transaction #$transactionId was refused by bank.", ['paylineResult' => $paylineResult->getResultHash()]);
            }

            return;
        }
        break;
        // Transaction was validated, do whatever you need to update your order
        // ...
        // Assuming you have set a private data with "internal_id" key when initiating the transaction.
        $internalId = $paylineResult->getPrivateData('idCommande');
        $repoCommande = $this->getDoctrine()->getManager()->getRepository('CommandeBundle:Commande');
        $commande = $repoCommande->find($id);
        $commande->setValide(1);
        $em = $this->getDoctrine()->getManager();
        $em->persist($commande);
        $em->flush();
        $this->logger->info("Transaction #$transactionId is valid. Internal ID is $internalId");
    }
}

then I declared it as a service

services:
    app.payment_listener:
        class: AppBundle\EventSubscriber\PaymentListener
        arguments: ["@LoggerInterface"]
        tags:
            - { name: kernel.event_subscriber }

But the arguments is not good. The constructor asks a loggerInterface argument and it returns me the following error :

ServiceNotFoundException in CheckExceptionOnInvalidReferenceBehaviorPass.php line 58: The service "app.payment_listener" has a dependency on a non-existent service "loggerinterface".

I explain what I would like to do, in fact I want use the payline bundle but I am stuck here.

Please, help me.

Upvotes: 0

Views: 187

Answers (1)

xurshid29
xurshid29

Reputation: 4210

When you're passing an argument to constructor, as _construct(LoggerInterface $logger) you're telling that $logger argument can be any object whose class is the child of the LoggerInterface. So, in your service definition you can pass any logger service (@logger service, for example), not the interface itself. The answer to your question is, pass @logger service from Monolog bridge (or any other service name, which extends the LoggerInterface).

You can find more information here.

Upvotes: 2

Related Questions