custom/plugins/CioBudget/src/Service/SessionService.php line 35

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace CioBudget\Service;
  3. use CioBudget\Definition\Budget\BudgetEntity;
  4. use CioBudget\Definition\BudgetStore\BudgetStoreEntity;
  5. use Psr\Container\ContainerInterface;
  6. use Shopware\Core\Checkout\Customer\CustomerEntity;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. class SessionService
  12. {
  13.     private BudgetInfoService $budgetInfoService;
  14.     private BudgetLoaderService $budgetLoaderService;
  15.     private ContainerInterface $container;
  16.     private StoreLoaderService $storeLoaderService;
  17.     public function __construct(BudgetInfoService $budgetInfoServiceBudgetLoaderService $budgetLoaderServiceStoreLoaderService $storeLoaderService)
  18.     {
  19.         $this->budgetInfoService $budgetInfoService;
  20.         $this->budgetLoaderService $budgetLoaderService;
  21.         $this->storeLoaderService $storeLoaderService;
  22.     }
  23.     public function setContainer(ContainerInterface $container)
  24.     {
  25.         $this->container $container;
  26.     }
  27.     public function getCurrentBudgetId()
  28.     {
  29.         return $this->container->get('session')->get('cio_current_budget');
  30.     }
  31.     public function setCustomerBudgetInSession(CustomerEntity $customerSalesChannelContext $context)
  32.     {
  33.         if ($customerStores $this->storeLoaderService->getCustomerStores($customer)) {
  34.             $firstStoreId $customerStores[0]['id'];
  35.             $budgetId $this->budgetLoaderService->getBudgetIdByStoreId($firstStoreId);
  36.             if ($budgetId) {
  37.                 $this->setBudgetIdInSession($budgetId$context);
  38.             }
  39.         }
  40.     }
  41.     public function setBudgetIdInSession(string $budgetIdSalesChannelContext $context)
  42.     {
  43.         if (!$budgetId) {
  44.             return;
  45.         }
  46.         if ($this->budgetInfoService->isBudgetActive($budgetId$context->getContext())) {
  47.             $this->container->get('session')->set('cio_current_budget'$budgetId);
  48.         }
  49.     }
  50.     public function getBudgetIdFromSession()
  51.     {
  52.         return $this->container->get('session')->get('cio_current_budget');
  53.     }
  54.     /**
  55.      * @param Context $context
  56.      * @return BudgetEntity|null
  57.      */
  58.     public function getBudgetEntityFromSession(Context $context)
  59.     {
  60.         if ($this->getCurrentBudgetId()) {
  61.             /** @var EntityRepositoryInterface $budgetRepository */
  62.             $budgetRepository $this->container->get('cio_budget.repository');
  63.             return $budgetRepository->search(new Criteria([$this->getCurrentBudgetId()]), $context)->first();
  64.         }
  65.         return null;
  66.     }
  67.     /**
  68.      * @param Context $context
  69.      * @return BudgetStoreEntity|null
  70.      */
  71.     public function getBudgetStoreEntityFromSession(Context $context)
  72.     {
  73.         /** @var BudgetEntity $budget */
  74.         if ($budget $this->getBudgetEntityFromSession($context)) {
  75.             /** @var EntityRepositoryInterface $budgetStoreRepository */
  76.             $budgetStoreRepository $this->container->get('cio_budget_store.repository');
  77.             return $budgetStoreRepository->search((new Criteria([$budget->getStoreId()]))->addAssociation('region')->addAssociation('division')->addAssociation('addressbook'), $context)->first();
  78.         }
  79.         return null;
  80.     }
  81.     public function removeOldActiveBudget()
  82.     {
  83.         $this->container->get('session')->set('cio_current_budget'null);
  84.     }
  85. }