package updates

This commit is contained in:
2018-12-19 23:27:43 -06:00
parent aa1da4d4fb
commit 1ffd21369f
1181 changed files with 51194 additions and 11046 deletions
+121
View File
@@ -0,0 +1,121 @@
<?php
/**
* Nexmo Client Library for PHP
*
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
*/
namespace Nexmo\Application;
use Nexmo\Entity\JsonUnserializableInterface;
use Nexmo\Entity\EntityInterface;
use Nexmo\Entity\JsonResponseTrait;
use Nexmo\Entity\JsonSerializableTrait;
use Nexmo\Entity\Psr7Trait;
class Application implements EntityInterface, \JsonSerializable, JsonUnserializableInterface
{
use JsonSerializableTrait;
use Psr7Trait;
use JsonResponseTrait;
protected $voiceConfig;
protected $name;
protected $keys = [];
protected $id;
public function __construct($id = null)
{
$this->id = $id;
}
public function getId()
{
return $this->id;
}
public function setVoiceConfig(VoiceConfig $config)
{
$this->voiceConfig = $config;
return $this;
}
/**
* @return VoiceConfig
*/
public function getVoiceConfig()
{
if(!isset($this->voiceConfig)){
$this->setVoiceConfig(new VoiceConfig());
$data = $this->getResponseData();
if(isset($data['voice']) AND isset($data['voice']['webhooks'])){
foreach($data['voice']['webhooks'] as $webhook){
$this->voiceConfig->setWebhook($webhook['endpoint_type'], $webhook['endpoint'], $webhook['http_method']);
}
}
}
return $this->voiceConfig;
}
public function getPublicKey()
{
if(isset($this->keys['public_key'])){
return $this->keys['public_key'];
}
}
public function getPrivateKey()
{
if(isset($this->keys['private_key'])){
return $this->keys['private_key'];
}
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->name;
}
public function jsonUnserialize(array $json)
{
$this->name = $json['name'];
$this->id = $json['id'];
$this->keys = $json['keys'];
//todo: make voice hydrate-able
$this->voiceConfig = new VoiceConfig();
if(isset($json['voice']) AND isset($json['voice']['webhooks'])){
foreach($json['voice']['webhooks'] as $webhook){
$this->voiceConfig->setWebhook($webhook['endpoint_type'], new Webhook($webhook['endpoint'], $webhook['http_method']));
}
}
}
public function jsonSerialize()
{
return [
'name' => $this->getName(),
//currently, the request data does not match the response data
'event_url' => (string) $this->getVoiceConfig()->getWebhook(VoiceConfig::EVENT),
'answer_url' => (string) $this->getVoiceConfig()->getWebhook(VoiceConfig::ANSWER),
'type' => 'voice' //currently the only type
];
}
public function __toString()
{
return (string) $this->getId();
}
}
@@ -0,0 +1,16 @@
<?php
/**
* Nexmo Client Library for PHP
*
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
*/
namespace Nexmo\Application;
use Nexmo\Entity\EntityInterface;
interface ApplicationInterface extends EntityInterface
{
public function getId();
}
+207
View File
@@ -0,0 +1,207 @@
<?php
/**
* Nexmo Client Library for PHP
*
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
*/
namespace Nexmo\Application;
use Nexmo\Client\ClientAwareInterface;
use Nexmo\Client\ClientAwareTrait;
use Nexmo\Entity\CollectionInterface;
use Nexmo\Entity\CollectionTrait;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Request;
use Nexmo\Client\Exception;
class Client implements ClientAwareInterface, CollectionInterface
{
use ClientAwareTrait;
use CollectionTrait;
public static function getCollectionName()
{
return 'applications';
}
public static function getCollectionPath()
{
return '/v1/' . self::getCollectionName();
}
public function hydrateEntity($data, $id)
{
$application = new Application($id);
$application->jsonUnserialize($data);
return $application;
}
public function get($application)
{
if(!($application instanceof Application)){
$application = new Application($application);
}
$request = new Request(
$this->getClient()->getApiUrl() . $this->getCollectionPath() . '/' . $application->getId()
,'GET'
);
$application->setRequest($request);
$response = $this->client->send($request);
$application->setResponse($response);
if($response->getStatusCode() != '200'){
throw $this->getException($response, $application);
}
return $application;
}
public function create($application)
{
return $this->post($application);
}
public function post($application)
{
if(!($application instanceof Application)){
$application = $this->createFromArray($application);
}
$body = $application->getRequestData(false);
$request = new Request(
$this->getClient()->getApiUrl() . $this->getCollectionPath()
,'POST',
'php://temp',
['content-type' => 'application/json']
);
$request->getBody()->write(json_encode($body));
$application->setRequest($request);
$response = $this->client->send($request);
$application->setResponse($response);
if($response->getStatusCode() != '201'){
throw $this->getException($response, $application);
}
return $application;
}
public function update($application, $id = null)
{
return $this->put($application, $id);
}
public function put($application, $id = null)
{
if(!($application instanceof Application)){
$application = $this->createFromArray($application);
}
if(is_null($id)){
$id = $application->getId();
}
$body = $application->getRequestData(false);
$request = new Request(
$this->getClient()->getApiUrl() . $this->getCollectionPath() . '/' . $id,
'PUT',
'php://temp',
['content-type' => 'application/json']
);
$request->getBody()->write(json_encode($body));
$application->setRequest($request);
$response = $this->client->send($request);
$application->setResponse($response);
if($response->getStatusCode() != '200'){
throw $this->getException($response, $application);
}
return $application;
}
public function delete($application)
{
if(($application instanceof Application)){
$id = $application->getId();
} else {
$id = $application;
}
$request = new Request(
$this->getClient()->getApiUrl(). $this->getCollectionPath() . '/' . $id
,'DELETE'
);
if($application instanceof Application){
$application->setRequest($request);
}
$response = $this->client->send($request);
if($application instanceof Application){
$application->setResponse($response);
}
if($response->getStatusCode() != '204'){
throw $this->getException($response, $application);
}
return true;
}
protected function getException(ResponseInterface $response, $application = null)
{
$body = json_decode($response->getBody()->getContents(), true);
$status = $response->getStatusCode();
if($status >= 400 AND $status < 500) {
$e = new Exception\Request($body['error_title'], $status);
} elseif($status >= 500 AND $status < 600) {
$e = new Exception\Server($body['error_title'], $status);
} else {
$e = new Exception\Exception('Unexpected HTTP Status Code');
throw $e;
}
//todo use interfaces here
if(($application instanceof Application) AND (($e instanceof Exception\Request) OR ($e instanceof Exception\Server))){
$e->setEntity($application);
}
return $e;
}
protected function createFromArray($array)
{
if(!is_array($array)){
throw new \RuntimeException('application must implement `' . ApplicationInterface::class . '` or be an array`');
}
foreach(['name',] as $param){
if(!isset($array[$param])){
throw new \InvalidArgumentException('missing expected key `' . $param . '`');
}
}
$application = new Application();
$application->setName($array['name']);
foreach(['event', 'answer'] as $type){
if(isset($array[$type . '_url'])){
$method = isset($array[$type . '_method']) ? $array[$type . '_method'] : null;
$application->getVoiceConfig()->setWebhook($type . '_url', new Webhook($array[$type . '_url'], $method));
}
}
return $application;
}
}
+39
View File
@@ -0,0 +1,39 @@
<?php
/**
* Nexmo Client Library for PHP
*
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
*/
namespace Nexmo\Application;
use Nexmo\Entity\FilterInterface;
/**
* Simple value object for application filtering.
*/
class Filter implements FilterInterface
{
const FORMAT = 'Y:m:d:H:i:s';
protected $start;
protected $end;
public function __construct(\DateTime $start, \DateTime $end)
{
if($start < $end){
$this->start = $start;
$this->end = $end;
} else {
$this->start = $end;
$this->end = $start;
}
}
public function getQuery()
{
return [
'date' => $this->start->format(self::FORMAT) . '-' . $this->end->format(self::FORMAT)
];
}
}
+34
View File
@@ -0,0 +1,34 @@
<?php
/**
* Nexmo Client Library for PHP
*
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
*/
namespace Nexmo\Application;
class VoiceConfig
{
const EVENT = 'event_url';
const ANSWER = 'answer_url';
protected $webhooks = [];
public function setWebhook($type, $url, $method = null)
{
if(!($url instanceof Webhook)){
$url = new Webhook($url, $method);
}
$this->webhooks[$type] = $url;
return $this;
}
public function getWebhook($type)
{
if(isset($this->webhooks[$type])){
return $this->webhooks[$type];
}
}
}
+46
View File
@@ -0,0 +1,46 @@
<?php
/**
* Nexmo Client Library for PHP
*
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
*/
namespace Nexmo\Application;
class Webhook
{
const METHOD_POST = 'POST';
const METHOD_GET = 'GET';
/**
* @var string;
*/
protected $method;
/**
* @var string
*/
protected $url;
public function __construct($url, $method = self::METHOD_POST)
{
$this->url = $url;
$this->method = $method;
}
public function getMethod()
{
return $this->method;
}
public function getUrl()
{
return $this->url;
}
public function __toString()
{
return $this->getUrl();
}
}