<?php declare(strict_types=1);
namespace CioBudget\Service;
use CioBudget\Definition\Budget\BudgetEntity;
use CioBudget\Definition\BudgetStore\BudgetStoreEntity;
use Psr\Container\ContainerInterface;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
class SessionService
{
private BudgetInfoService $budgetInfoService;
private BudgetLoaderService $budgetLoaderService;
private ContainerInterface $container;
private StoreLoaderService $storeLoaderService;
public function __construct(BudgetInfoService $budgetInfoService, BudgetLoaderService $budgetLoaderService, StoreLoaderService $storeLoaderService)
{
$this->budgetInfoService = $budgetInfoService;
$this->budgetLoaderService = $budgetLoaderService;
$this->storeLoaderService = $storeLoaderService;
}
public function setContainer(ContainerInterface $container)
{
$this->container = $container;
}
public function getCurrentBudgetId()
{
return $this->container->get('session')->get('cio_current_budget');
}
public function setCustomerBudgetInSession(CustomerEntity $customer, SalesChannelContext $context)
{
if ($customerStores = $this->storeLoaderService->getCustomerStores($customer)) {
$firstStoreId = $customerStores[0]['id'];
$budgetId = $this->budgetLoaderService->getBudgetIdByStoreId($firstStoreId);
if ($budgetId) {
$this->setBudgetIdInSession($budgetId, $context);
}
}
}
public function setBudgetIdInSession(string $budgetId, SalesChannelContext $context)
{
if (!$budgetId) {
return;
}
if ($this->budgetInfoService->isBudgetActive($budgetId, $context->getContext())) {
$this->container->get('session')->set('cio_current_budget', $budgetId);
}
}
public function getBudgetIdFromSession()
{
return $this->container->get('session')->get('cio_current_budget');
}
/**
* @param Context $context
* @return BudgetEntity|null
*/
public function getBudgetEntityFromSession(Context $context)
{
if ($this->getCurrentBudgetId()) {
/** @var EntityRepositoryInterface $budgetRepository */
$budgetRepository = $this->container->get('cio_budget.repository');
return $budgetRepository->search(new Criteria([$this->getCurrentBudgetId()]), $context)->first();
}
return null;
}
/**
* @param Context $context
* @return BudgetStoreEntity|null
*/
public function getBudgetStoreEntityFromSession(Context $context)
{
/** @var BudgetEntity $budget */
if ($budget = $this->getBudgetEntityFromSession($context)) {
/** @var EntityRepositoryInterface $budgetStoreRepository */
$budgetStoreRepository = $this->container->get('cio_budget_store.repository');
return $budgetStoreRepository->search((new Criteria([$budget->getStoreId()]))->addAssociation('region')->addAssociation('division')->addAssociation('addressbook'), $context)->first();
}
return null;
}
public function removeOldActiveBudget()
{
$this->container->get('session')->set('cio_current_budget', null);
}
}