227 lines
5.9 KiB
PHP
227 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace KupShop\KupShopBundle\Context;
|
|
|
|
use KupShop\KupShopBundle\PriceType\NullPriceType;
|
|
use KupShop\KupShopBundle\PriceType\PriceForDiscountPriceType;
|
|
use KupShop\KupShopBundle\PriceType\PriceTypeInterface;
|
|
use KupShop\KupShopBundle\PriceType\PriceWithoutDiscountPriceType;
|
|
use KupShop\ProductReservationBundle\Util\ProductReservationUtil;
|
|
use KupShop\UserBundle\Security\User;
|
|
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
|
use Symfony\Contracts\Service\Attribute\Required;
|
|
|
|
class UserContext implements ContextInterface, EmptiableContext
|
|
{
|
|
private TokenStorageInterface $tokenStorage;
|
|
private RequestStack $requestStack;
|
|
protected ?\User $activeUser = null;
|
|
private bool $loaded = false;
|
|
|
|
protected PriceTypeInterface $retailPriceType;
|
|
protected PriceTypeInterface $originalPriceType;
|
|
|
|
public function __construct()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @param \User|string|null $id DEJ MI CELÝHO USERA!!!
|
|
*/
|
|
public function activate(\User|string|null $id)
|
|
{
|
|
if (null === $id) {
|
|
trigger_error('Passing $id=null into ContextInterface::activate is deprecated! Use ::clearCache() instead.', \E_USER_DEPRECATED);
|
|
}
|
|
|
|
if (is_string($id)) {
|
|
$id = \User::createFromId($id);
|
|
}
|
|
|
|
$this->activeUser = $id;
|
|
|
|
return $this->loaded = (bool) $id;
|
|
}
|
|
|
|
public function getActive()
|
|
{
|
|
if (is_null($this->activeUser) && !$this->loaded) {
|
|
$this->activeUser = $this->loadActive();
|
|
}
|
|
|
|
return $this->activeUser;
|
|
}
|
|
|
|
public function getActiveId()
|
|
{
|
|
if ($user = $this->getActive()) {
|
|
return (int) $user->id;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
protected function loadActive()
|
|
{
|
|
$user = null;
|
|
|
|
if ($this->isActive()) {
|
|
$user = \User::createFromLogin($this->tokenStorage->getToken()->getUserIdentifier()) ?? null;
|
|
}
|
|
|
|
return $user;
|
|
}
|
|
|
|
public function isActive()
|
|
{
|
|
$token = $this->tokenStorage->getToken();
|
|
|
|
return $token?->getUser() instanceof User;
|
|
}
|
|
|
|
/**
|
|
* @deprecated Use isType('b2b') instead
|
|
*/
|
|
public function isDealer()
|
|
{
|
|
if (!findModule(\Modules::B2B)) {
|
|
if ($this->getActive()) {
|
|
if ($this->getActive()->getPriceLevel()) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $this->isType('b2b');
|
|
}
|
|
|
|
public function isType(string $type): bool
|
|
{
|
|
if ($this->getActive()) {
|
|
return $this->getActive()->isType($type);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function getTypes(): array
|
|
{
|
|
return $this->getActive()?->types ?? [];
|
|
}
|
|
|
|
public function getRetailPriceType(): PriceTypeInterface
|
|
{
|
|
return $this->retailPriceType ?? $this->retailPriceType = $this->loadRetailPriceType();
|
|
}
|
|
|
|
protected function loadRetailPriceType(): PriceTypeInterface
|
|
{
|
|
return new NullPriceType();
|
|
}
|
|
|
|
public function getOriginalPriceType(): PriceTypeInterface
|
|
{
|
|
return $this->originalPriceType ?? $this->originalPriceType = $this->loadOriginalPriceType();
|
|
}
|
|
|
|
protected function loadOriginalPriceType(): PriceTypeInterface
|
|
{
|
|
if (findModule(\Modules::PRICE_HISTORY)) {
|
|
return new PriceForDiscountPriceType();
|
|
}
|
|
|
|
return new PriceWithoutDiscountPriceType();
|
|
}
|
|
|
|
public function getSupported(): null
|
|
{
|
|
return null;
|
|
}
|
|
|
|
public function clearCache(): void
|
|
{
|
|
$this->loaded = false;
|
|
$this->activeUser = null;
|
|
unset($this->retailPriceType);
|
|
unset($this->originalPriceType);
|
|
}
|
|
|
|
public function forceEmpty(): void
|
|
{
|
|
$this->loaded = true;
|
|
$this->activeUser = null;
|
|
unset($this->retailPriceType);
|
|
unset($this->originalPriceType);
|
|
}
|
|
|
|
public function isEmpty(): bool
|
|
{
|
|
return $this->loaded && $this->activeUser === null;
|
|
}
|
|
|
|
public function isValid(\User|string $id): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function isVatPayer(): bool
|
|
{
|
|
if (!isAdministration()) {
|
|
$cart = null;
|
|
try {
|
|
// TODO: remove HOTFIX prasárna
|
|
$req = $this->requestStack->getCurrentRequest();
|
|
if (isset($req) && str_starts_with($req->getPathInfo(), '/feed/')) {
|
|
return false;
|
|
}
|
|
|
|
if ($cart = $this->requestStack->getSession()->get('cart')) {
|
|
$cart = unserialize($cart);
|
|
}
|
|
} catch (SessionNotFoundException|\RuntimeException) {
|
|
}
|
|
|
|
if ($cart) {
|
|
return !empty($cart['invoice']['dic']);
|
|
}
|
|
}
|
|
|
|
if ($user = $this->getActive()) {
|
|
return !empty($user['dic']);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Returns ignored reservation types (see `ProductReservationUtil` constants).
|
|
*
|
|
* If a reservation type is returned, then the type is ignored in `Query\Product::getInStoreField` and is not included in the reservation total quantity.
|
|
*/
|
|
public function getIgnoredReservationTypes(): array
|
|
{
|
|
$ignored = [];
|
|
|
|
// if user is not b2b, so retail (MOC) reserve should be ignored
|
|
if (!$this->isType('b2b')) {
|
|
$ignored[] = ProductReservationUtil::TYPE_RETAIL_RESERVE;
|
|
}
|
|
|
|
return $ignored;
|
|
}
|
|
|
|
#[Required]
|
|
final public function _setTokenStorage(TokenStorageInterface $tokenStorage): void
|
|
{
|
|
$this->tokenStorage = $tokenStorage;
|
|
}
|
|
|
|
#[Required]
|
|
final public function setRequestStack(RequestStack $requestStack): void
|
|
{
|
|
$this->requestStack = $requestStack;
|
|
}
|
|
}
|