Image   A
last analyzed

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 getConvertCommand() 0 7 1
A setExecutable() 0 5 1
A __construct() 0 4 2
A getFormat() 0 3 1
A export() 0 9 2
A setFormat() 0 5 1
A getExecutable() 0 3 1
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