Completed
Push — master ( 1f1747...e8aa43 )
by personal
17s queued 11s
created

Graphviz::checkInstallation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Component\Chart;
11
12
13
/**
14
 * Generate graphs
15
 *
16
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
17
 */
18
class Graphviz
19
{
20
    /**
21
     * Checks installation of graphviz
22
     *
23
     * @return boolean
24
     */
25
    public function isAvailable() {
26
        $result = shell_exec('circo -V 2>&1');
27
        return preg_match('!graphviz version!', $result);
28
    }
29
30
    /**
31
     * Get image content
32
     *
33
     * @param string $dotContent
34
     * @return string
35
     */
36
    public function getImage($dotContent) {
37
38
        if(!$this->isAvailable()) {
39
            throw new \RuntimeException('Graphviz not installed');
40
        }
41
42
        $dotFile = tempnam(sys_get_temp_dir(), 'phpmetrics-graphviz');
43
        $imageFile = tempnam(sys_get_temp_dir(), 'phpmetrics-graphviz');
44
45
        // dot file
46
        $dotContent = str_replace('\\', '/', $dotContent);
47
        file_put_contents($dotFile, $dotContent);
48
49
        // image
50
        shell_exec(sprintf('circo -Lg -Tsvg -o%2$s %1$s  2>&1', $dotFile, $imageFile));
51
        $content = file_get_contents($imageFile);
52
        unlink($imageFile);
53
        unlink($dotFile);
54
        return $content;
55
    }
56
}