Completed
Push — master ( a479d6...e36e08 )
by Scott
02:44
created

Resource   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 133
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addData() 0 4 1
A addLog() 0 16 3
A getDate() 0 9 2
A throwException() 0 6 1
A request() 0 13 2
1
<?php
2
3
namespace Sjdaws\NewRelicApi;
4
5
use Exception;
6
use GuzzleHttp\Client as GuzzleClient;
7
use Monolog\Logger;
8
9
abstract class Resource
10
{
11
    /**
12
     * The API key used to access the New Relic API, should be an admin or REST api key
13
     * @see https://docs.newrelic.com/docs/apis/rest-api-v2/requirements/api-keys
14
     *
15
     * @param string
16
     */
17
    protected $apiKey;
18
19
    /**
20
     * The endpoint requests will be sent to
21
     * @see https://docs.newrelic.com/docs/apis/rest-api-v2/requirements/new-relic-rest-api-v2-getting-started#appid
22
     *
23
     * @param string
24
     */
25
    private $apiUrl = 'https://api.newrelic.com/v2/';
26
27
    /**
28
     * Payload to be sent with the request
29
     *
30
     * @param array
31
     */
32
    private $data = [];
33
34
    /**
35
     * Monolog instance to be used for logging messages
36
     *
37
     * @param Logger
38
     */
39
    protected $logger;
40
41
    /**
42
     * Create a new resource instance
43
     *
44
     * @param string $apiKey
45
     * @param Logger $logger
46
     */
47
    public function __construct($apiKey, Logger $logger = null)
48
    {
49
        $this->apiKey = $apiKey;
50
        $this->logger = $logger;
51
    }
52
53
    /**
54
     * Add some data to the payload to be sent with the request
55
     *
56
     * @param  string $name
57
     * @param  mixed  $value
58
     * @return void
59
     */
60
    public function addData($name, $value)
61
    {
62
        $this->data[$name] = $value;
63
    }
64
65
    /**
66
     * Log a message
67
     *
68
     * @param  string $type
69
     * @param  string $message
70
     * @param  array  $data
71
     * @return void
72
     */
73
    protected function addLog($type, $message, array $data = [])
74
    {
75
        $type = mb_strtolower($type);
76
77
        // If we don't have a logger instance return to prevent fatal error
78
        if (!$this->logger instanceof Logger) {
79
            return;
80
        }
81
82
        // Make sure the type is valid
83
        if (!in_array($type, ['debug', 'info', 'notice', 'warning', 'error', 'critical', 'alert', 'emergency'])) {
84
            return;
85
        }
86
87
        $this->logger->$type($message, $data);
88
    }
89
90
    /**
91
     * Convert most date/times to a timestamp
92
     *
93
     * @param  mixed  $date
94
     * @param  string $format
95
     * @return int
96
     */
97
    protected function getDate($date, $format = 'U')
98
    {
99
        // If date isn't a timestamp convert it to a timestamp
100
        if (!is_int($date)) {
101
            $date = strtotime($date);
102
        }
103
104
        return date($format, $date);
105
    }
106
107
    /**
108
     * Log a critical message and throw an exception
109
     *
110
     * @param  string      $message
111
     * @throws Exception
112
     * @return void
113
     */
114
    protected function throwException($message)
115
    {
116
        // Add critical log before throwing exception
117
        $this->addLog('critical', get_class($this) . ': ' . $message);
118
        throw new Exception($message);
119
    }
120
121
    /**
122
     * Send a request to the API endpoint and return json response
123
     *
124
     * @param  string   $uri
125
     * @param  string   $method
126
     * @return string
127
     */
128
    protected function request($uri, $method = 'GET')
129
    {
130
        $guzzle = new GuzzleClient(['base_uri' => $this->apiUrl]);
131
        $params = ['headers' => ['X-Api-Key' => $this->apiKey]];
132
133
        if (count($this->data)) {
134
            $params['form_params'] = $this->data;
135
        }
136
137
        $this->addLog('debug', "Calling {$this->apiUrl}$uri using $method", $params);
138
139
        return $guzzle->request($method, $uri, $params)->getBody();
140
    }
141
}
142