-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOutbound.php
More file actions
146 lines (125 loc) · 3.56 KB
/
Outbound.php
File metadata and controls
146 lines (125 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* Interfax
*
* (C) InterFAX, 2016
*
* @package interfax/interfax
* @author Interfax <dev@interfax.net>
* @author Mike Smith <mike.smith@camc-ltd.co.uk>
* @copyright Copyright (c) 2016, InterFAX
* @license MIT
*/
namespace Interfax;
use Interfax\Exception\RequestException;
use Interfax\Outbound\Fax;
class Outbound
{
/**
* @var GenericFactory
*/
private $factory;
protected $client;
public function __construct(Client $client, GenericFactory $factory = null)
{
$this->client = $client;
if ($factory === null) {
$factory = new GenericFactory();
}
$this->factory = $factory;
}
/**
* @param array $definitions
* @return Fax[]
* @throws \InvalidArgumentException
*/
protected function createFaxes($definitions)
{
$res = [];
foreach ($definitions as $f_data) {
if (array_key_exists('id', $f_data)) {
$res[] = $this->factory->instantiateClass(
'Interfax\Outbound\Fax',
[$this->client, $f_data['id'], $f_data]
);
} else {
throw new \InvalidArgumentException('No id attribute found in fax definition');
}
}
return $res;
}
/**
* @param array $ids
* @return Fax[]
* @throws \InvalidArgumentException
* @throws RequestException
*/
public function completed($ids = [])
{
$params = [];
if (count($ids) === 0) {
throw new \InvalidArgumentException('Must provide at least one id for completed request');
}
$params['query'] = ['ids' => implode(',', $ids)];
$json = $this->client->get('/outbound/faxes/completed', $params);
return $this->createFaxes($json);
}
/**
* @param array $query_params
* @return Outbound\Fax[]
* @internal param $params
*/
public function recent($query_params = [])
{
$params = [];
if (count($query_params)) {
$params = ['query' => $query_params];
}
return $this->createFaxes($this->client->get('/outbound/faxes', $params));
}
/**
* @param $id
* @param null $fax_number
* @return mixed
* @throws RequestException
*/
public function resend($id, $fax_number = null)
{
$fax = $this->factory->instantiateClass('Interfax\Outbound\Fax', [$this->client, $id]);
return $fax->resend($fax_number);
}
/**
* Get an individual Fax resource for the given $id.
*
* @param $id
* @return null|Interfax\Outbound\Fax
*/
public function find($id)
{
try {
$response = $this->client->get('/outbound/faxes/' . $id);
if (is_array($response) && array_key_exists('id', $response)) {
return $this->factory->instantiateClass(
'Interfax\Outbound\Fax',
[$this->client, $response['id'], $response]
);
}
} catch (\RuntimeException $e) {
if ((int) $e->getStatusCode() === 404) {
return null;
}
throw $e;
}
throw new \RuntimeException('A reasonable but unhandled response was received');
}
/**
* @param array $params
* @return Outbound\Fax[]
* @throws \InvalidArgumentException
* @throws RequestException
*/
public function search($params = [])
{
return $this->createFaxes($this->client->get('/outbound/search', ['query' => $params]));
}
}