161 lines
3.4 KiB
PHP
161 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace KupShop\KupShopBundle\Context;
|
|
|
|
use KupShop\I18nBundle\Entity\Currency;
|
|
|
|
class CurrencyContext implements ContextInterface
|
|
{
|
|
/** @var string */
|
|
protected $activeId;
|
|
|
|
/** @var Currency[] */
|
|
protected $supported;
|
|
|
|
protected $defaultId;
|
|
|
|
/**
|
|
* @var []Currency
|
|
*/
|
|
protected $currencies;
|
|
|
|
public function __construct()
|
|
{
|
|
}
|
|
|
|
public function remember(string $id)
|
|
{
|
|
}
|
|
|
|
public function activate(string $id)
|
|
{
|
|
$this->activeId = $id;
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @return Currency
|
|
*/
|
|
public function getActive()
|
|
{
|
|
$active = $this->getActiveId();
|
|
if (!$this->validate($active)) {
|
|
$active = key($this->getSupported());
|
|
}
|
|
|
|
return $this->getSupported()[$active] ?? $this->getAll()[$active];
|
|
}
|
|
|
|
/**
|
|
* @return Currency[]
|
|
*/
|
|
public function getSupported()
|
|
{
|
|
if (!$this->supported) {
|
|
$this->supported = $this->loadSupported();
|
|
}
|
|
|
|
return $this->supported;
|
|
}
|
|
|
|
/**
|
|
* @return Currency
|
|
*/
|
|
public function getDefault()
|
|
{
|
|
return $this->getSupported()[$this->getDefaultId()];
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getDefaultId()
|
|
{
|
|
if (!isset($this->defaultId)) {
|
|
$dbcfg = \Settings::getDefault();
|
|
$this->defaultId = $dbcfg->currency_code ?? 'CZK';
|
|
}
|
|
|
|
return $this->defaultId;
|
|
}
|
|
|
|
public function getOrDefault(?string $currency): Currency
|
|
{
|
|
return $this->getAll()[$currency] ?? $this->getDefault();
|
|
}
|
|
|
|
/**
|
|
* @return bool
|
|
*/
|
|
protected function validate($id)
|
|
{
|
|
if (isAdministration()) {
|
|
return array_key_exists($id, $this->getAll());
|
|
}
|
|
|
|
return array_key_exists($id, $this->getSupported());
|
|
}
|
|
|
|
/**
|
|
* @return Currency[]
|
|
*/
|
|
protected function loadSupported()
|
|
{
|
|
return $this->getAll();
|
|
}
|
|
|
|
public function getAll()
|
|
{
|
|
if (!$this->currencies) {
|
|
$dbcfg = \Settings::getDefault();
|
|
|
|
$currency = (new Currency())
|
|
->setId($dbcfg->currency_code)
|
|
->setName('Česká koruna')
|
|
->setSymbol($dbcfg->currency)
|
|
->setPriceRound($dbcfg->price_round)
|
|
->setPriceRoundOrder($dbcfg->price_round_order)
|
|
->setPriceRoundDirection($dbcfg->price_round_direction)
|
|
->setPricePrecision($dbcfg->price_precision)
|
|
->setPriceDecimalMark($dbcfg->price_decimal_mark)
|
|
->setRate(\DecimalConstants::one());
|
|
|
|
$this->currencies = [$this->getDefaultId() => $currency];
|
|
}
|
|
|
|
return $this->currencies;
|
|
}
|
|
|
|
protected function loadActive()
|
|
{
|
|
return $this->getDefaultId();
|
|
}
|
|
|
|
public function getActiveId(): string
|
|
{
|
|
if (is_null($this->activeId)) {
|
|
$this->activeId = $this->loadActive();
|
|
}
|
|
|
|
return $this->activeId;
|
|
}
|
|
|
|
public function clearCache(): void
|
|
{
|
|
$this->activeId = null;
|
|
}
|
|
|
|
public function isValid(string $id): bool
|
|
{
|
|
return $this->validate($id);
|
|
}
|
|
|
|
public function getSourceRounding(mixed $source, array &$params)
|
|
{
|
|
if ($source instanceof \Product && is_null($params['bata']) && $source->isVirtual()) {
|
|
$params['bata'] = false;
|
|
}
|
|
}
|
|
}
|