Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
Command to create Notification
Browse files Browse the repository at this point in the history
  • Loading branch information
aquibbaig committed Jul 17, 2019
1 parent 0f7fc47 commit 38ea100
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions site/app/config/services.yml
Expand Up @@ -8,6 +8,7 @@ services:
public: false
bind:
$updateProjectInfoProducer: '@old_sound_rabbit_mq.update_project_info_producer'
$notificationProducer: '@old_sound_rabbit_mq.notification_producer'

# makes classes in src/Librecores/ProjectRepoBundle available to be used as services
# this creates a service per class whose id is the fully-qualified class name
Expand Down
79 changes: 79 additions & 0 deletions site/src/Command/CreateNotificationCommand.php
@@ -0,0 +1,79 @@
<?php

namespace App\Command;

use App\Util\Notification;
use FOS\UserBundle\Model\UserManagerInterface;
use OldSound\RabbitMqBundle\RabbitMq\ProducerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;

/**
* Create a notification for a given user
*/
class CreateNotificationCommand extends Command
{
/**
* @var string
*/
protected static $defaultName = 'librecores:send-notification';

/**
* @var ProducerInterface
*/
private $producer;

/**
* @var UserManagerInterface
*/
private $userManager;

/**
* CreateNotificationCommand constructor.
*
* @param ProducerInterface $notificationProducer
* @param UserManagerInterface $userManager
*/
public function __construct(ProducerInterface $notificationProducer, UserManagerInterface $userManager)
{
parent::__construct();
$this->producer = $notificationProducer;
$this->userManager = $userManager;
}

/**
* Configuration for the Command
*/
protected function configure()
{
$this->setDescription('Send a notification to a user')
->setHelp('This command allows you to send a notification to a user');
$this->addArgument('subject', InputArgument::REQUIRED, 'Add notification subject');
$this->addArgument('message', InputArgument::REQUIRED, 'Add notification message');
$this->addArgument('type', InputArgument::REQUIRED, 'Specify notification type');
$this->addArgument('user', InputArgument::REQUIRED, 'Specify user to whom this notification will be sent to');
}

/**
* Send notification to the user
*
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$user = $this->userManager->findUserByUsername($input->getArgument('user'));

$notification = new Notification();
$notification->setSubject($input->getArgument('subject'));
$notification->setMessage($input->getArgument('message'));
$notification->setType($input->getArgument('type'));
$notification->setRecipient($user);

$this->producer->publish(serialize($notification));
}
}

?>

0 comments on commit 38ea100

Please sign in to comment.