Passed
Push — master ( 345042...3ce0e3 )
by Pol
02:23
created

Image   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 2
Bugs 2 Features 0
Metric Value
eloc 20
c 2
b 2
f 0
dl 0
loc 78
ccs 22
cts 24
cp 0.9167
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A setExecutable() 0 5 1
A getFormat() 0 3 1
A export() 0 9 2
A setFormat() 0 5 1
A getExecutable() 0 3 1
A getConvertCommand() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace loophp\phptree\Exporter;
6
7
use Exception;
8
use loophp\phptree\Node\NodeInterface;
9
10
use const PHP_OS;
11
12
/**
13
 * Class Image.
14
 */
15
final class Image implements ExporterInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    private $executable = 'dot';
21
22
    /**
23
     * @var string
24
     */
25
    private $format = 'svg';
26
27
    /**
28
     * Image constructor.
29
     */
30 4
    public function __construct()
31
    {
32 4
        if (0 === mb_stripos(PHP_OS, 'WIN')) {
33
            $this->executable = 'dot.exe';
34
        }
35 4
    }
36
37
    /**
38
     * @throws Exception
39
     */
40 1
    public function export(NodeInterface $node): string
41
    {
42 1
        if (!($tmp = tempnam(sys_get_temp_dir(), 'phptree-export-'))) {
43
            return '';
44
        }
45
46 1
        file_put_contents($tmp, (new Gv())->export($node));
47
48 1
        return (string) shell_exec($this->getConvertCommand($tmp));
49
    }
50
51
    /**
52
     * Get the executable to use.
53
     */
54 3
    public function getExecutable(): string
55
    {
56 3
        return $this->executable;
57
    }
58
59 2
    public function getFormat(): string
60
    {
61 2
        return $this->format;
62
    }
63
64
    /**
65
     * Change the executable to use.
66
     *
67
     * @return \loophp\phptree\Exporter\Image
68
     */
69 1
    public function setExecutable(string $executable): self
70
    {
71 1
        $this->executable = $executable;
72
73 1
        return $this;
74
    }
75
76
    /**
77
     * @return \loophp\phptree\Exporter\Image
78
     */
79 1
    public function setFormat(string $format): self
80
    {
81 1
        $this->format = $format;
82
83 1
        return $this;
84
    }
85
86 1
    private function getConvertCommand(string $path): string
87
    {
88 1
        return sprintf(
89 1
            '%s -T%s %s',
90 1
            $this->getExecutable(),
91 1
            $this->getFormat(),
92
            $path
93
        );
94
    }
95
}
96