FileReporter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 45
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A report() 0 6 1
A getFilename() 0 7 1
A __construct() 0 3 1
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