aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Command
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/CoreBundle/Command')
-rw-r--r--src/Wallabag/CoreBundle/Command/AbstractNotificationCommand.php41
-rw-r--r--src/Wallabag/CoreBundle/Command/AdminNotificationCommand.php103
-rw-r--r--src/Wallabag/CoreBundle/Command/ReleaseNotificationCommand.php88
3 files changed, 232 insertions, 0 deletions
diff --git a/src/Wallabag/CoreBundle/Command/AbstractNotificationCommand.php b/src/Wallabag/CoreBundle/Command/AbstractNotificationCommand.php
new file mode 100644
index 00000000..b40b589a
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Command/AbstractNotificationCommand.php
@@ -0,0 +1,41 @@
1<?php
2
3namespace Wallabag\CoreBundle\Command;
4
5use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6use Symfony\Component\Console\Input\InputArgument;
7use Symfony\Component\Console\Output\OutputInterface;
8
9abstract class AbstractNotificationCommand extends ContainerAwareCommand
10{
11 /** @var OutputInterface */
12 protected $output;
13
14 protected function configure()
15 {
16 $this
17 ->addArgument(
18 'username',
19 InputArgument::OPTIONAL,
20 'User to send the notification to'
21 )
22 ;
23 }
24
25 /**
26 * Fetches a user from its username.
27 *
28 * @param string $username
29 *
30 * @return \Wallabag\UserBundle\Entity\User
31 */
32 protected function getUser($username)
33 {
34 return $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findOneByUserName($username);
35 }
36
37 protected function getDoctrine()
38 {
39 return $this->getContainer()->get('doctrine');
40 }
41}
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
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\Input\InputOption;
9use Symfony\Component\Console\Output\OutputInterface;
10use Wallabag\CoreBundle\Entity\Notification;
11use Wallabag\CoreBundle\Notifications\InfoAction;
12use Wallabag\UserBundle\Entity\User;
13
14class 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}
diff --git a/src/Wallabag/CoreBundle/Command/ReleaseNotificationCommand.php b/src/Wallabag/CoreBundle/Command/ReleaseNotificationCommand.php
new file mode 100644
index 00000000..cd9c61a3
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Command/ReleaseNotificationCommand.php
@@ -0,0 +1,88 @@
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}