Passed
Push — master ( d71011...0ef60e )
by Pol
01:55
created

Image::getTemporaryFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
c 0
b 0
f 0
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 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));
0 ignored issues
show
Bug Best Practice introduced by
The expression return shell_exec($this->getConvertCommand($tmp)) could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
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
105
    {
106 1
        return sprintf(
107 1
            '%s -T%s %s',
108 1
            $this->getExecutable(),
109 1
            $this->getFormat(),
110 1
            $path
111
        );
112
    }
113
}
114