app/Plugin/Api42/Service/WebHookService.php line 63

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\Api42\Service;
  13. use Eccube\Util\StringUtil;
  14. use GuzzleHttp\Client;
  15. use GuzzleHttp\Exception\TransferException;
  16. use GuzzleHttp\Pool;
  17. use GuzzleHttp\Psr7\Request;
  18. use GuzzleHttp\Psr7\Response;
  19. use Plugin\Api42\Entity\WebHook;
  20. use Plugin\Api42\Repository\WebHookRepository;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Symfony\Component\HttpKernel\Event\ResponseEvent ;
  23. use Symfony\Component\HttpKernel\KernelEvents;
  24. use Symfony\Component\Routing\RouterInterface;
  25. class WebHookService implements EventSubscriberInterface
  26. {
  27.     /**
  28.      * @var RouterInterface
  29.      */
  30.     private $router;
  31.     /**
  32.      * @var WebHookRepository
  33.      */
  34.     private $webHookRepository;
  35.     /**
  36.      * @var WebHookEvents
  37.      */
  38.     private $webHookEvents;
  39.     /**
  40.      * WebHookService constructor.
  41.      */
  42.     public function __construct(RouterInterface $routerWebHookRepository $webHookRepositoryWebHookEvents $webHookEvents)
  43.     {
  44.         $this->router $router;
  45.         $this->webHookRepository $webHookRepository;
  46.         $this->webHookEvents $webHookEvents;
  47.     }
  48.     public static function getSubscribedEvents()
  49.     {
  50.         return [
  51.             KernelEvents::RESPONSE => 'fire',
  52.         ];
  53.     }
  54.     public function fire(ResponseEvent $event)
  55.     {
  56.         if (!$event->isMainRequest()) {
  57.             return;
  58.         }
  59.         $events $this->webHookEvents->toArray();
  60.         if ($events) {
  61.             $payload json_encode($events);
  62.             /** @var WebHook[] $availableWebHooks */
  63.             $availableWebHooks $this->webHookRepository->findBy(['enabled' => true]);
  64.             $requests array_map(function (WebHook $WebHook) use ($payload) {
  65.                 return $this->createRequest($payload$WebHook);
  66.             }, $availableWebHooks);
  67.             $client = new Client();
  68.             $pool = new Pool($client$requests, [
  69.                 'concurrency' => 5,
  70.                 'options' => [
  71.                     'connect_timeout' => 1,
  72.                     'timeout' => 5,
  73.                     'allow_redirects' => false,
  74.                 ],
  75.                 'fulfilled' => function (Response $reason$index) use ($availableWebHooks) {
  76.                     log_info('WebHook request successful.', ['Payload URL' => $availableWebHooks[$index]->getPayloadUrl()]);
  77.                 },
  78.                 'rejected' => function (TransferException $e$index) use ($availableWebHooks) {
  79.                     log_error($e->getMessage(), ['Payload URL' => $availableWebHooks[$index]->getPayloadUrl()]);
  80.                 },
  81.             ]);
  82.             $p $pool->promise();
  83.             $p->wait();
  84.         }
  85.     }
  86.     private function createRequest($payload$WebHook)
  87.     {
  88.         $headers = [
  89.             'Content-Type' => 'application/json',
  90.             'X-ECCUBE-URL' => $this->router->generate('homepage', [], RouterInterface::ABSOLUTE_URL),
  91.         ];
  92.         if (StringUtil::isNotBlank($WebHook->getSecret())) {
  93.             $headers['X-ECCUBE-Signature'] = hash_hmac('sha256'$payload$WebHook->getSecret());
  94.         }
  95.         return new Request('POST'$WebHook->getPayloadUrl(), $headers$payload);
  96.     }
  97. }