|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sjdaws\NewRelicApi\Account; |
|
4
|
|
|
|
|
5
|
|
|
use Sjdaws\NewRelicApi\Resource; |
|
6
|
|
|
|
|
7
|
|
|
class Users extends Resource |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* The type of filter to apply to the user list, can be 'ids' or 'email' |
|
11
|
|
|
* |
|
12
|
|
|
* @param string |
|
13
|
|
|
*/ |
|
14
|
|
|
private $filterType; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The value for the filter. Can be a single id, comma separated list of ids, an email address or part of an email address |
|
18
|
|
|
* |
|
19
|
|
|
* @param string |
|
20
|
|
|
*/ |
|
21
|
|
|
private $filterValue; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Add a filter to the request |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $type |
|
27
|
|
|
* @param mixed $value Can be an array or string |
|
28
|
|
|
* @throws Exception If an invalid parameter is passed |
|
29
|
|
|
* @return Users |
|
30
|
|
|
*/ |
|
31
|
|
|
public function filter($type, $value) |
|
32
|
|
|
{ |
|
33
|
|
|
$type = mb_strtolower($type); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Make sure our type is either ids or email |
|
37
|
|
|
* @see https://docs.newrelic.com/docs/apis/rest-api-v2/account-examples-v2/listing-users-your-account#list_by_mail |
|
38
|
|
|
* @see https://docs.newrelic.com/docs/apis/rest-api-v2/account-examples-v2/listing-users-your-account#list_by_userid |
|
39
|
|
|
*/ |
|
40
|
|
|
if (!$this->checkString('filter type', $type) || !in_array($type, ['ids', 'email'])) { |
|
41
|
|
|
$this->throwException('Invalid filter type specified: ' . $type . '. Must be one of: [ids, email].'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// Parse filter values correctly so we always end up with a string |
|
45
|
|
|
$value = $this->parseFilterValues($type, $value); |
|
46
|
|
|
|
|
47
|
|
|
// If we don't have a string something is wrong |
|
48
|
|
|
if (!is_string($value)) { |
|
49
|
|
|
// If we are using the type ids we can accept arrays and strings, an array would've already |
|
50
|
|
|
// been converted to a string at this point but notify the end user |
|
51
|
|
|
$canBeArray = ($type == 'ids') ? ' or array' : ''; |
|
52
|
|
|
|
|
53
|
|
|
$this->throwException('Invalid value type used: ' . gettype($value) . '. Must be a string' . $canBeArray . '.'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$this->filterType = $type; |
|
57
|
|
|
$this->filterValue = $value; |
|
58
|
|
|
|
|
59
|
|
|
$this->addLog('debug', "Setting filter type to '" . $this->filterType . "'"); |
|
60
|
|
|
$this->addLog('debug', "Setting filter value to '" . $this->filterValue . "'"); |
|
61
|
|
|
|
|
62
|
|
|
// Ensure we can chain methods |
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Perform API request |
|
68
|
|
|
* |
|
69
|
|
|
* @throws GuzzleHttp\Exception\ClientException Exception thrown by Guzzle |
|
70
|
|
|
* @return GuzzleHttp\Psr7\Stream The guzzle response body |
|
71
|
|
|
*/ |
|
72
|
|
|
public function get() |
|
73
|
|
|
{ |
|
74
|
|
|
// Set filter if there is one, otherwise all users will be returned |
|
75
|
|
|
if ($this->filterType && $this->filterValue) { |
|
76
|
|
|
$this->addData('filter[' . $this->filterType . ']', $this->filterValue); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $this->request('users.json'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Parse filter values |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $type |
|
86
|
|
|
* @param string $value |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
private function parseFilterValues($type, $value) |
|
90
|
|
|
{ |
|
91
|
|
|
// Ids accepts multiple values so we might have a comma separated string or an array, either |
|
92
|
|
|
// way we want to filter the values to make sure it's legit |
|
93
|
|
|
if ($type == 'ids') { |
|
94
|
|
|
// If we don't have an array create on |
|
95
|
|
|
if (!is_array($value)) { |
|
96
|
|
|
$value = explode(',', $value); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// Convert all array values to integers |
|
100
|
|
|
$value = array_map('intval', $value); |
|
101
|
|
|
|
|
102
|
|
|
// Convert back to string |
|
103
|
|
|
$value = implode(',', $value); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $value; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|