vendor/store.shopware.com/agiqonoci/src/Services/DataResolverService.php line 43

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace AgiqonOci\Services;
  3. use AgiqonOci\Models\OciSession;
  4. use Shopware\Core\Checkout\Cart\Cart;
  5. use Shopware\Core\Checkout\Cart\LineItem\LineItemCollection;
  6. use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
  7. use Shopware\Core\Content\Product\ProductEntity;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  14. use Shopware\Core\System\Currency\CurrencyEntity;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Shopware\Core\System\SystemConfig\SystemConfigService;
  17. use Symfony\Component\HttpFoundation\Session\Session;
  18. use Symfony\Component\Security\Core\Exception\SessionUnavailableException;
  19. class DataResolverService
  20. {
  21.     protected ?OciSession $session;
  22.     protected EntityRepository $productRepository;
  23.     protected EntityRepository $currencyRepository;
  24.     protected SystemConfigService $configService;
  25.     protected Cart $cart;
  26.     protected Context $context;
  27.     protected ?array $config;
  28.     protected LineItemCollection $lineItems;
  29.     protected EntitySearchResult $products;
  30.     protected CalculatedPrice $shippingCosts;
  31.     protected $shippingCostsRate;
  32.     protected ?CurrencyEntity $currency;
  33.     public function __construct(Session             $session,
  34.                                 EntityRepository    $productRepository,
  35.                                 EntityRepository    $currencyRepository,
  36.                                 SystemConfigService $configService)
  37.     {
  38.         $this->session $session->get(OciSession::OCI_SESSION_NAME);
  39.         $this->productRepository $productRepository;
  40.         $this->currencyRepository $currencyRepository;
  41.         $this->configService $configService;
  42.     }
  43.     /**
  44.      * Resolves all needed data for OciSession
  45.      *
  46.      * @param Cart $cart
  47.      * @param Context $context
  48.      */
  49.     public function resolveOciData(Cart $cartSalesChannelContext $context)
  50.     {
  51.         if (!$this->session) {
  52.             throw new SessionUnavailableException(OciSession::OCI_SESSION_NAME ' not found!');
  53.         }
  54.         $this->cart $cart;
  55.         $this->context $context->getContext();
  56.         $this->setConfig($context->getSalesChannelId());
  57.         $this->setLineItems();
  58.         $this->setProducts();
  59.         $this->setShippingCosts();
  60.         $this->setShippingCostsRate();
  61.         $this->setCurrency();
  62.     }
  63.     public function getSession()
  64.     {
  65.         return $this->session;
  66.     }
  67.     private function setConfig(?string $salesChannelId null): void
  68.     {
  69.         $this->config $this->configService->get('AgiqonOci.config'$salesChannelId);
  70.     }
  71.     public function getConfig(): ?array
  72.     {
  73.         return $this->config;
  74.     }
  75.     public function getConfigValue(string $key): string
  76.     {
  77.         return $this->config[$key] ?? '';
  78.     }
  79.     private function setLineItems(): void
  80.     {
  81.         $this->lineItems $this->cart->getLineItems();
  82.     }
  83.     public function getLineItems()
  84.     {
  85.         return $this->lineItems;
  86.     }
  87.     private function setProducts(): void
  88.     {
  89.         $productIds = [];
  90.         foreach ($this->cart->getLineItems() as $lineItem) {
  91.             if ($lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE ) {
  92.                 $productIds[] = $lineItem->getReferencedId();
  93.             }
  94.             if($lineItem->getType() === 'customized-products'){
  95.                 $customizedProduct $lineItem->getChildren()->getElements();
  96.                 $customizedProductID array_values($customizedProduct)[0]->getReferencedId();
  97.                 $productIds[] = $customizedProductID;
  98.             }
  99.         }
  100.         $criteria = new Criteria($productIds);
  101.         $criteria->addAssociation('tax');
  102.         $this->products $this->productRepository->search($criteria$this->context);
  103.     }
  104.     public function getProducts(): ?EntityCollection
  105.     {
  106.         return $this->products;
  107.     }
  108.     public function getProductById($productId): ?ProductEntity
  109.     {
  110.         return $this->products->get($productId);
  111.     }
  112.     private function setShippingCosts()
  113.     {
  114.         $this->shippingCosts $this->cart->getDeliveries()->getShippingCosts()->sum();
  115.     }
  116.     public function getShippingCosts(): ?CalculatedPrice
  117.     {
  118.         return $this->shippingCosts;
  119.     }
  120.     public function setShippingCostsRate()
  121.     {
  122.        $this->shippingCostsRate $this->cart->getDeliveries()->getShippingCosts()->getTaxRules()->first() ? $this->cart->getDeliveries()->getShippingCosts()->getTaxRules()->first()->getTaxRate() : 0;
  123.     }
  124.     public function getShippingCostsRate(): float
  125.     {
  126.         return $this->shippingCostsRate;
  127.     }
  128.     private function setCurrency()
  129.     {
  130.         $criteria = new Criteria([$this->context->getCurrencyId()]);
  131.         $this->currency $this->currencyRepository->search($criteria$this->context)->first();
  132.     }
  133.     public function getCurrency(): ?CurrencyEntity
  134.     {
  135.         return $this->currency;
  136.     }
  137. }