Skip to content

Commit 8278cf2

Browse files
authored
Merge pull request #45 from Bandwidth/DX-679
Dx 679
2 parents cc46b89 + 43b6e79 commit 8278cf2

File tree

5 files changed

+196
-51
lines changed

5 files changed

+196
-51
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ PHP Client library for Bandwidth's Phone Number Dashboard (AKA: Dashboard, Iris)
1212
| 2.0.3 | Fixed HTTP request for `set_tn_options` to the correct XML object |
1313
| 2.0.4 | Added `localVanity` to `availableNumbers` |
1414
| 2.0.5 | Added `NewBillingTelephoneNumber` to `Portins` model |
15+
| 2.0.6 | Build `ReportsModel` functionality |
1516

1617
## Supported PHP Versions
1718

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Bandwidth's Iris SDK for PHP",
55
"keywords": ["iris","sdk","php"],
66
"homepage": "http://dev.bandwidth.com",
7-
"reference": "v2.0.5",
7+
"reference": "v2.0.6",
88
"license": "MIT",
99
"authors": [
1010
],

src/ReportsInstanceModel.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/**
4+
* @model ReportInstance
5+
* https://api.test.inetwork.com/v1.0/accounts/reports/{id}/instances
6+
*
7+
*
8+
*
9+
* provides:
10+
* get/0
11+
*
12+
*/
13+
14+
namespace Iris;
15+
16+
final class InstanceParameters {
17+
use BaseModel;
18+
19+
protected $fields = array(
20+
"Parameter" => array("type" => "\Iris\InstanceParameter")
21+
);
22+
public function __construct($data) {
23+
$this->set_data($data);
24+
}
25+
}
26+
27+
final class InstanceParameter {
28+
use BaseModel;
29+
30+
protected $fields = array(
31+
"Name" => array("type" => "string"),
32+
"Value" => array("type" => "string"),
33+
);
34+
public function __construct($data) {
35+
$this->set_data($data);
36+
}
37+
}
38+
39+
final class ReportInstance extends RestEntry{
40+
use BaseModel;
41+
42+
protected $fields = array(
43+
"Id" => array("type" => "string"),
44+
"ReportId" => array("type" => "string"),
45+
"ReportName" => array("type" => "string"),
46+
"OutputFormat" => array("type" => "string"),
47+
"RequestedByUserName" => array("type" => "string"),
48+
"RequestedAt" => array("type" => "string"),
49+
"Parameters" => array("type" => "\Iris\InstanceParameters"),
50+
"Status" => array("type" => "string"),
51+
"ExpiresAt" => array("type" => "string"),
52+
);
53+
54+
public function __construct($report, $data)
55+
{
56+
if(isset($data)) {
57+
if(is_object($data) && $data->Id)
58+
$this->id = $data->Id;
59+
if(is_array($data) && isset($data['Id']))
60+
$this->id = $data['Id'];
61+
}
62+
$this->set_data($data);
63+
64+
if(!is_null($report)) {
65+
$this->parent = $report;
66+
parent::_init($report->get_rest_client(), $report->get_relative_namespace());
67+
}
68+
}
69+
70+
}

src/ReportsModel.php

Lines changed: 71 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@
1313

1414
namespace Iris;
1515

