Files
kupshop/bundles/KupShop/KupShopBundle/Context/PriceLevelContext.php
2025-08-02 16:30:27 +02:00

199 lines
5.1 KiB
PHP

<?php
namespace KupShop\KupShopBundle\Context;
use Doctrine\DBAL\Exception;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\KupShopBundle\Util\Contexts;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Contracts\Service\Attribute\Required;
class PriceLevelContext implements ContextInterface, EmptiableContext
{
public const SESSION_KEY = 'priceLevel';
public const PERSISTENT_KEY = 'persistentPriceLevel';
protected SessionInterface $session;
protected \KupShop\CatalogBundle\PriceLevel $priceLevel;
protected ?string $activeId = null;
protected bool $loaded = false;
/** @var \PriceLevel[] */
protected array $activeCache = [];
/** @var \PriceLevel[]|null */
private ?array $supported = null;
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();
}
return $this;
}
/**
* @return \KupShop\CatalogBundle\PriceLevel|\PriceLevel
*/
public function getById(string $activeId)
{
if (!array_key_exists($activeId, $this->activeCache)) {
$this->priceLevel = ServiceContainer::getService(\KupShop\CatalogBundle\PriceLevel::class)->createFromDB($activeId);
$this->activeCache[$activeId] = $this->priceLevel;
}
return $this->activeCache[$activeId];
}
public function getActive(): ?\PriceLevel
{
if (is_null($this->activeId)) {
$activeId = $this->getActiveId();
} else {
$activeId = $this->activeId;
}
if (!$activeId) {
return null;
}
return $this->getById($activeId);
}
protected function validate($id)
{
$pricelevel = sqlQueryBuilder()->select('COUNT(id) as count')
->from('price_levels')
->where('id = :id')->setParameter('id', $id)
->execute()->fetch();
if ($pricelevel['count'] == 1) {
return true;
}
return false;
}
protected function loadActive()
{
$this->loaded = true;
if (($active = $this->session->get(self::SESSION_KEY, false)) !== false) {
return $active;
}
$userContext = Contexts::get(UserContext::class);
/** @var \User|null $user */
if ($user = $userContext->getActive()) {
$priceLevelId = null;
// price level set on user
if (!empty($user->getUserPriceLevelId())) {
$priceLevelId = $user->getUserPriceLevelId();
}
// price level set on user group
foreach ($user->getGroups() as $group) {
if (!empty($group['id_price_level'])) {
$priceLevelId = $group['id_price_level'];
break;
}
}
// save price level id to session
$this->session->set(self::SESSION_KEY, $priceLevelId);
return $priceLevelId;
}
return null;
}
public function getActiveId(): ?string
{
return $this->isEmpty() ? null : $this->activeId ??= $this->loadActive();
}
public function clearPriceLevelSession()
{
$this->session->remove(static::SESSION_KEY);
}
public function clearCache(): void
{
$this->clearPriceLevelSession();
$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;
}
/**
* @return \PriceLevel[]
*
* @throws Exception
* @throws \Doctrine\DBAL\Driver\Exception
*/
public function getSupported(): array
{
if ($this->supported === null) {
$supported = sqlQueryBuilder()->select('*')
->from('price_levels')
->execute()
->fetchAllAssociative();
$instances = [];
foreach ($supported as $pricelevel) {
$newInstance = new \PriceLevel();
foreach ($pricelevel as $key => $value) {
$newInstance->{$key} = $value;
}
$instances[$newInstance->id] = $newInstance;
}
$this->supported = $instances;
}
return $this->supported;
}
public function isValid(string $id): bool
{
return $this->validate($id);
}
#[Required]
final public function _setSession(SessionInterface $session): void
{
$this->session = $session;
}
#[Required]
final public function _setPriceLevel(\KupShop\CatalogBundle\PriceLevel $priceLevel): void
{
$this->priceLevel = $priceLevel;
}
}