ApmServerCurlReporter::report()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 17
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 29
rs 9.7
1
<?php
2
3
namespace ZoiloMora\ElasticAPM\Reporter;
4
5
use ZoiloMora\ElasticAPM\Events\Common\Service\Agent;
6
use ZoiloMora\ElasticAPM\Helper\NDJson;
7
use ZoiloMora\ElasticAPM\Helper\Compressor;
8
9
final class ApmServerCurlReporter implements Reporter
10
{
11
    const METHOD = 'POST';
12
    const URI = '/intake/v2/events';
13
14
    /**
15
     * @var string
16
     */
17
    private $baseUri;
18
19
    /**
20
     * @param string $baseUri
21
     */
22
    public function __construct($baseUri)
23
    {
24
        $this->baseUri = $baseUri;
25
    }
26
27
    /**
28
     * @param array $events
29
     *
30
     * @return void
31
     *
32
     * @throws \Exception
33
     */
34
    public function report(array $events)
35
    {
36
        $url = $this->getUrl();
37
        $body = Compressor::gzip(
38
            NDJson::convert($events)
39
        );
40
        $headers = $this->getHttpHeaders(
41
            $this->getHeaders($body)
42
        );
43
44
        $ch = curl_init($url);
45
46
        if (false === $ch) {
47
            throw new \Exception('Could not initialize the curl handler.');
48
        }
49
50
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, self::METHOD);
51
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
52
        curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
53
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
54
55
        $response = curl_exec($ch);
56
57
        $httpStatusCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
58
59
        curl_close($ch);
60
61
        if (202 !== $httpStatusCode) {
62
            throw new ReporterException($response, $httpStatusCode);
63
        }
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    private function getUrl()
70
    {
71
        return sprintf(
72
            '%s%s',
73
            $this->baseUri,
74
            self::URI
75
        );
76
    }
77
78
    /**
79
     * @param array $headers
80
     *
81
     * @return array
82
     *
83
     * @return array
84
     */
85
    private function getHttpHeaders(array $headers)
86
    {
87
        return array_map(
88
            static function ($key, $value) {
89
                return sprintf(
90
                    '%s: %s',
91
                    $key,
92
                    $value
93
                );
94
            },
95
            array_keys($headers),
96
            array_values($headers)
97
        );
98
    }
99
100
    /**
101
     * @param string $body
102
     *
103
     * @return array
104
     */
105
    private function getHeaders($body)
106
    {
107
        return array_merge(
108
            $this->defaultRequestHeaders(),
109
            [
110
                'Content-Length' => strlen($body),
111
            ]
112
        );
113
    }
114
115
    /**
116
     * @return array
117
     */
118
    private function defaultRequestHeaders()
119
    {
120
        return [
121
            'Content-Type' => NDJson::contentType(),
122
            'Content-Encoding' => 'gzip',
123
            'User-Agent' => sprintf('%s/%s', Agent::NAME, Agent::VERSION),
124
            'Accept' => 'application/json',
125
        ];
126
    }
127
}
128