Skip to content

Commit

Permalink
Fix no commerce trait with new routes index
Browse files Browse the repository at this point in the history
  • Loading branch information
maximehuran committed Sep 3, 2024
1 parent 1ca4068 commit 61b6563
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/Kernel/SyliusNoCommerceKernelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function loadRoutes(LoaderInterface $loader): RouteCollection
private function getConfig(): ConfigInterface
{
return new Config(
(array) $this->container->getParameter('monsieurbiz_sylius_nocommerce.config') ?? []
(array) ($this->container->getParameter('monsieurbiz_sylius_nocommerce.config') ?? [])
);
}

Expand All @@ -65,21 +65,21 @@ public function getRoutesToRemove(): array
{
$config = $this->getConfig();
$routesToRemove = [];
$this->routesToRemove = ConfigInterface::ROUTES_BY_GROUP;
$routesByGroup = ConfigInterface::ROUTES_BY_GROUP;

/** @deprecated */
if ($config->areCustomersAllowed()) {
unset($this->routesToRemove['customer']);
unset($routesByGroup['customer_shop'], $routesByGroup['customer_api'], $routesByGroup['customer_admin']);
}

/** @deprecated */
if ($config->areZonesAllowed()) {
unset($this->routesToRemove['zone']);
unset($routesByGroup['zone_admin'], $routesByGroup['zone_api']);
}

/** @deprecated */
if ($config->areZonesAllowed() || $config->areCountriesAllowed()) {
unset($this->routesToRemove['country']);
unset($routesByGroup['country_admin'], $routesByGroup['country_api']);
}

// Loop on settings to add routes
Expand All @@ -93,25 +93,30 @@ public function getRoutesToRemove(): array
}

foreach ($routesToEnable as $route) {
$this->enableRoute($route);
$this->enableRoute($route, $routesByGroup);
}

foreach ($this->routesToRemove as $routes) {
foreach ($routesByGroup as $routes) {
$routesToRemove = array_merge($routesToRemove, $routes);
}

return $routesToRemove;
}

private function enableRoute(string $route): void
/**
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function enableRoute(string $route, array &$routesByGroup): void
{
foreach ($this->routesToRemove as $group => $routes) {
if (false !== ($key = array_search($route, $routes, true))) {
unset($this->routesToRemove[$group][$key]);
foreach ($routesByGroup as $group => $routes) {
// Remove route from group
if (false !== ($key = array_search($route, $routes, true)) && isset($routesByGroup[$group][$key])) {
unset($routesByGroup[$group][$key]);
}

if (empty($this->routesToRemove[$group])) {
unset($this->routesToRemove[$group]);
// Remove group if empty
if (empty($routesByGroup[$group])) {
unset($routesByGroup[$group]);
}
}
}
Expand Down

0 comments on commit 61b6563

Please sign in to comment.