Completed
Push — master ( 836339...732961 )
by Scott
02:36
created

Usage::setDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace Sjdaws\NewRelicApi\Account;
4
5
use Monolog\Logger;
6
use Sjdaws\NewRelicApi\Resource;
7
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)
52
    {
53
        parent::__construct($apiKey, $logger);
54
55
        // Use today as default date range
56
        $this->startDate(time());
57
        $this->endDate(time());
58
    }
59
60
    /**
61
     * Set the date to get data to
62
     *
63
     * @param  mixed   $date
64
     * @return Usage
65
     */
66
    public function endDate($date)
67
    {
68
        return $this->setDate('end', $date);
69
    }
70
71
    /**
72
     * Perform API request
73
     *
74
     * @param  string                 $type
75
     * @return GuzzleHttp\Psr7\Stream The guzzle response body
76
     */
77
    public function get($type = null)
78
    {
79
        // If we have a product type passed, use it from here
80
        if ($type !== null) {
81
            $this->productType($type);
82
        }
83
84
        // If no type has been specified the request won't complete
85
        if (!$this->productType) {
86
            $this->throwException('No product type specified. Must be one of: [' . implode(', ', $this->productTypes) . '].');
87
        }
88
89
        $this->addData('start_date', $this->startDate);
90
        $this->addData('end_date', $this->endDate);
91
92
        if ($this->includeSubAccounts) {
93
            // New Relic expects the boolean to be sent as string
94
            $this->addData('include_subaccounts', 'true');
95
        }
96
97
        return $this->request('usages/' . $this->productType . '.json');
98
    }
99
100
    /**
101
     * Toggle include sub accounts
102
     *
103
     * @param  boolean $include
104
     * @return Usage
105
     */
106
    public function includeSubAccounts($include)
107
    {
108
        $this->includeSubAccounts = (bool) $include;
109
        $this->addLog('debug', 'Setting include sub accounts to ' . $this->includeSubAccounts);
110
111
        return $this;
112
    }
113
114
    /**
115
     * Set the product type
116
     *
117
     * @param  string  $type
118
     * @return Usage
119
     */
120
    public function productType($type)
121
    {
122
        $type = mb_strtolower($type);
123
124
        /**
125
         * Make sure the product type is apm, browser or mobile
126
         * @see https://docs.newrelic.com/docs/apis/rest-api-v2/account-examples-v2/retrieving-account-usage-metrics-rest-api#product_names
127
         */
128
        if (!in_array($type, $this->productTypes)) {
129
            $this->throwException('Invalid product type specified: ' . $type . '. Must be one of: [' . implode(', ', $this->productTypes) . '].');
130
        }
131
132
        $this->productType = $type;
133
        $this->addLog('debug', 'Setting product type to ' . $this->productType);
134
135
        return $this;
136
    }
137
138
    /**
139
     * Set a date to something
140
     *
141
     * @param  string  $type
142
     * @param  mixed   $date
143
     * @return Usage
144
     */
145
    private function setDate($type, $date)
146
    {
147
        $variable = $type . 'Date';
148
149
        $this->$variable = $this->getDate($date, 'Y-m-d');
150
        $this->addLog('debug', 'Setting ' . $type . ' date to ' . $this->$variable);
151
152
        return $this;
153
    }
154
155
    /**
156
     * Set the date to get data from
157
     *
158
     * @param  mixed   $date
159
     * @return Usage
160
     */
161
    public function startDate($date)
162
    {
163
        return $this->setDate('start', $date);
164
    }
165
}
166