2016-08-18 71 views
0

這裏是我的我的課聽衆代碼:symfony的[2.8]如何實現服務的接口

<?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". 

我解釋,我想做些什麼,其實我想用支付線捆綁,但我在這裏停留。

請幫幫我。

+0

不應該是_ @ logger_? '參數:[「@logger」]'? – pavlovich

回答

2

當你傳遞一個參數給構造函數,如_construct(LoggerInterface $logger)你告訴$logger參數可以是其類是LoggerInterface的孩子的任何對象。因此,在您的服務定義中,您可以傳遞任何記錄器服務(例如@logger服務),而不是接口本身。您的問題的答案是,通過來自Monolog橋的@logger服務(或任何其他服務名稱,其擴展LoggerInterface)。

你可以找到更多的信息here