|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Nexmo Client Library for PHP |
| 4 | + * |
| 5 | + * @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com) |
| 6 | + * @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Nexmo\Insights; |
| 10 | + |
| 11 | +use Nexmo\Client\ClientAwareInterface; |
| 12 | +use Nexmo\Client\ClientAwareTrait; |
| 13 | +use Nexmo\Client\Exception; |
| 14 | +use Zend\Diactoros\Request; |
| 15 | + |
| 16 | +/** |
| 17 | + * Class Client |
| 18 | + */ |
| 19 | +class Client implements ClientAwareInterface |
| 20 | +{ |
| 21 | + use ClientAwareTrait; |
| 22 | + |
| 23 | + public function basic($number) |
| 24 | + { |
| 25 | + $insightsResults = $this->makeRequest('/ni/basic/json', $number); |
| 26 | + |
| 27 | + $basic = new Basic($insightsResults['national_format_number']); |
| 28 | + $basic->jsonUnserialize($insightsResults); |
| 29 | + return $basic; |
| 30 | + } |
| 31 | + |
| 32 | + public function standardCNam($number) |
| 33 | + { |
| 34 | + $insightsResults = $this->makeRequest('/ni/standard/json', $number, ['cnam' => 'true']); |
| 35 | + $standard = new StandardCnam($insightsResults['national_format_number']); |
| 36 | + $standard->jsonUnserialize($insightsResults); |
| 37 | + return $standard; |
| 38 | + } |
| 39 | + |
| 40 | + public function advancedCnam($number) |
| 41 | + { |
| 42 | + $insightsResults = $this->makeRequest('/ni/advanced/json', $number, ['cnam' => 'true']); |
| 43 | + $standard = new AdvancedCnam($insightsResults['national_format_number']); |
| 44 | + $standard->jsonUnserialize($insightsResults); |
| 45 | + return $standard; |
| 46 | + } |
| 47 | + |
| 48 | + public function standard($number, $useCnam=false) |
| 49 | + { |
| 50 | + $insightsResults = $this->makeRequest('/ni/standard/json', $number); |
| 51 | + $standard = new Standard($insightsResults['national_format_number']); |
| 52 | + $standard->jsonUnserialize($insightsResults); |
| 53 | + return $standard; |
| 54 | + } |
| 55 | + |
| 56 | + public function advanced($number) |
| 57 | + { |
| 58 | + $insightsResults = $this->makeRequest('/ni/advanced/json', $number); |
| 59 | + $advanced = new Advanced($insightsResults['national_format_number']); |
| 60 | + $advanced->jsonUnserialize($insightsResults); |
| 61 | + return $advanced; |
| 62 | + } |
| 63 | + |
| 64 | + public function advancedAsync($number, $webhook) |
| 65 | + { |
| 66 | + // This method does not have a return value as it's async. If there is no exception thrown |
| 67 | + // We can assume that everything is fine |
| 68 | + $this->makeRequest('/ni/advanced/async/json', $number, ['callback' => $webhook]); |
| 69 | + } |
| 70 | + |
| 71 | + public function makeRequest($path, $number, $additionalParams = []) |
| 72 | + { |
| 73 | + if ($number instanceof Number) |
| 74 | + { |
| 75 | + $number = $number->getMsisdn(); |
| 76 | + } |
| 77 | + |
| 78 | + $queryString = http_build_query([ |
| 79 | + 'number' => $number, |
| 80 | + ] + $additionalParams); |
| 81 | + |
| 82 | + $request = new Request( |
| 83 | + \Nexmo\Client::BASE_API . $path.'?'.$queryString, |
| 84 | + 'GET', |
| 85 | + 'php://temp', |
| 86 | + [ |
| 87 | + 'Accept' => 'application/json' |
| 88 | + ] |
| 89 | + ); |
| 90 | + |
| 91 | + $response = $this->client->send($request); |
| 92 | + |
| 93 | + $insightsResults = json_decode($response->getBody()->getContents(), true); |
| 94 | + |
| 95 | + if('200' != $response->getStatusCode()){ |
| 96 | + throw $this->getException($response); |
| 97 | + } |
| 98 | + |
| 99 | + return $insightsResults; |
| 100 | + } |
| 101 | + |
| 102 | + protected function getException(ResponseInterface $response) |
| 103 | + { |
| 104 | + $body = json_decode($response->getBody()->getContents(), true); |
| 105 | + $status = $response->getStatusCode(); |
| 106 | + |
| 107 | + if($status >= 400 AND $status < 500) { |
| 108 | + $e = new Exception\Request($body['error-code-label'], $status); |
| 109 | + } elseif($status >= 500 AND $status < 600) { |
| 110 | + $e = new Exception\Server($body['error-code-label'], $status); |
| 111 | + } else { |
| 112 | + $e = new Exception\Exception('Unexpected HTTP Status Code'); |
| 113 | + throw $e; |
| 114 | + } |
| 115 | + |
| 116 | + return $e; |
| 117 | + } |
| 118 | +} |
0 commit comments