]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Command/TagAllCommand.php
Fix getContainer in command
[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;
10
11class TagAllCommand extends ContainerAwareCommand
12{
13 protected function configure()
14 {
15 $this
16 ->setName('wallabag:tag:all')
17 ->setDescription('Tag all entries using the tagging rules.')
18 ->addArgument(
19 'username',
20 InputArgument::REQUIRED,
21 'User to tag entries for.'
22 )
23 ;
24 }
25
26 protected function execute(InputInterface $input, OutputInterface $output)
27 {
28 try {
29 $user = $this->getUser($input->getArgument('username'));
30 } catch (NoResultException $e) {
27ea492c 31 $output->writeln(sprintf('<error>User "%s" not found.</error>', $input->getArgument('username')));
625acf33
KG
32
33 return 1;
34 }
35 $tagger = $this->getContainer()->get('wallabag_core.rule_based_tagger');
36
4d318f37 37 $output->write(sprintf('Tagging entries for user « <comment>%s</comment> »... ', $user->getUserName()));
625acf33
KG
38
39 $entries = $tagger->tagAllForUser($user);
40
4d318f37
JB
41 $output->writeln('<info>Done.</info>');
42 $output->write(sprintf('Persist entries ... ', $user->getUserName()));
43
625acf33
KG
44 $em = $this->getDoctrine()->getManager();
45 foreach ($entries as $entry) {
46 $em->persist($entry);
47 }
48 $em->flush();
49
50 $output->writeln('<info>Done.</info>');
51 }
52
53 /**
54 * Fetches a user from its username.
55 *
56 * @param string $username
57 *
58 * @return \Wallabag\UserBundle\Entity\User
59 */
60 private function getUser($username)
61 {
03ce43d4 62 return $this->getContainer()->get('wallabag_user.user_repository')->findOneByUserName($username);
625acf33
KG
63 }
64
65 private function getDoctrine()
66 {
67 return $this->getContainer()->get('doctrine');
68 }
69}