Dumper::dump()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 3
1
<?php
2
3
namespace Glooby\Debug\Dumper;
4
5
use Glooby\Debug\Formatter\FormatterAwareTrait;
6
use Glooby\Debug\Writer\WriterAwareTrait;
7
8
/**
9
 * @author Emil Kilhage
10
 */
11
class Dumper implements DumperInterface
12
{
13
    use FormatterAwareTrait;
14
    use WriterAwareTrait;
15
16
    /**
17
     * @var bool
18
     */
19
    private $enabled = false;
20
21
    /**
22
     * @param boolean $enabled
23
     */
24
    public function setEnabled($enabled)
25
    {
26
        $this->enabled = $enabled;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function dump($id, $output, $ext = 'txt')
33
    {
34
        if ($this->enabled === false) {
35
            return;
36
        }
37
38
        $output = $this->formatter->format($output);
39
40
        $name = $this->createName($id, $ext);
41
42
        $this->writer->write($name, $output);
43
    }
44
45
    /**
46
     * @param string $id
47
     * @param string $ext
48
     * @return string
49
     */
50
    private function createName($id, $ext)
51
    {
52
        return sprintf('%s.%s.%s.%s.%s', $id, date('ymd-His'), posix_getpid(), mt_rand(), $ext);
53
    }
54
}
55