Graphviz   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 39
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A checkInstallation() 0 6 2
A getImage() 0 18 1
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
     * @throws \RuntimeException
24
     */
25
    public function checkInstallation() {
26
        $result = shell_exec('circo -V 2>&1');
27
        if(!preg_match('!graphviz version!', $result)) {
28
            throw new \RuntimeException('Graphviz not installed');
29
        }
30
    }
31
32
    /**
33
     * Get image content
34
     *
35
     * @param string $dotContent
36
     * @return string
37
     */
38
    public function getImage($dotContent) {
39
40
        $this->checkInstallation();
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
}