1 | <?php |
||
8 | class Usage extends Resource |
||
9 | { |
||
10 | /** |
||
11 | * The end date in Y-m-d format |
||
12 | * |
||
13 | * @param string |
||
14 | */ |
||
15 | private $endDate; |
||
16 | |||
17 | /** |
||
18 | * Whether sub accounts should be included in the request or not |
||
19 | * |
||
20 | * @param boolean |
||
21 | */ |
||
22 | private $includeSubAccounts; |
||
23 | |||
24 | /** |
||
25 | * The product type which will be called by this resource |
||
26 | * |
||
27 | * @param string |
||
28 | */ |
||
29 | private $productType; |
||
30 | |||
31 | /** |
||
32 | * The product types that can be used with this resource |
||
33 | * |
||
34 | * @param array |
||
35 | */ |
||
36 | private $productTypes = ['apm', 'browser', 'mobile']; |
||
37 | |||
38 | /** |
||
39 | * The start date in Y-m-d format |
||
40 | * |
||
41 | * @param string |
||
42 | */ |
||
43 | private $startDate; |
||
44 | |||
45 | /** |
||
46 | * Create a new usage resource and set the start and end to today |
||
47 | * |
||
48 | * @param string $apiKey |
||
49 | * @param Logger $logger |
||
50 | */ |
||
51 | public function __construct($apiKey, Logger $logger = null) |
||
59 | |||
60 | /** |
||
61 | * Set the date to get data to |
||
62 | * |
||
63 | * @param mixed $date |
||
64 | * @return Usage |
||
65 | */ |
||
66 | public function endDate($date) |
||
70 | |||
71 | /** |
||
72 | * Perform API request |
||
73 | * |
||
74 | * @param string $type |
||
75 | * @throws GuzzleHttp\Exception\ClientException Exception thrown by Guzzle |
||
76 | * @return GuzzleHttp\Psr7\Stream The guzzle response body |
||
77 | */ |
||
78 | public function get($type = '') |
||
79 | { |
||
80 | // If we have a product type passed, use it from here |
||
81 | if ($type) { |
||
82 | $this->productType($type); |
||
83 | } |
||
84 | |||
85 | // If no type has been specified the request won't complete |
||
86 | if (!$this->productType) { |
||
87 | $this->throwException('No product type specified. Must be one of: [' . implode(', ', $this->productTypes) . '].'); |
||
88 | } |
||
89 | |||
90 | $this->addData('start_date', $this->startDate); |
||
91 | $this->addData('end_date', $this->endDate); |
||
92 | |||
93 | if ($this->includeSubAccounts) { |
||
94 | // New Relic expects the boolean to be sent as string |
||
95 | $this->addData('include_subaccounts', 'true'); |
||
96 | } |
||
97 | |||
98 | return $this->request('usages/' . $this->productType . '.json'); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Toggle include sub accounts |
||
103 | * |
||
104 | * @param boolean $include |
||
105 | * @throws Exception If $include isn't a boolean |
||
106 | * @return Usage |
||
107 | */ |
||
108 | public function includeSubAccounts($include) |
||
120 | |||
121 | /** |
||
122 | * Set the product type |
||
123 | * |
||
124 | * @param string $type |
||
125 | * @throws Exception If $type isn't a string or isn't a valid product type |
||
126 | * @return Usage |
||
127 | */ |
||
128 | public function productType($type) |
||
145 | |||
146 | /** |
||
147 | * Set a date to something |
||
148 | * |
||
149 | * @param string $type |
||
150 | * @param mixed $date |
||
151 | * @throws Exception If $date isn't an integer or string |
||
152 | * @return Usage |
||
153 | */ |
||
154 | private function setDate($type, $date) |
||
168 | |||
169 | /** |
||
170 | * Set the date to get data from |
||
171 | * |
||
172 | * @param mixed $date |
||
173 | * @return Usage |
||
174 | */ |
||
175 | public function startDate($date) |
||
179 | } |
||
180 |