Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Apr 10, 2024
1 parent 5e41328 commit 3768f42
Showing 1 changed file with 16 additions and 33 deletions.
49 changes: 16 additions & 33 deletions src/Menu/Matcher/Voter/AdminVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,22 @@ public function __construct(

public function matchItem(ItemInterface $item): ?bool
{
$admin = $item->getExtra('admin');
$request = $this->requestStack->getMainRequest();
if (null === $request) {
return null;
}

if ($admin instanceof AdminInterface
$admin = $item->getExtra('admin');
if (
$admin instanceof AdminInterface
&& $admin->hasRoute('list') && $admin->hasAccess('list')
&& null !== $request
&& $this->match($admin, $request->get('_sonata_admin'))
) {
$requestCode = $request->get('_sonata_admin');

if ($admin->getCode() === $requestCode) {
return true;
}

if ($this->hasChildren($admin)) {
$isMatch = $this->matchChildren($admin->getChildren(), $requestCode);

if (null !== $isMatch) {
return $isMatch;
}
}
return true;
}

$route = $item->getExtra('route');

if (null !== $route && null !== $request && $route === $request->get('_route')) {
if (null !== $route && $route === $request->get('_route')) {
return true;
}

Expand All @@ -66,26 +57,18 @@ public function matchItem(ItemInterface $item): ?bool
/**
* @param AdminInterface<object> $admin
*/
private function hasChildren(AdminInterface $admin): bool
private function match(AdminInterface $admin, mixed $requestCode): bool
{
return [] !== $admin->getChildren();
}

/**
* @param array<int, AdminInterface<object>> $children
*/
private function matchChildren(array $children, mixed $requestCode): ?bool
{
foreach ($children as $child) {
if ($child->getBaseCodeRoute() === $requestCode) {
return true;
}
if ($admin->getBaseCodeRoute() === $requestCode) {
return true;
}

if ($this->hasChildren($child) && true === $this->matchChildren($child->getChildren(), $requestCode)) {
foreach ($admin->getChildren() as $child) {
if ($this->match($child, $requestCode)) {
return true;
}
}

return null;
return false;
}
}

0 comments on commit 3768f42

Please sign in to comment.