diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Command/AdminNotificationCommand.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Command/AdminNotificationCommand.php | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/src/Wallabag/CoreBundle/Command/AdminNotificationCommand.php b/src/Wallabag/CoreBundle/Command/AdminNotificationCommand.php new file mode 100644 index 00000000..cfde714b --- /dev/null +++ b/src/Wallabag/CoreBundle/Command/AdminNotificationCommand.php | |||
@@ -0,0 +1,103 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Command; | ||
4 | |||
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; | ||
13 | |||
14 | class AdminNotificationCommand extends AbstractNotificationCommand | ||
15 | { | ||
16 | protected function configure() | ||
17 | { | ||
18 | $this | ||
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.') | ||
22 | ->addArgument( | ||
23 | 'title', | ||
24 | InputArgument::REQUIRED, | ||
25 | 'Title of your notification. This is required if if the type of notification is an admin one.' | ||
26 | ) | ||
27 | ->addArgument( | ||
28 | 'message', | ||
29 | InputArgument::REQUIRED, | ||
30 | 'Message of your notification. This is required if the type of notification is an admin one.' | ||
31 | ) | ||
32 | ->addOption( | ||
33 | 'link', | ||
34 | 'l', | ||
35 | InputOption::VALUE_REQUIRED, | ||
36 | 'A link to display with the notification' | ||
37 | ) | ||
38 | ; | ||
39 | parent::configure(); | ||
40 | } | ||
41 | |||
42 | protected function execute(InputInterface $input, OutputInterface $output) | ||
43 | { | ||
44 | $this->output = $output; | ||
45 | |||
46 | $username = $input->getArgument('username'); | ||
47 | |||
48 | $message = $input->getArgument('message'); | ||
49 | $title = $input->getArgument('title'); | ||
50 | |||
51 | $link = $input->getOption('link'); | ||
52 | |||
53 | if ($username) { | ||
54 | try { | ||
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)); | ||
59 | |||
60 | return 1; | ||
61 | } | ||
62 | } else { | ||
63 | $users = $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findAll(); | ||
64 | |||
65 | $output->writeln(sprintf('Sending notifications to %d user accounts. This can take some time.', count($users))); | ||
66 | |||
67 | foreach ($users as $user) { | ||
68 | $output->writeln(sprintf('Processing user %s', $user->getUsername())); | ||
69 | $this->sendNotification($user, $title, $message, $link); | ||
70 | } | ||
71 | $output->writeln('Finished sending notifications.'); | ||
72 | } | ||
73 | |||
74 | return 0; | ||
75 | } | ||
76 | |||
77 | /** | ||
78 | * @param User $user | ||
79 | * @param $title | ||
80 | * @param $message | ||
81 | * @param null $link | ||
82 | */ | ||
83 | private function sendNotification(User $user, $title, $message, $link = null) | ||
84 | { | ||
85 | $em = $this->getContainer()->get('doctrine.orm.entity_manager'); | ||
86 | |||
87 | $notification = new Notification($user); | ||
88 | $notification->setTitle($title) | ||
89 | ->setDescription($message) | ||
90 | ->setType(Notification::TYPE_ADMIN); | ||
91 | |||
92 | if ($link) { | ||
93 | $action = new InfoAction($link); | ||
94 | |||
95 | $notification->addAction($action); | ||
96 | } | ||
97 | |||
98 | $em->persist($notification); | ||
99 | $em->flush(); | ||
100 | |||
101 | $this->output->writeln(sprintf('Sent notification for user %s', $user->getUserName())); | ||
102 | } | ||
103 | } | ||