custom/plugins/PayonePayment/src/EventListener/KernelEventListener.php line 31

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace PayonePayment\EventListener;
  4. use PayonePayment\Components\ConfigReader\ConfigReader;
  5. use PayonePayment\Components\Ratepay\Profile\ProfileServiceInterface;
  6. use PayonePayment\PaymentHandler\PaymentHandlerGroups;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  11. class KernelEventListener implements EventSubscriberInterface
  12. {
  13.     private ProfileServiceInterface $profileService;
  14.     public function __construct(ProfileServiceInterface $profileService)
  15.     {
  16.         $this->profileService $profileService;
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             ResponseEvent::class => 'onKernelResponse',
  22.         ];
  23.     }
  24.     public function onKernelResponse(ResponseEvent $event): void
  25.     {
  26.         $route $event->getRequest()->get('_route');
  27.         $response $event->getResponse();
  28.         if ($route === 'api.action.core.save.system-config.batch') {
  29.             $results = [];
  30.             $configurations $event->getRequest()->request->all();
  31.             foreach ($configurations as $salesChannelId => $configuration) {
  32.                 foreach (PaymentHandlerGroups::RATEPAY as $ratepayHandler) {
  33.                     $profilesConfigKey ConfigReader::getConfigKeyByPaymentHandler($ratepayHandler'Profiles');
  34.                     if (isset($configuration[$profilesConfigKey])) {
  35.                         $result $this->profileService->updateProfileConfiguration(
  36.                             $ratepayHandler,
  37.                             $salesChannelId === 'null' null : (string) $salesChannelId
  38.                         );
  39.                         $results[$salesChannelId][] = $result;
  40.                     }
  41.                 }
  42.             }
  43.             if ($response instanceof JsonResponse && \count($results) > 0) {
  44.                 $this->setResponseData($response$results);
  45.             }
  46.         }
  47.     }
  48.     protected function setResponseData(JsonResponse $response, array $updateResults): void
  49.     {
  50.         $data = [];
  51.         if ($response->getContent()) {
  52.             $data json_decode($response->getContent(), true);
  53.         }
  54.         $data['payoneRatepayProfilesUpdateResult'] = [];
  55.         foreach ($updateResults as $salesChannelId => $updateResultsBySalesChannel) {
  56.             $salesChannelData = [
  57.                 'updates' => [],
  58.                 'errors' => [],
  59.             ];
  60.             foreach ($updateResultsBySalesChannel as $updateResult) {
  61.                 $salesChannelData['updates'][] = $updateResult['updates'];
  62.                 $salesChannelData['errors'][] = $updateResult['errors'];
  63.             }
  64.             $salesChannelData['updates'] = array_merge(...$salesChannelData['updates']);
  65.             $salesChannelData['errors'] = array_merge(...$salesChannelData['errors']);
  66.             $data['payoneRatepayProfilesUpdateResult'][$salesChannelId] = $salesChannelData;
  67.         }
  68.         $response->setData($data);
  69.         $response->setStatusCode(Response::HTTP_OK);
  70.     }
  71. }