mirror of
https://github.com/wallabag/wallabag.git
synced 2025-10-15 19:42:08 +00:00
Remove ContainerAwareCommand
from commands
And use DI to retrieve services in commands (except for `RedisWorkerCommand` where the container is injected, hard to find a better way, at least for now).
This commit is contained in:
parent
9c16dd7bd1
commit
5832482a10
14 changed files with 290 additions and 169 deletions
|
@ -4,7 +4,7 @@ namespace Wallabag\CoreBundle\Command;
|
|||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\NoResultException;
|
||||
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
@ -14,12 +14,22 @@ use Wallabag\CoreBundle\Repository\EntryRepository;
|
|||
use Wallabag\UserBundle\Entity\User;
|
||||
use Wallabag\UserBundle\Repository\UserRepository;
|
||||
|
||||
class CleanDuplicatesCommand extends ContainerAwareCommand
|
||||
class CleanDuplicatesCommand extends Command
|
||||
{
|
||||
/** @var SymfonyStyle */
|
||||
protected $io;
|
||||
protected SymfonyStyle $io;
|
||||
protected int $duplicates = 0;
|
||||
private EntityManagerInterface $entityManager;
|
||||
private EntryRepository $entryRepository;
|
||||
private UserRepository $userRepository;
|
||||
|
||||
protected $duplicates = 0;
|
||||
public function __construct(EntityManagerInterface $entityManager, EntryRepository $entryRepository, UserRepository $userRepository)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
$this->entryRepository = $entryRepository;
|
||||
$this->userRepository = $userRepository;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
|
@ -52,7 +62,7 @@ class CleanDuplicatesCommand extends ContainerAwareCommand
|
|||
|
||||
$this->io->success('Finished cleaning.');
|
||||
} else {
|
||||
$users = $this->getContainer()->get(UserRepository::class)->findAll();
|
||||
$users = $this->userRepository->findAll();
|
||||
|
||||
$this->io->text(sprintf('Cleaning through <info>%d</info> user accounts', \count($users)));
|
||||
|
||||
|
@ -68,10 +78,7 @@ class CleanDuplicatesCommand extends ContainerAwareCommand
|
|||
|
||||
private function cleanDuplicates(User $user)
|
||||
{
|
||||
$em = $this->getContainer()->get(EntityManagerInterface::class);
|
||||
$repo = $this->getContainer()->get(EntryRepository::class);
|
||||
|
||||
$entries = $repo->findAllEntriesIdAndUrlByUserId($user->getId());
|
||||
$entries = $this->entryRepository->findAllEntriesIdAndUrlByUserId($user->getId());
|
||||
|
||||
$duplicatesCount = 0;
|
||||
$urls = [];
|
||||
|
@ -82,8 +89,8 @@ class CleanDuplicatesCommand extends ContainerAwareCommand
|
|||
if (\in_array($url, $urls, true)) {
|
||||
++$duplicatesCount;
|
||||
|
||||
$em->remove($repo->find($entry['id']));
|
||||
$em->flush(); // Flushing at the end of the loop would require the instance not being online
|
||||
$this->entityManager->remove($this->entryRepository->find($entry['id']));
|
||||
$this->entityManager->flush(); // Flushing at the end of the loop would require the instance not being online
|
||||
} else {
|
||||
$urls[] = $entry['url'];
|
||||
}
|
||||
|
@ -112,6 +119,6 @@ class CleanDuplicatesCommand extends ContainerAwareCommand
|
|||
*/
|
||||
private function getUser($username)
|
||||
{
|
||||
return $this->getContainer()->get(UserRepository::class)->findOneByUserName($username);
|
||||
return $this->userRepository->findOneByUserName($username);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue