Total Complexity | 9 |
Total Lines | 96 |
Duplicated Lines | 0 % |
Coverage | 92% |
Changes | 0 |
1 | <?php |
||
15 | final class Image extends AbstractExporter |
||
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 | * @param \loophp\phptree\Node\NodeInterface $node |
||
39 | * |
||
40 | * @throws Exception |
||
41 | * |
||
42 | * @return string |
||
43 | */ |
||
44 | 1 | public function export(NodeInterface $node): string |
|
45 | { |
||
46 | 1 | if (false === $tmp = tempnam(sys_get_temp_dir(), 'phptree-export-')) { |
|
47 | return ''; |
||
48 | } |
||
49 | |||
50 | 1 | file_put_contents($tmp, (new Gv())->export($node)); |
|
51 | |||
52 | 1 | return shell_exec($this->getConvertCommand($tmp)); |
|
|
|||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Get the executable to use. |
||
57 | * |
||
58 | * @return string |
||
59 | */ |
||
60 | 3 | public function getExecutable(): string |
|
61 | { |
||
62 | 3 | return $this->executable; |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * @return string |
||
67 | */ |
||
68 | 2 | public function getFormat(): string |
|
69 | { |
||
70 | 2 | return $this->format; |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * Change the executable to use. |
||
75 | * |
||
76 | * @param string $executable |
||
77 | * |
||
78 | * @return \loophp\phptree\Exporter\Image |
||
79 | */ |
||
80 | 1 | public function setExecutable(string $executable): self |
|
81 | { |
||
82 | 1 | $this->executable = $executable; |
|
83 | |||
84 | 1 | return $this; |
|
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param string $format |
||
89 | * |
||
90 | * @return \loophp\phptree\Exporter\Image |
||
91 | */ |
||
92 | 1 | public function setFormat(string $format): self |
|
93 | { |
||
94 | 1 | $this->format = $format; |
|
95 | |||
96 | 1 | return $this; |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param string $path |
||
101 | * |
||
102 | * @return string |
||
103 | */ |
||
104 | 1 | private function getConvertCommand(string $path): string |
|
111 | ); |
||
112 | } |
||
113 | } |
||
114 |