Skip to content

Commit

Permalink
Fix accessing session in construct is not allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
lopes-vincent authored Jul 20, 2022
1 parent e4a03fe commit fd33441
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions Model/Api/BaseApiModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ abstract class BaseApiModel implements \JsonSerializable
/** @var Request */
protected $request;

/** @var string */
protected $locale;

/** @var Country */
protected $country;

Expand Down Expand Up @@ -63,7 +60,6 @@ public function __construct(

$this->modelFactory = $modelFactory;
$this->request = $requestStack->getCurrentRequest();
$this->locale = $this->request->getSession()->getLang(true)->getLocale();
$this->country = $taxEngine->getDeliveryCountry();
$this->state = $taxEngine->getDeliveryState();

Expand All @@ -72,6 +68,11 @@ public function __construct(
}
}

public function getCurrentLocale()
{
return $this->request->getSession()->getLang(true)->getLocale();
}

/**
* @param $groups
*
Expand Down Expand Up @@ -135,7 +136,7 @@ public function jsonSerialize()
public function createOrUpdateFromData($data, $locale = null): void
{
if (null === $locale) {
$locale = $this->locale;
$locale = $this->getCurrentLocale();
}

if (\is_object($data)) {
Expand Down Expand Up @@ -213,7 +214,7 @@ public function toTheliaModel($locale = null)

// If model need locale, set it
if (method_exists($theliaModel, 'setLocale')) {
$theliaModel->setLocale($locale !== null ? $locale : $this->locale);
$theliaModel->setLocale($locale !== null ? $locale : $this->getCurrentLocale());
}

// Look all method of Open API model
Expand Down Expand Up @@ -275,7 +276,7 @@ public function toTheliaModel($locale = null)
public function createFromTheliaModel($theliaModel, $locale = null)
{
if (method_exists($theliaModel, 'setLocale')) {
$theliaModel->setLocale($locale !== null ? $locale : $this->locale);
$theliaModel->setLocale($locale !== null ? $locale : $this->getCurrentLocale());
}

foreach (get_class_methods($this) as $modelMethod) {
Expand Down

0 comments on commit fd33441

Please sign in to comment.