]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Command/CleanDuplicatesCommand.php
php-cs-fixer
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Command / CleanDuplicatesCommand.php
index 65f35d8e6f9099418282dfda5c4e3a4f118260b4..99170967018af889b1cf1e13d5beb5cb110308c5 100644 (file)
@@ -7,13 +7,14 @@ use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
 use Symfony\Component\Console\Input\InputArgument;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Style\SymfonyStyle;
 use Wallabag\CoreBundle\Entity\Entry;
 use Wallabag\UserBundle\Entity\User;
 
 class CleanDuplicatesCommand extends ContainerAwareCommand
 {
-    /** @var OutputInterface */
-    protected $output;
+    /** @var SymfonyStyle */
+    protected $io;
 
     protected $duplicates = 0;
 
@@ -32,7 +33,7 @@ class CleanDuplicatesCommand extends ContainerAwareCommand
 
     protected function execute(InputInterface $input, OutputInterface $output)
     {
-        $this->output = $output;
+        $this->io = new SymfonyStyle($input, $output);
 
         $username = $input->getArgument('username');
 
@@ -41,20 +42,22 @@ class CleanDuplicatesCommand extends ContainerAwareCommand
                 $user = $this->getUser($username);
                 $this->cleanDuplicates($user);
             } catch (NoResultException $e) {
-                $output->writeln(sprintf('<error>User "%s" not found.</error>', $username));
+                $this->io->error(sprintf('User "%s" not found.', $username));
 
                 return 1;
             }
+
+            $this->io->success('Finished cleaning.');
         } else {
-            $users = $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findAll();
+            $users = $this->getContainer()->get('wallabag_user.user_repository')->findAll();
 
-            $output->writeln(sprintf('Cleaning through %d user accounts', count($users)));
+            $this->io->text(sprintf('Cleaning through <info>%d</info> user accounts', \count($users)));
 
             foreach ($users as $user) {
-                $output->writeln(sprintf('Processing user %s', $user->getUsername()));
+                $this->io->text(sprintf('Processing user <info>%s</info>', $user->getUsername()));
                 $this->cleanDuplicates($user);
             }
-            $output->writeln(sprintf('Finished cleaning. %d duplicates found in total', $this->duplicates));
+            $this->io->success(sprintf('Finished cleaning. %d duplicates found in total', $this->duplicates));
         }
 
         return 0;
@@ -66,9 +69,9 @@ class CleanDuplicatesCommand extends ContainerAwareCommand
     private function cleanDuplicates(User $user)
     {
         $em = $this->getContainer()->get('doctrine.orm.entity_manager');
-        $repo = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
+        $repo = $this->getContainer()->get('wallabag_core.entry_repository');
 
-        $entries = $repo->getAllEntriesIdAndUrl($user->getId());
+        $entries = $repo->findAllEntriesIdAndUrlByUserId($user->getId());
 
         $duplicatesCount = 0;
         $urls = [];
@@ -76,7 +79,7 @@ class CleanDuplicatesCommand extends ContainerAwareCommand
             $url = $this->similarUrl($entry['url']);
 
             /* @var $entry Entry */
-            if (in_array($url, $urls)) {
+            if (\in_array($url, $urls, true)) {
                 ++$duplicatesCount;
 
                 $em->remove($repo->find($entry['id']));
@@ -88,13 +91,13 @@ class CleanDuplicatesCommand extends ContainerAwareCommand
 
         $this->duplicates += $duplicatesCount;
 
-        $this->output->writeln(sprintf('Cleaned %d duplicates for user %s', $duplicatesCount, $user->getUserName()));
+        $this->io->text(sprintf('Cleaned <info>%d</info> duplicates for user <info>%s</info>', $duplicatesCount, $user->getUserName()));
     }
 
     private function similarUrl($url)
     {
-        if (in_array(substr($url, -1), ['/', '#'])) { // get rid of "/" and "#" and the end of urls
-            return substr($url, 0, strlen($url));
+        if (\in_array(substr($url, -1), ['/', '#'], true)) { // get rid of "/" and "#" and the end of urls
+            return substr($url, 0, \strlen($url));
         }
 
         return $url;
@@ -109,11 +112,6 @@ class CleanDuplicatesCommand extends ContainerAwareCommand
      */
     private function getUser($username)
     {
-        return $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findOneByUserName($username);
-    }
-
-    private function getDoctrine()
-    {
-        return $this->getContainer()->get('doctrine');
+        return $this->getContainer()->get('wallabag_user.user_repository')->findOneByUserName($username);
     }
 }