Image::getConvertCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 1
eloc 5
c 2
b 2
f 0
nc 1
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php
2
3
/**
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types=1);
9
10
namespace loophp\phptree\Exporter;
11
12
use Exception;
13
use loophp\phptree\Node\NodeInterface;
14
15
use const PHP_OS;
16
17
/**
18
 * Class Image.
19
 */
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
92
    {
93 1
        return sprintf(
94 1
            '%s -T%s %s',
95 1
            $this->getExecutable(),
96 1
            $this->getFormat(),
97
            $path
98
        );
99
    }
100
}
101