custom/plugins/RpayPayments/src/Components/Logging/Subscriber/RequestSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright (c) Ratepay GmbH
  4.  *
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace Ratepay\RpayPayments\Components\Logging\Subscriber;
  9. use Monolog\Logger;
  10. use Ratepay\RpayPayments\Components\Logging\Service\ApiLogger;
  11. use Ratepay\RpayPayments\Components\PaymentHandler\Event\PaymentFailedEvent;
  12. use Ratepay\RpayPayments\Components\RatepayApi\Event\RequestDoneEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class RequestSubscriber implements EventSubscriberInterface
  15. {
  16.     protected ApiLogger $apiLogger;
  17.     private Logger $fileLogger;
  18.     public function __construct(ApiLogger $apiLoggerLogger $fileLogger)
  19.     {
  20.         $this->apiLogger $apiLogger;
  21.         $this->fileLogger $fileLogger;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             RequestDoneEvent::class => 'onRequestDone',
  27.             PaymentFailedEvent::class => 'onPaymentFailed',
  28.         ];
  29.     }
  30.     public function onPaymentFailed(PaymentFailedEvent $event): void
  31.     {
  32.         $exception $event->getException();
  33.         if ($exception) {
  34.             $exception $exception->getPrevious() ?? $exception;
  35.             $message $exception->getMessage();
  36.         } elseif ($event->getResponse()) {
  37.             $message $event->getResponse()->getReasonMessage();
  38.         }
  39.         $this->fileLogger->error($message ?? 'Unknown error', [
  40.             'order_id' => $event->getOrder()->getId(),
  41.             'order_number' => $event->getOrder()->getOrderNumber(),
  42.             'request_bag' => $event->getRequestDataBag(),
  43.         ]);
  44.     }
  45.     public function onRequestDone(RequestDoneEvent $event): void
  46.     {
  47.         $this->apiLogger->logRequest($event);
  48.     }
  49. }