149 lines
3.5 KiB
PHP
149 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace KupShop\PricelistBundle\Context;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use KupShop\KupShopBundle\Context\ContextInterface;
|
|
use KupShop\KupShopBundle\Context\EmptiableContext;
|
|
use KupShop\KupShopBundle\Context\UserContext;
|
|
use KupShop\PricelistBundle\Entity\Pricelist;
|
|
use Symfony\Contracts\Service\Attribute\Required;
|
|
|
|
class PricelistContext implements ContextInterface, EmptiableContext
|
|
{
|
|
public const PERSISTENT_KEY = 'persistentPricelist';
|
|
|
|
protected EntityManagerInterface $em;
|
|
private UserContext $userContext;
|
|
|
|
protected ?string $activeId = null;
|
|
private bool $loaded = false;
|
|
|
|
/** @var array<string, Pricelist> */
|
|
protected array $activeCache = [];
|
|
|
|
public function __construct()
|
|
{
|
|
}
|
|
|
|
public function activate(?string $id)
|
|
{
|
|
if (null === $id) {
|
|
trigger_error('Passing $id=null into ContextInterface::activate is deprecated! Use ::clearCache() instead.', \E_USER_DEPRECATED);
|
|
}
|
|
|
|
if ($id) {
|
|
$this->activeId = $id;
|
|
$this->loaded = true;
|
|
} else {
|
|
$this->clearCache();
|
|
}
|
|
}
|
|
|
|
public function getActive(): ?Pricelist
|
|
{
|
|
if (null !== ($id = $this->getActiveId())) {
|
|
if (array_key_exists($id, $this->activeCache)) {
|
|
return $this->activeCache[$id];
|
|
} else {
|
|
return $this->activeCache[$id] = $this->loadPricelistById($id);
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
protected function loadActive()
|
|
{
|
|
if ($user = $this->userContext->getActive()) {
|
|
if (!empty($user->id_pricelist)) {
|
|
return $user->id_pricelist;
|
|
}
|
|
|
|
foreach ($user->getGroups() as $group) {
|
|
if (!empty($group['id_pricelist'])) {
|
|
return $group['id_pricelist'];
|
|
}
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function getActiveId(): ?string
|
|
{
|
|
return $this->isEmpty() ? null : $this->activeId ??= $this->loadActive();
|
|
}
|
|
|
|
protected function loadPricelistById($id)
|
|
{
|
|
return $this->em->getRepository(Pricelist::class)->find($id);
|
|
}
|
|
|
|
public function getDefault(): ?Pricelist
|
|
{
|
|
if (!$this->getDefaultId()) {
|
|
return null;
|
|
}
|
|
|
|
return $this->em->getRepository(Pricelist::class)->find($this->getDefaultId());
|
|
}
|
|
|
|
public function getDefaultId(): ?string
|
|
{
|
|
return null;
|
|
}
|
|
|
|
protected function loadPricelistByCurrency($currency)
|
|
{
|
|
return $this->em->getRepository(Pricelist::class)->findOneBy(['currency' => $currency]);
|
|
}
|
|
|
|
/**
|
|
* @return Pricelist[]
|
|
*/
|
|
public function getSupported(): array
|
|
{
|
|
return $this->em->getRepository(Pricelist::class)->findAll();
|
|
}
|
|
|
|
public function clearCache(): void
|
|
{
|
|
$this->loaded = false;
|
|
$this->activeId = null;
|
|
}
|
|
|
|
public function forceEmpty(): void
|
|
{
|
|
$this->loaded = true;
|
|
$this->activeId = null;
|
|
}
|
|
|
|
public function isEmpty(): bool
|
|
{
|
|
return $this->loaded && $this->activeId === null;
|
|
}
|
|
|
|
public function isValid(string $id): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function getMultipleActiveIds(int $priceListId): ?array
|
|
{
|
|
return null;
|
|
}
|
|
|
|
#[Required]
|
|
public function _setUserContext(UserContext $userContext): void
|
|
{
|
|
$this->userContext = $userContext;
|
|
}
|
|
|
|
#[Required]
|
|
public function _setEm(EntityManagerInterface $em): void
|
|
{
|
|
$this->em = $em;
|
|
}
|
|
}
|