custom/plugins/EasyCreditRatenkauf/src/Service/WebshopInfoService.php line 42

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * (c) NETZKOLLEKTIV GmbH <kontakt@netzkollektiv.com>
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace Netzkollektiv\EasyCredit\Service;
  9. use Netzkollektiv\EasyCredit\Api\IntegrationFactory;
  10. use Symfony\Component\Cache\Adapter\AdapterInterface;
  11. use Symfony\Contracts\Cache\ItemInterface;
  12. use Teambank\EasyCreditApiV3\Model\WebshopResponse;
  13. use Psr\Log\LoggerInterface;
  14. class WebshopInfoService
  15. {
  16.     private IntegrationFactory $integrationFactory;
  17.     private AdapterInterface $cache;
  18.     private LoggerInterface $logger;
  19.     public function __construct(
  20.         IntegrationFactory $integrationFactory,
  21.         AdapterInterface $cache,
  22.         LoggerInterface $logger
  23.     ) {
  24.         $this->integrationFactory $integrationFactory;
  25.         $this->cache $cache;
  26.         $this->logger $logger;
  27.     }
  28.     public function getWebshopInfo(string $salesChannelId): WebshopResponse
  29.     {
  30.         if (!\method_exists($this->cache,'get')) { // no cache for <= sw 6.4.9
  31.             return $this->integrationFactory
  32.                 ->createCheckout($salesChannelId)
  33.                 ->getWebshopDetails();
  34.         }
  35.         return $this->cache->get('easycredit-webshop-details-' $salesChannelId, function (ItemInterface $item) use ($salesChannelId) {
  36.             $item->expiresAfter(3600); // Cache for 1 hour
  37.             try {
  38.                 return $this->integrationFactory
  39.                     ->createCheckout($salesChannelId)
  40.                     ->getWebshopDetails();
  41.             } catch (\Throwable $e) {
  42.                 $this->logger->error('Failed to fetch webshop details: ' $e->getMessage(), [
  43.                     'exception' => $e,
  44.                     'salesChannelId' => $salesChannelId
  45.                 ]);
  46.                 throw $e// Re-throw to prevent caching
  47.             }
  48.         });
  49.     }