From c9ad5c208aec6924cd9aa6850f0718d11be942e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Hern=C3=A1ndez=20Gil?= Date: Mon, 9 Dec 2013 20:29:26 +0100 Subject: [PATCH] SMS send service and command --- Command/SmsSendCommand.php | 53 +++++++++++++++++++++ DependencyInjection/Configuration.php | 38 +++++++++++++++ DependencyInjection/JhgNexmoExtension.php | 56 +++++++++++++++++++++++ JhgNexmoBundle.php | 13 ++++++ README.md | 5 +- Resources/config/services.yml | 6 +++ Sender/SmsSender.php | 36 +++++++++++++++ composer.json | 2 +- 8 files changed, 207 insertions(+), 2 deletions(-) create mode 100644 Command/SmsSendCommand.php create mode 100644 DependencyInjection/Configuration.php create mode 100644 DependencyInjection/JhgNexmoExtension.php create mode 100644 JhgNexmoBundle.php create mode 100644 Resources/config/services.yml create mode 100644 Sender/SmsSender.php diff --git a/Command/SmsSendCommand.php b/Command/SmsSendCommand.php new file mode 100644 index 0000000..61fd15b --- /dev/null +++ b/Command/SmsSendCommand.php @@ -0,0 +1,53 @@ + + */ +class SmsSendCommand extends ContainerAwareCommand +{ + /** + * @see Command + */ + protected function configure() + { + $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'), + )) + ->setHelp(<<nexmo:sms:send command sends a SMS message through Nexmo API + php app/console nexmo:sms:send +34666555444 MyApp "Hello World!!" +EOT + ); + } + + /** + * @see Command + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $number = $input->getArgument('number'); + $fromName = $input->getArgument('fromName'); + $message = $input->getArgument('message'); + + $sender = $this->getContainer()->get('jhg_nexmo_sms.sms.sender'); + + if($sender->send($number,$fromName,$message,null,0)) { + $output->writeln(sprintf('SMS send to %u from %s: "%s"',$number,$fromName,$message)); + } else { + $output->writeln(sprintf('There was an error sending SMS to %u from %s: "%s"',$number,$fromName,$message)); + } + } + +} diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php new file mode 100644 index 0000000..259a496 --- /dev/null +++ b/DependencyInjection/Configuration.php @@ -0,0 +1,38 @@ +root('jhg_nexmo'); + + // Here you should define the parameters that are allowed to + // configure your bundle. See the documentation linked above for + // more information on that topic. + + $rootNode + ->children() + ->scalarNode('api_key')->end() + ->scalarNode('api_secret')->end() + ->scalarNode('from_name')->end() + ->booleanNode('disable_delivery')->end() + ->end() + ; + + return $treeBuilder; + } +} diff --git a/DependencyInjection/JhgNexmoExtension.php b/DependencyInjection/JhgNexmoExtension.php new file mode 100644 index 0000000..d5f5b4e --- /dev/null +++ b/DependencyInjection/JhgNexmoExtension.php @@ -0,0 +1,56 @@ +processConfiguration($configuration, $configs); + + if (!isset($config['api_key'])) { + throw new \InvalidArgumentException('The "api_key" option must be set for JhgNexmoBundle'); + } + + if (!isset($config['api_secret'])) { + throw new \InvalidArgumentException('The "api_secret" option must be set for JhgNexmoBundle'); + } + + $container->setParameter('jhg_nexmo.api_key', $config['api_key']); + $container->setParameter('jhg_nexmo.api_secret', $config['api_secret']); + + if(isset($config['disable_delivery'])) { + $container->setParameter('jhg_nexmo.disable_delivery', $config['disable_delivery']); + } + + if(isset($config['from_name'])) { + if (strlen($config['from_name'])>11) { + throw new \InvalidArgumentException('The "jhg_nexmo.from_name" option can not be larger than 11 characters'); + } + + if (!preg_match('/^[0-9a-z]{11}$/i', $config['from_name'])) { + throw new \InvalidArgumentException('The "jhg_nexmo.from_name" option only have alphanumeric characters'); + } + + $container->setParameter('jhg_nexmo.from_name', $config['from_name']); + } else { + $container->setParameter('jhg_nexmo.from_name', 'MyAppName'); + } + + $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); + $loader->load('services.yml'); + } +} diff --git a/JhgNexmoBundle.php b/JhgNexmoBundle.php new file mode 100644 index 0000000..89fafaa --- /dev/null +++ b/JhgNexmoBundle.php @@ -0,0 +1,13 @@ + + */ +class JhgNexmoBundle extends Bundle +{ + +} diff --git a/README.md b/README.md index cac0069..f759918 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,7 @@ nexmo-bundle ============ +Integrates Nexmo libs in Symfony2 -Integrates Nexmo libs in Symfon2 +Reference +--------- +https://docs.nexmo.com/index.php/messaging-sms-api/send-message diff --git a/Resources/config/services.yml b/Resources/config/services.yml new file mode 100644 index 0000000..94e12ed --- /dev/null +++ b/Resources/config/services.yml @@ -0,0 +1,6 @@ +parameters: + +services: + jhg_nexmo.sms.sender: + class: Jhg\NexmoBundle\Sender\SmsSender + arguments: [@service_container] diff --git a/Sender/SmsSender.php b/Sender/SmsSender.php new file mode 100644 index 0000000..7cdfcee --- /dev/null +++ b/Sender/SmsSender.php @@ -0,0 +1,36 @@ +container = $container; + + $api_key = $this->container->getParameter('jhg_nexmo.api_key'); + $api_secret = $this->container->getParameter('jhg_nexmo.api_secret'); + + $this->nexmoMessage = new NexmoMessage($api_key, $api_secret); + } + + public function send($number,$fromName=null,$message,$unicode=null, $status_report_req=0) { + + if($fromName===null) + $fromName = $this->container->getParameter('jhg_nexmo.from_name'); + + return $this->nexmoMessage->sendText($number,$fromName,$message,$unicode,$status_report_req); + } +} diff --git a/composer.json b/composer.json index 7cbf4b9..0fb0869 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "authors": [ { - "name": "Javier Hernández Gil", + "name": "Javi Hernández Gil", "homepage": "https://github.com/javihernandezgil/nexmo-bundle" } ],