symfony [2.8]如何在服务中实现接口

时间:2016-08-18 17:17:10

标签: symfony

这是我的类监听器的代码:

<?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");
    }
}

然后我宣布它为服务

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

但争论并不好。构造函数询问loggerInterface参数,它返回以下错误:

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

我解释了我想做什么,事实上我想使用支付线套装,但我被困在这里。

请帮助我。

1 个答案:

答案 0 :(得分:2)

当你将一个参数传递给构造函数时,就像_construct(LoggerInterface $logger)一样,你告诉$logger参数可以是任何类,其类是LoggerInterface的子元素。因此,在您的服务定义中,您可以传递任何记录器服务(例如@logger服务),而不是接口本身。您的问题的答案是,从Monolog网桥(或任何其他服务名称,传递@logger)传递LoggerInterface服务。

您可以找到更多信息here