Skip to content

Commit

Permalink
Move findProviderByCode in Browsable object provider
Browse files Browse the repository at this point in the history
  • Loading branch information
maximehuran committed Apr 9, 2024
1 parent 4a9c6f5 commit 49df1f8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
15 changes: 1 addition & 14 deletions src/Controller/BrowserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace MonsieurBiz\SyliusMenuPlugin\Controller;

use MonsieurBiz\SyliusMenuPlugin\Provider\BrowsableObjectProviderInterface;
use MonsieurBiz\SyliusMenuPlugin\Provider\UrlProviderInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -50,7 +49,7 @@ public function listItemsAction(
$inputValue = (string) $request->query->get('inputValue', '');
$locale = (string) $request->query->get('locale', '');

$urlProvider = $this->findProviderByCode($providerCode);
$urlProvider = $this->browsableObjectProvider->findProviderByCode($providerCode);
if (null === $urlProvider) {
return new JsonResponse(['error' => 'URL Provider not found'], 404);
}
Expand All @@ -62,16 +61,4 @@ public function listItemsAction(
'locale' => $locale,
]);
}

private function findProviderByCode(string $code): ?UrlProviderInterface
{
/** @var UrlProviderInterface $urlProvider */
foreach ($this->browsableObjectProvider->getUrlProviders() as $urlProvider) {
if ($urlProvider->getCode() === $code) {
return $urlProvider;
}
}

return null;
}
}
12 changes: 12 additions & 0 deletions src/Provider/BrowsableObjectProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,16 @@ public function getUrlProviders(): array

return $urlProviders;
}

public function findProviderByCode(string $code): ?UrlProviderInterface
{
/** @var UrlProviderInterface $urlProvider */
foreach ($this->getUrlProviders() as $urlProvider) {
if ($urlProvider->getCode() === $code) {
return $urlProvider;
}
}

return null;
}
}
2 changes: 2 additions & 0 deletions src/Provider/BrowsableObjectProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@
interface BrowsableObjectProviderInterface
{
public function getUrlProviders(): array;

public function findProviderByCode(string $code): ?UrlProviderInterface;
}

0 comments on commit 49df1f8

Please sign in to comment.