3 namespace Wallabag\CoreBundle\Command
;
5 use Doctrine\ORM\NoResultException
;
6 use Symfony\Component\Console\Input\InputArgument
;
7 use Symfony\Component\Console\Input\InputInterface
;
8 use Symfony\Component\Console\Input\InputOption
;
9 use Symfony\Component\Console\Output\OutputInterface
;
10 use Wallabag\CoreBundle\Entity\Notification
;
11 use Wallabag\CoreBundle\Notifications\InfoAction
;
12 use Wallabag\UserBundle\Entity\User
;
14 class AdminNotificationCommand
extends AbstractNotificationCommand
16 protected function configure()
19 ->setName('wallabag:notification:send')
20 ->setDescription('Emits a notification to all users')
21 ->setHelp('This command helps you send notifications to all of the users instance, or just for one user.')
24 InputArgument
::REQUIRED
,
25 'Title of your notification. This is required if if the type of notification is an admin one.'
29 InputArgument
::REQUIRED
,
30 'Message of your notification. This is required if the type of notification is an admin one.'
35 InputOption
::VALUE_REQUIRED
,
36 'A link to display with the notification'
42 protected function execute(InputInterface
$input, OutputInterface
$output)
44 $this->output
= $output;
46 $username = $input->getArgument('username');
48 $message = $input->getArgument('message');
49 $title = $input->getArgument('title');
51 $link = $input->getOption('link');
55 $user = $this->getUser($username);
56 $this->sendNotification($user, $title, $message, $link);
57 } catch (NoResultException
$e) {
58 $output->writeln(sprintf('<error>User "%s" not found.</error>', $username));
63 $users = $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findAll();
65 $output->writeln(sprintf('Sending notifications to %d user accounts. This can take some time.', count($users)));
67 foreach ($users as $user) {
68 $output->writeln(sprintf('Processing user %s', $user->getUsername()));
69 $this->sendNotification($user, $title, $message, $link);
71 $output->writeln('Finished sending notifications.');
83 private function sendNotification(User
$user, $title, $message, $link = null)
85 $em = $this->getContainer()->get('doctrine.orm.entity_manager');
87 $notification = new Notification($user);
88 $notification->setTitle($title)
89 ->setDescription($message)
90 ->setType(Notification
::TYPE_ADMIN
);
93 $action = new InfoAction($link);
95 $notification->addAction($action);
98 $em->persist($notification);
101 $this->output
->writeln(sprintf('Sent notification for user %s', $user->getUserName()));