Skip to content

Commit

Permalink
set legacy shop contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthieu Rolland committed Oct 19, 2023
1 parent 533d6df commit a2e7341
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
8 changes: 1 addition & 7 deletions src/Adapter/LegacyContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,7 @@ public function __construct(
public function getContext()
{
if (null === static::$instance) {
$legacyContext = Context::getContext();
if ($legacyContext && !empty($legacyContext->shop) && !isset($legacyContext->controller) && isset($legacyContext->employee)) {
//init real legacy shop context
//$adminController = new AdminController();
//$adminController->initShopContext();
}
static::$instance = $legacyContext;
static::$instance = Context::getContext();
}

return static::$instance;
Expand Down
2 changes: 2 additions & 0 deletions src/Core/Context/CurrencyContextBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,7 @@ public function setCurrencyId(int $currencyId)
$legacyCurrency = $this->currencyRepository->get(new CurrencyId($this->currencyId));
$this->contextStateManager->setCurrency($legacyCurrency);
}

return $this;
}
}
22 changes: 21 additions & 1 deletion src/Core/Context/ShopContextBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@

namespace PrestaShop\PrestaShop\Core\Context;

use PrestaShop\PrestaShop\Adapter\ContextStateManager;
use PrestaShop\PrestaShop\Adapter\Shop\Repository\ShopRepository;
use PrestaShop\PrestaShop\Core\Domain\Shop\ValueObject\ShopConstraint;
use PrestaShop\PrestaShop\Core\Domain\Shop\ValueObject\ShopId;
use PrestaShop\PrestaShop\Core\Exception\InvalidArgumentException;
use Shop as LegacyShop;

Check failure on line 36 in src/Core/Context/ShopContextBuilder.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Namespace Shop is forbidden, No legacy calls inside the prestashop bundle. Please create an interface and an adapter if you need to.

Check failure on line 36 in src/Core/Context/ShopContextBuilder.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Namespace Shop is forbidden, No legacy calls inside the prestashop bundle. Please create an interface and an adapter if you need to.

/**
* @experimental Depends on ADR https://github.com/PrestaShop/ADR/pull/36
Expand All @@ -42,7 +44,8 @@ class ShopContextBuilder
private ?int $shopId = null;

public function __construct(
private readonly ShopRepository $shopRepository
private readonly ShopRepository $shopRepository,
private readonly ContextStateManager $contextStateManager
) {
}

Expand Down Expand Up @@ -82,6 +85,19 @@ public function build(): ShopContext
);
}

public function buildLegacyContext(ShopConstraint $shopConstraint): self
{
if ($shopConstraint->forAllShops()) {
$this->contextStateManager->setShopContext(LegacyShop::CONTEXT_ALL);

Check failure on line 91 in src/Core/Context/ShopContextBuilder.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Class Shop is forbidden, No legacy calls inside the prestashop bundle. Please create an interface and an adapter if you need to.

Check failure on line 91 in src/Core/Context/ShopContextBuilder.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Class Shop is forbidden, No legacy calls inside the prestashop bundle. Please create an interface and an adapter if you need to.
} else if (empty($shopConstraint->getShopId())) {
$this->contextStateManager->setShopContext(LegacyShop::CONTEXT_GROUP, $shopConstraint->getShopGroupId()->getValue());

Check failure on line 93 in src/Core/Context/ShopContextBuilder.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Class Shop is forbidden, No legacy calls inside the prestashop bundle. Please create an interface and an adapter if you need to.

Check failure on line 93 in src/Core/Context/ShopContextBuilder.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Class Shop is forbidden, No legacy calls inside the prestashop bundle. Please create an interface and an adapter if you need to.
} else {
$this->contextStateManager->setShopContext(LegacyShop::CONTEXT_SHOP, $shopConstraint->getShopId()->getValue());

Check failure on line 95 in src/Core/Context/ShopContextBuilder.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Class Shop is forbidden, No legacy calls inside the prestashop bundle. Please create an interface and an adapter if you need to.

Check failure on line 95 in src/Core/Context/ShopContextBuilder.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Class Shop is forbidden, No legacy calls inside the prestashop bundle. Please create an interface and an adapter if you need to.
}

return $this;
}

public function setShopId(int $shopId): self
{
$this->shopId = $shopId;
Expand All @@ -95,4 +111,8 @@ public function setShopConstraint(ShopConstraint $shopConstraint): self

return $this;
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

use PrestaShop\PrestaShop\Adapter\Feature\MultistoreFeature;
use PrestaShop\PrestaShop\Adapter\LegacyContext;
use PrestaShop\PrestaShop\Adapter\Shop\Context as ShopContextAdapter;
use PrestaShop\PrestaShop\Core\Context\EmployeeContext;
use PrestaShop\PrestaShop\Core\Context\ShopContextBuilder;
use PrestaShop\PrestaShop\Core\Domain\Configuration\ShopConfigurationInterface;
Expand All @@ -48,7 +47,6 @@ public function __construct(
private readonly ShopConfigurationInterface $configuration,
private readonly LegacyContext $legacyContext,
private readonly MultistoreFeature $multistoreFeature,
private readonly ShopContextAdapter $shopContextAdapter
) {
}

Expand All @@ -65,9 +63,9 @@ public function onKernelRequest(RequestEvent $event): void
$shopConstraint = ShopConstraint::allShops();
$cookieShopConstraint = $this->getShopConstraintFromCookie();

if (!empty($event->getRequest()->get('setShopContext'))) {
/*if (!empty($event->getRequest()->get('setShopContext'))) {
$this->changeShopContext($event->getRequest()->get('setShopContext'));
}
}*/

if ($cookieShopConstraint && $cookieShopConstraint->getShopGroupId()) {
// Check if the employee has permission on selected group if not fallback on single shop context with employee's default shop
Expand All @@ -93,13 +91,7 @@ public function onKernelRequest(RequestEvent $event): void
$this->shopContextBuilder->setShopId($shopConstraint->getShopId()->getValue());
}

/*if ($shopConstraint->forAllShops()) {
$this->shopContextAdapter->setAllContext((int) $this->configuration->get('PS_SHOP_DEFAULT', null, ShopConstraint::allShops()));
} else if (empty($shopConstraint->getShopId())) {
$this->shopContextAdapter->setShopGroupContext($shopConstraint->getShopGroupId()->getValue());
} else {
$this->shopContextAdapter->setShopContext($shopConstraint->getShopId()->getValue());
} */
$this->shopContextBuilder->buildLegacyContext($shopConstraint);
}

/**
Expand Down

0 comments on commit a2e7341

Please sign in to comment.