]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Command/TagAllCommand.php
Merge pull request #3011 from wallabag/2.3
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Command / TagAllCommand.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Command;
4
5 use Doctrine\ORM\NoResultException;
6 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7 use Symfony\Component\Console\Input\InputArgument;
8 use Symfony\Component\Console\Input\InputInterface;
9 use Symfony\Component\Console\Output\OutputInterface;
10 use Symfony\Component\Console\Style\SymfonyStyle;
11
12 class TagAllCommand extends ContainerAwareCommand
13 {
14 protected function configure()
15 {
16 $this
17 ->setName('wallabag:tag:all')
18 ->setDescription('Tag all entries using the tagging rules.')
19 ->addArgument(
20 'username',
21 InputArgument::REQUIRED,
22 'User to tag entries for.'
23 )
24 ;
25 }
26
27 protected function execute(InputInterface $input, OutputInterface $output)
28 {
29 $io = new SymfonyStyle($input, $output);
30
31 try {
32 $user = $this->getUser($input->getArgument('username'));
33 } catch (NoResultException $e) {
34 $io->error(sprintf('User "%s" not found.', $input->getArgument('username')));
35
36 return 1;
37 }
38 $tagger = $this->getContainer()->get('wallabag_core.rule_based_tagger');
39
40 $io->text(sprintf('Tagging entries for user <info>%s</info>...', $user->getUserName()));
41
42 $entries = $tagger->tagAllForUser($user);
43
44 $io->text('Persist entries... ');
45
46 $em = $this->getDoctrine()->getManager();
47 foreach ($entries as $entry) {
48 $em->persist($entry);
49 }
50 $em->flush();
51
52 $io->success('Done.');
53
54 return 0;
55 }
56
57 /**
58 * Fetches a user from its username.
59 *
60 * @param string $username
61 *
62 * @return \Wallabag\UserBundle\Entity\User
63 */
64 private function getUser($username)
65 {
66 return $this->getContainer()->get('wallabag_user.user_repository')->findOneByUserName($username);
67 }
68
69 private function getDoctrine()
70 {
71 return $this->getContainer()->get('doctrine');
72 }
73 }