| Total Complexity | 9 |
| Total Lines | 78 |
| Duplicated Lines | 0 % |
| Coverage | 91.67% |
| Changes | 2 | ||
| Bugs | 2 | Features | 0 |
| 1 | <?php |
||
| 20 | final class Image implements ExporterInterface |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | private $executable = 'dot'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private $format = 'svg'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Image constructor. |
||
| 34 | */ |
||
| 35 | 4 | public function __construct() |
|
| 36 | { |
||
| 37 | 4 | if (0 === stripos(PHP_OS, 'WIN')) { |
|
| 38 | $this->executable = 'dot.exe'; |
||
| 39 | } |
||
| 40 | 4 | } |
|
| 41 | |||
| 42 | /** |
||
| 43 | * @throws Exception |
||
| 44 | */ |
||
| 45 | 1 | public function export(NodeInterface $node): string |
|
| 46 | { |
||
| 47 | 1 | if (!($tmp = tempnam(sys_get_temp_dir(), 'phptree-export-'))) { |
|
| 48 | return ''; |
||
| 49 | } |
||
| 50 | |||
| 51 | 1 | file_put_contents($tmp, (new Gv())->export($node)); |
|
| 52 | |||
| 53 | 1 | return (string) shell_exec($this->getConvertCommand($tmp)); |
|
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Get the executable to use. |
||
| 58 | */ |
||
| 59 | 3 | public function getExecutable(): string |
|
| 60 | { |
||
| 61 | 3 | return $this->executable; |
|
| 62 | } |
||
| 63 | |||
| 64 | 2 | public function getFormat(): string |
|
| 65 | { |
||
| 66 | 2 | return $this->format; |
|
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Change the executable to use. |
||
| 71 | * |
||
| 72 | * @return \loophp\phptree\Exporter\Image |
||
| 73 | */ |
||
| 74 | 1 | public function setExecutable(string $executable): self |
|
| 75 | { |
||
| 76 | 1 | $this->executable = $executable; |
|
| 77 | |||
| 78 | 1 | return $this; |
|
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return \loophp\phptree\Exporter\Image |
||
| 83 | */ |
||
| 84 | 1 | public function setFormat(string $format): self |
|
| 85 | { |
||
| 86 | 1 | $this->format = $format; |
|
| 87 | |||
| 88 | 1 | return $this; |
|
| 89 | } |
||
| 90 | |||
| 91 | 1 | private function getConvertCommand(string $path): string |
|
| 98 | ); |
||
| 99 | } |
||
| 100 | } |
||
| 101 |