Skip to content

Commit

Permalink
✨ Added artisan commands to:
Browse files Browse the repository at this point in the history
- create a Nexmo application.
- link a Nexmo number to an application.
  • Loading branch information
tjlytle committed Apr 19, 2017
1 parent cd00813 commit 1761ccb
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 1 deletion.
70 changes: 70 additions & 0 deletions app/Console/Commands/AppCreate.php
@@ -0,0 +1,70 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Nexmo\Client;
use Nexmo\Application\Application;
use Nexmo\Application\VoiceConfig;

class AppCreate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'nexmo:app:create {name} {answer_url} {event_url} {--type=voice} {--answer_method} {--event_method} {--keyfile=nexmo.key}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new Nexmo Application';

/**
* @var Client
*/
protected $client;

/**
* Create a new command instance.
*
* @param Client $client
* @return void
*/
public function __construct(Client $client)
{
$this->client = $client;
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$application = new Application();
$application->setName($this->argument('name'));

$config = new VoiceConfig();
$config->setWebhook(VoiceConfig::ANSWER, $this->argument('answer_url'), $this->option('answer_method'));
$config->setWebhook(VoiceConfig::EVENT, $this->argument('event_url'), $this->option('event_method'));

$application->setVoiceConfig($config);

try {
$this->info('Making API Request');
$application = $this->client->applications()->post($application);
file_put_contents($this->option('keyfile'), $application->getPrivateKey());
$this->info('Key Saved To: ' . realpath($this->option('keyfile')));
$this->info('Created Application: ' . $application->getId());
} catch (\Exception $e) {
$this->error('Request Failed: ' . $e->getMessage());
$this->error($e->getTraceAsString(), \Symfony\Component\Console\Output\OutputInterface::VERBOSITY_DEBUG);
}
}
}
64 changes: 64 additions & 0 deletions app/Console/Commands/LinkApp.php
@@ -0,0 +1,64 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Nexmo\Client;
use Nexmo\Application\Application;
use Nexmo\Numbers\Number;

class LinkApp extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'nexmo:link:app {number} {app}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Link a Number to an Application';

/**
* @var Client
*/
protected $client;

/**
* Create a new command instance.
*
* @param Client $client
* @return void
*/
public function __construct(Client $client)
{
$this->client = $client;
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$application = new Application($this->argument('app'));
$number = new Number($this->argument('number'));

$number->setVoiceDestination($application);

try{
$this->info('Making API Request');
$this->client->numbers()->update($number);
$this->info('Linked Number to Application');
} catch (\Exception $e) {
$this->error('Request Failed: ' . $e->getMessage());
$this->error($e->getTraceAsString(), \Symfony\Component\Console\Output\OutputInterface::VERBOSITY_DEBUG);
}
}
}
3 changes: 2 additions & 1 deletion app/Console/Kernel.php
Expand Up @@ -13,7 +13,8 @@ class Kernel extends ConsoleKernel
* @var array
*/
protected $commands = [
//
\App\Console\Commands\AppCreate::class,
\App\Console\Commands\LinkApp::class
];

/**
Expand Down

0 comments on commit 1761ccb

Please sign in to comment.