nexmo-bundle/Command/SmsSendCommand.php

59 lines
2.3 KiB
PHP
Raw Normal View History

2013-12-09 19:29:26 +00:00
<?php
namespace Jhg\NexmoBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
2014-03-30 22:20:02 +00:00
* @author Javi Hernández
2013-12-09 19:29:26 +00:00
*/
class SmsSendCommand extends ContainerAwareCommand
{
/**
* @see Command
*/
2014-03-30 22:20:02 +00:00
protected function configure() {
2013-12-09 19:29:26 +00:00
$this
->setName('nexmo:sms:send')
->setDescription('Send a SMS message')
->setDefinition(array(
new InputArgument('number', InputArgument::REQUIRED, 'The number'),
new InputArgument('fromName', InputArgument::REQUIRED, 'The name shown as origin'),
new InputArgument('message', InputArgument::REQUIRED, 'The message'),
2014-03-30 22:20:02 +00:00
new InputOption('report','r',InputOption::VALUE_OPTIONAL,'Ask for status report'),
2013-12-09 19:29:26 +00:00
))
->setHelp(<<<EOT
The <info>nexmo:sms:send</info> command sends a SMS message through Nexmo API
2014-03-30 22:20:02 +00:00
<info>php app/console nexmo:sms:send +34666555444 MyApp "Hello World!!"</info>
2013-12-09 19:29:26 +00:00
EOT
);
}
/**
* @see Command
*/
2014-03-30 22:20:02 +00:00
protected function execute(InputInterface $input, OutputInterface $output) {
2013-12-09 19:29:26 +00:00
$number = $input->getArgument('number');
$fromName = $input->getArgument('fromName');
$message = $input->getArgument('message');
2014-03-30 22:20:02 +00:00
$report = (int)$input->getOption('report');
$smsManager = $this->getContainer()->get('jhg_nexmo_sms');
if($response = $smsManager->sendText($number,$message,$fromName,$report)) {
$output->writeln(sprintf('SMS send to %s from %s: "%s"',$number,$fromName,$message));
$output->writeln(sprintf(' message id: %s',$response->getMessageId()));
$output->writeln(sprintf(' status: %s',$response->getStatus()));
$output->writeln(sprintf(' message price: %f',$response->getMessagePrice()));
$output->writeln(sprintf(' remaining balance: %f',$response->getRemainingBalance()));
$output->writeln(sprintf(' network: %u',$response->getNetwork()));
2013-12-09 19:29:26 +00:00
} else {
2014-03-30 22:20:02 +00:00
$output->writeln(sprintf('There was an error sending SMS to %s from %s: "%s"',$number,$fromName,$message));
2013-12-09 19:29:26 +00:00
}
}
}