Insights::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 2
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
ccs 10
cts 10
cp 1
crap 1
1
<?php
2
3
namespace DJStarCOM\NewRelic;
4
5
use GuzzleHttp\Client;
6
use DJStarCOM\NewRelic\Entity\Insights\EventCollection;
7
use Respect\Validation\Validator;
8
9
class Insights
10
{
11
    private $key;
12
    private $client;
13
14
    /**
15
     * Insights constructor.
16
     * @param Client $client
17
     * @param string $key
18
     */
19 6
    public function __construct(Client $client, $key)
20
    {
21 6
        $this->client = $client;
22 6
        $this->key = $key;
23 6
        $baseUrl = $this->client->getConfig('base_uri');
24 6
        Validator::notEmpty()
25 6
            ->url()
26 6
            ->endsWith('/')
27 6
            ->setName("URL for NewRelic's Insights API must be valid and have a trailing slash")
28 6
            ->assert($baseUrl);
29 6
    }
30
31
    /**
32
     * @param EventCollection $events
33
     * @return \GuzzleHttp\Promise\PromiseInterface
34
     */
35 6
    public function sendEvent(EventCollection $events)
36
    {
37 6
        $promise = $this->client->postAsync('events', [
38 6
            'body' => json_encode($events),
39
            'headers' => [
40 3
                'X-Insert-Key' => $this->key,
41 3
                'Content-Type' => 'application/json',
42
            ]
43
        ]);
44
45 3
        return $promise;
46
    }
47
}
48