16+
require_once("ReportsInstanceModel.php");
17+
1618
final class Reports extends RestEntry {
1719

1820
public function __construct($parent) {
1921
$this->parent = $parent;
2022
parent::_init($this->parent->get_rest_client(), $this->parent->get_relative_namespace());
2123
}
2224

23-
public function getList($filters = Array()) {
25+
public function getList($filters = array()) {
2426

2527
$reports = [];
2628

27-
$data = parent::_get('reports', $filters, Array("page"=> 1, "size" => 30), Array("page", "size"));
28-
print_r($data); exit;
29-
/* TODO: correct struct */
30-
if($data['ListOrderIdUserIdDate'] && $data['ListOrderIdUserIdDate']['TotalCount']) {
31-
foreach($data['ListOrderIdUserIdDate']['OrderIdUserIdDate'] as $report) {
29+
$data = parent::_get('reports', $filters, array("page"=> 1, "size" => 30), array("page", "size"));
30+
if($data['Reports']) {
31+
foreach($data['Reports']['Report'] as $report) {
3232
$reports[] = new Report($this, $report);
3333
}
3434
}
@@ -45,19 +45,68 @@ public function get_by_id($id) {
4545
public function get_appendix() {
4646
return '/reports';
4747
}
48+
}
49+
50+
final class ReportValue {
51+
use BaseModel;
4852

53+
protected $fields = array(
54+
"InternalName" => array("type" => "string"),
55+
"DisplayName" => array("type" => "string"),
56+
);
57+
public function __construct($data) {
58+
$this->set_data($data);
59+
}
4960
}
5061

51-
final class Report extends RestEntry{
62+
final class ReportValues {
5263
use BaseModel;
5364

5465
protected $fields = array(
55-
"orderId" => array(
56-
"type" => "string"
57-
),
58-
/* TODO: fill fields */
66+
"Value" => array("type" => "\Iris\ReportValue")
5967
);
68+
public function __construct($data) {
69+
$this->set_data($data);
70+
}
71+
}
6072

73+
final class ReportParameter {
74+
use BaseModel;
75+
76+
protected $fields = array(
77+
"Name" => array("type" => "string"),
78+
"Type" => array("type" => "string"),
79+
"Required" => array("type" => "string"),
80+
"Description" => array("type" => "string"),
81+
"MultiSelectAllowed" => array("type" => "string"),
82+
"HelpInformation" => array("type" => "string"),
83+
"Values" => array("type" => "\Iris\ReportValues"),
84+
);
85+
public function __construct($data) {
86+
$this->set_data($data);
87+
}
88+
}
89+
90+
final class ReportParameters {
91+
use BaseModel;
92+
93+
protected $fields = array(
94+
"Parameter" => array("type" => "\Iris\ReportParameter")
95+
);
96+
public function __construct($data) {
97+
$this->set_data($data);
98+
}
99+
}
100+
101+
final class Report extends RestEntry{
102+
use BaseModel;
103+
104+
protected $fields = array(
105+
"Id" => array("type" => "string"),
106+
"Name" => array("type" => "string"),
107+
"Description" => array("type" => "string"),
108+
"Parameters" => array("type" => "\Iris\ReportParameters"),
109+
);
61110

62111
public function __construct($reports, $data)
63112
{
@@ -80,7 +129,7 @@ public function get() {
80129
throw new \Exception('Id should be provided');
81130

82131
$data = parent::_get($this->id);
83-
$this->set_data($data['Order']);
132+
$this->set_data($data['Report']);
84133
}
85134

86135
public function areaCodes()
@@ -90,55 +139,27 @@ public function areaCodes()
90139
return $data;
91140
}
92141

93-
public function instances()
142+
public function instances($filters = array())
94143
{
95144
$rep_instances = [];
96145

97-
$data = parent::_get('reports/{$this->id}/instances', $filters, Array("page"=> 1, "size" => 30), Array("page", "size"));
146+
$data = parent::_get($this->Id.'/instances', $filters, array("page"=> 1, "size" => 30), array("page", "size"));
98147

99-
if($data['ListOrderIdUserIdDate'] && $data['ListOrderIdUserIdDate']['TotalCount']) {
100-
foreach($data['ListOrderIdUserIdDate']['OrderIdUserIdDate'] as $instance) {
101-
$rep_instances[] = new ReportInctace($this, $instance);
148+
if($data['Instances']) {
149+
foreach($data['Instances']['Instance'] as $instance) {
150+
$rep_instances[] = new ReportInstance($this, $instance);
102151
}
103152
}
104153

105154
return $rep_instances;
106155
}
107-
}
108-
109-
110-
final class ReportInstance extends RestEntry{
111-
use BaseModel;
112-
113-
protected $fields = array(
114-
"orderId" => array(
115-
"type" => "string"
116-
),
117-
/* TODO: fill fields */
118-
);
119-
120-
121-
public function __construct($report, $data)
122-
{
123-
if(isset($data)) {
124-
if(is_object($data) && $data->Id)
125-
$this->id = $data->Id;
126-
if(is_array($data) && isset($data['Id']))
127-
$this->id = $data['Id'];
128-
}
129-
$this->set_data($data);
130156

131-
if(!is_null($report)) {
132-
$this->parent = $report;
133-
parent::_init($report->get_rest_client(), $report->get_relative_namespace());
134-
}
157+
public function modifyInstance($instance) {
158+
$response = parent::post($this->Id."/instances/".$instance->Id, "Instance", $this->to_array());
159+
return $response; //this api endpoint returns a response header
135160
}
136161

137-
public function get() {
138-
if(is_null($this->id))
139-
throw new \Exception('Id should be provided');
140-
141-
$data = parent::_get($this->id);
142-
$this->set_data($data['Order']);
162+
public function get_appendix() {
163+
return $this->parent->get_appendix();
143164
}
144165
}

tests/ReportsTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
use GuzzleHttp\Handler\MockHandler;
4+
use GuzzleHttp\HandlerStack;
5+
use GuzzleHttp\Psr7\Response;
6+
use GuzzleHttp\Middleware;
7+
8+
class ReportsTest extends PHPUnit_Framework_TestCase {
9+
public static $container;
10+
public static $reports;
11+
public static $index = 0;
12+
13+
public static function setUpBeforeClass() {
14+
$mock = new MockHandler([
15+
//GET report
16+
new Response(200, [], '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ReportsResponse><Reports><Report><Name>Sample Report 1</Name><Id>100020</Id><Description>Sample Report 1 Description</Description></Report><Report><Name>Sample Report 2</Name><Id>100021</Id><Description>Sample Report 2 Description</Description></Report></Reports></ReportsResponse>'),
17+
//GET report by id
18+
new Response(200, [], '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ReportsResponse><Report><Id>123</Id><Name>Sample Report 1</Name><Parameters><Parameter><Name>Report Parameter 1</Name><Type>Enum</Type><Required>false</Required><ValueFilter>Value1</ValueFilter><Values><Value><InternalName>Value1</InternalName><DisplayName>Display Value1</DisplayName></Value></Values><Description>Report Parameter 1 Description</Description><MultiSelectAllowed>true</MultiSelectAllowed><HelpInformation>Report Parameter 1 Help Text</HelpInformation></Parameter><Parameter><Name>Report Parameter 2</Name></Parameter></Parameters></Report></ReportsResponse>'),
19+
//GET instances for report
20+
new Response(200, [], '<ReportInstancesResponse><Instances><Instance><Id>100090</Id><ReportId>100020</ReportId><ReportName>Sample Report</ReportName><OutputFormat>pdf</OutputFormat><RequestedByUserName>jbm</RequestedByUserName><RequestedAt>2015-05-18 14:03:04</RequestedAt><Parameters><Parameter><Name>AccountId</Name><Value>2</Value></Parameter></Parameters><Status>Expired</Status></Instance><Instance><Id>100090</Id><ReportId>100020</ReportId><ReportName>Sample Report</ReportName><OutputFormat>pdf</OutputFormat><RequestedByUserName>jbm</RequestedByUserName><RequestedAt>2015-05-18 14:03:04</RequestedAt><Parameters><Parameter><Name>AccountId</Name><Value>1</Value></Parameter></Parameters><Status>Expired</Status></Instance></Instances></ReportInstancesResponse>'),
21+
22+
//POST update report instance
23+
new Response(200, [], ''),
24+
]);
25+
26+
self::$container = [];
27+
$history = Middleware::history(self::$container);
28+
$handler = HandlerStack::create($mock);
29+
$handler->push($history);
30+
31+
$client = new Iris\Client("test", "test", Array('url' => 'https://api.test.inetwork.com/v1.0', 'handler' => $handler));
32+
$account = new Iris\Account(9500249, $client);
33+
self::$reports = $account->reports();
34+
}
35+
36+
public function testReportsGet() {
37+
$report = self::$reports->getList()[0];
38+
$this->assertEquals("Sample Report 1 Description", $report->Description);
39+
$this->assertEquals("Sample Report 1", $report->Name);
40+
$this->assertEquals("100020", $report->Id);
41+
}
42+
43+
public function testReportsGetIdAndInstances() {
44+
$report = self::$reports->get_by_id("123");
45+
$this->assertEquals("123", $report->Id);
46+
$this->assertEquals("Sample Report 1", $report->Name);
47+
$this->assertEquals("Report Parameter 1", $report->Parameters->Parameter[0]->Name);
48+
49+
$instance = $report->instances()[0];
50+
$this->assertEquals("100090", $instance->Id);
51+
$this->assertEquals("Sample Report", $instance->ReportName);
52+
}
53+
}

0 commit comments

Comments
 (0)