]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Command/TagAllCommand.php
Better rendering for all core commands
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Command / TagAllCommand.php
CommitLineData
625acf33
KG
1<?php
2
3namespace Wallabag\CoreBundle\Command;
4
5use Doctrine\ORM\NoResultException;
6use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7use Symfony\Component\Console\Input\InputArgument;
8use Symfony\Component\Console\Input\InputInterface;
9use Symfony\Component\Console\Output\OutputInterface;
e1b33efb 10use Symfony\Component\Console\Style\SymfonyStyle;
625acf33
KG
11
12class 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 {
e1b33efb
NH
29 $io = new SymfonyStyle($input, $output);
30
625acf33
KG
31 try {
32 $user = $this->getUser($input->getArgument('username'));
33 } catch (NoResultException $e) {
e1b33efb 34 $io->error(sprintf('User "%s" not found.', $input->getArgument('username')));
625acf33
KG
35
36 return 1;
37 }
38 $tagger = $this->getContainer()->get('wallabag_core.rule_based_tagger');
39
e1b33efb 40 $io->text(sprintf('Tagging entries for user <info>%s</info>...', $user->getUserName()));
625acf33
KG
41
42 $entries = $tagger->tagAllForUser($user);
43
e1b33efb 44 $io->text('Persist entries... ');
4d318f37 45
625acf33
KG
46 $em = $this->getDoctrine()->getManager();
47 foreach ($entries as $entry) {
48 $em->persist($entry);
49 }
50 $em->flush();
51
e1b33efb
NH
52 $io->success('Done.');
53
54 return 0;
625acf33
KG
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 {
03ce43d4 66 return $this->getContainer()->get('wallabag_user.user_repository')->findOneByUserName($username);
625acf33
KG
67 }
68
69 private function getDoctrine()
70 {
71 return $this->getContainer()->get('doctrine');
72 }
73}