1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-09-30 19:22:12 +00:00

Move to controller as a service

Mostly using autowiring to inject deps.
The only tricky part was for import because all producer use the same class and have a different alias. So we must write them down in the service definition, autowiring doesn't work in that case.

Usually:
- if a controller has a constructor, it means injected services are at least re-used once in actions
- otherwise, service are injected per action
This commit is contained in:
Jeremy Benoist 2022-12-19 10:37:22 +01:00
parent 39f603e015
commit 6aca334d53
No known key found for this signature in database
GPG key ID: 7168D5DD29F38552
36 changed files with 855 additions and 699 deletions

View file

@ -20,6 +20,13 @@ use Wallabag\UserBundle\Entity\User;
class FeedController extends Controller
{
private EntryRepository $entryRepository;
public function __construct(EntryRepository $entryRepository)
{
$this->entryRepository = $entryRepository;
}
/**
* Shows unread entries for current user.
*
@ -92,7 +99,7 @@ class FeedController extends Controller
*
* @return Response
*/
public function showTagsFeedAction(Request $request, User $user, Tag $tag, $page)
public function showTagsFeedAction(Request $request, User $user, Tag $tag, PreparePagerForEntries $preparePagerForEntries, $page)
{
$sort = $request->query->get('sort', 'created');
@ -115,7 +122,7 @@ class FeedController extends Controller
UrlGeneratorInterface::ABSOLUTE_URL
);
$entriesByTag = $this->get(EntryRepository::class)->findAllByTagId(
$entriesByTag = $this->entryRepository->findAllByTagId(
$user->getId(),
$tag->getId(),
$sorts[$sort]
@ -123,7 +130,7 @@ class FeedController extends Controller
$pagerAdapter = new ArrayAdapter($entriesByTag);
$entries = $this->get(PreparePagerForEntries::class)->prepare(
$entries = $preparePagerForEntries->prepare(
$pagerAdapter,
$user
);
@ -184,22 +191,20 @@ class FeedController extends Controller
*
* @return Response
*/
private function showEntries($type, User $user, $page = 1)
private function showEntries(string $type, User $user, $page = 1)
{
$repository = $this->get(EntryRepository::class);
switch ($type) {
case 'starred':
$qb = $repository->getBuilderForStarredByUser($user->getId());
$qb = $this->entryRepository->getBuilderForStarredByUser($user->getId());
break;
case 'archive':
$qb = $repository->getBuilderForArchiveByUser($user->getId());
$qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId());
break;
case 'unread':
$qb = $repository->getBuilderForUnreadByUser($user->getId());
$qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId());
break;
case 'all':
$qb = $repository->getBuilderForAllByUser($user->getId());
$qb = $this->entryRepository->getBuilderForAllByUser($user->getId());
break;
default:
throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));