Completed
Push — master ( 997b3a...6e2f33 )
by Scott
02:36
created

Users::filter()   C

Complexity

Conditions 7
Paths 18

Size

Total Lines 46
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 46
rs 6.7272
cc 7
eloc 17
nc 18
nop 2
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 (!is_string($type) || !in_array($type, ['ids', 'email'])) {
41
            $this->throwException('Invalid type specified: ' . $type . '. Must be one of: [ids, email].');
42
        }
43
44
        // Ids accepts multiple values so we might have a comma separated string or an array, either
45
        // way we want to filter the values to make sure it's legit
46
        if ($type == 'ids') {
47
            // If we don't have an array create on
48
            if (!is_array($value)) {
49
                $value = explode(',', $value);
50
            }
51
52
            // Convert all array values to integers
53
            $value = array_map('intval', $value);
54
55
            // Convert back to string
56
            $value = implode(',', $value);
57
        }
58
59
        // If we don't have a string something is wrong
60
        if (!is_string($value)) {
61
            // If we are using the type ids we can accept arrays and strings, an array would've already
62
            // been converted to a string at this point but notify the end user
63
            $canBeArray = ($type == 'ids') ? ' or array' : '';
64
65
            $this->throwException('Invalid value type used: ' . gettype($value) . '. Must be a string' . $canBeArray . '.');
66
        }
67
68
        $this->filterType = $type;
69
        $this->filterValue = $value;
70
71
        $this->addLog('debug', "Setting filter type to '" . $this->filterType . "'");
72
        $this->addLog('debug', "Setting filter value to '" . $this->filterValue . "'");
73
74
        // Ensure we can chain methods
75
        return $this;
76
    }
77
78
    /**
79
     * Perform API request
80
     *
81
     * @return GuzzleHttp\Psr7\Stream The guzzle response body
82
     */
83
    public function get()
84
    {
85
        // Set filter if there is one, otherwise all users will be returned
86
        if ($this->filterType && $this->filterValue) {
87
            $this->addData('filter[' . $this->filterType . ']', $this->filterValue);
88
        }
89
90
        return $this->request('users.json');
91
    }
92
}
93