FileReporter::report()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
namespace ZoiloMora\ElasticAPM\Reporter;
4
5
use ZoiloMora\ElasticAPM\Helper\Cryptography;
6
use ZoiloMora\ElasticAPM\Helper\NDJson;
7
8
final class FileReporter implements Reporter
9
{
10
    const FILE_NAME_SIZE = 128;
11
12
    /**
13
     * @var string
14
     */
15
    private $basePath;
16
17
    /**
18
     * @param string $basePath
19
     */
20
    public function __construct($basePath)
21
    {
22
        $this->basePath = $basePath;
23
    }
24
25
    /**
26
     * @param array $events
27
     *
28
     * @return void
29
     *
30
     * @throws \Exception
31
     */
32
    public function report(array $events)
33
    {
34
        file_put_contents(
35
            $this->getFilename(),
36
            NDJson::convert($events),
37
            LOCK_EX
38
        );
39
    }
40
41
    /**
42
     * @return string
43
     *
44
     * @throws \Exception
45
     */
46
    private function getFilename()
47
    {
48
        return sprintf(
49
            '%s%s%s.json',
50
            $this->basePath,
51
            DIRECTORY_SEPARATOR,
52
            Cryptography::generateRandomBitsInHex(self::FILE_NAME_SIZE)
53
        );
54
    }
55
}
56