]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Command/ReleaseNotificationCommand.php
Notifications
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Command / ReleaseNotificationCommand.php
CommitLineData
378aaefb
TC
1<?php
2
3namespace Wallabag\CoreBundle\Command;
4
5use Doctrine\ORM\NoResultException;
6use Symfony\Component\Console\Input\InputArgument;
7use Symfony\Component\Console\Input\InputInterface;
8use Symfony\Component\Console\Output\OutputInterface;
9use Wallabag\CoreBundle\Entity\Notification;
10use Wallabag\CoreBundle\Notifications\Action;
11use Wallabag\UserBundle\Entity\User;
12
13class ReleaseNotificationCommand extends AbstractNotificationCommand
14{
15 /** @var OutputInterface */
16 protected $output;
17
18 protected function configure()
19 {
20 $this
21 ->setName('wallabag:notification:release')
22 ->setDescription('Emits a notification to all users to let them know of a new release')
23 ->setHelp('This command helps you send a release notification to all of the users instance, or just for one user.')
24 ->addArgument(
25 'link',
26 InputArgument::OPTIONAL,
27 'A link to display with the notification'
28 )
29 ;
30 parent::configure();
31 }
32
33 protected function execute(InputInterface $input, OutputInterface $output)
34 {
35 $this->output = $output;
36
37 $username = $input->getArgument('username');
38
39 $link = $input->getArgument('link');
40
41 if ($username) {
42 try {
43 $user = $this->getUser($username);
44 $this->sendNotification($user, $link);
45 } catch (NoResultException $e) {
46 $output->writeln(sprintf('<error>User "%s" not found.</error>', $username));
47
48 return 1;
49 }
50 } else {
51 $users = $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findAll();
52
53 $output->writeln(sprintf('Sending notifications to %d user accounts. This can take some time.', count($users)));
54
55 foreach ($users as $user) {
56 $output->writeln(sprintf('Processing user %s', $user->getUsername()));
57 $this->sendNotification($user, $link);
58 }
59 $output->writeln('Finished sending notifications.');
60 }
61
62 return 0;
63 }
64
65 /**
66 * @param User $user
67 */
68 private function sendNotification(User $user, $link)
69 {
70 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
71
72 $notification = new Notification($user);
73 $notification->setTitle('notifications.release.title')
74 ->addParameter('%version%', $this->getContainer()->getParameter('wallabag_core.version'))
75 ->setType(Notification::TYPE_RELEASE);
76 if ($link) {
77 $details = new Action();
78 $details->setType(Action::TYPE_INFO)
79 ->setLabel('notifications.release.details')
80 ->setLink($link);
81 $notification->addAction($details);
82 }
83 $em->persist($notification);
84 $em->flush();
85
86 $this->output->writeln(sprintf('Sent notification for user %s', $user->getUserName()));
87 }
88}