Passed
Push — master ( aade94...d46d5f )
by Luis
50s queued 12s
created

ImageProcessor::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * PHP version 8.0
4
 *
5
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
6
 */
7
8
namespace PhUml\Processors;
9
10
use Symfony\Component\Process\Process;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Process\Process was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Symplify\SmartFileSystem\SmartFileSystem;
0 ignored issues
show
Bug introduced by
The type Symplify\SmartFileSystem\SmartFileSystem was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
/**
14
 * It generates a `png` class diagram from a digraph in DOT format
15
 *
16
 * It takes a digraph in DOT format, saves it to a temporary location and creates a `png` class
17
 * diagram out of it.
18
 * It uses either the `dot` or `neato` command to create the image
19
 */
20
final class ImageProcessor implements Processor
21
{
22
    public static function neato(SmartFileSystem $filesystem): ImageProcessor
23
    {
24
        return new ImageProcessor(new ImageProcessorName('neato'), $filesystem);
25
    }
26
27
    public static function dot(SmartFileSystem $filesystem): ImageProcessor
28 63
    {
29
        return new ImageProcessor(new ImageProcessorName('dot'), $filesystem);
30 63
    }
31 63
32 63
    private function __construct(private ImageProcessorName $name, private SmartFileSystem $fileSystem)
33
    {
34
    }
35
36
    public function name(): string
37 39
    {
38
        return $this->name->value();
39 39
    }
40 39
41
    /**
42 39
     * It returns the contents of a `png` class diagram
43 39
     */
44
    public function process(OutputContent $digraphInDotFormat): OutputContent
45 39
    {
46
        $dotFile = $this->fileSystem->tempnam('/tmp', 'phuml');
47 39
        $imageFile = $this->fileSystem->tempnam('/tmp', 'phuml');
48
49 39
        $this->fileSystem->dumpFile($dotFile, $digraphInDotFormat->value());
50 39
        $this->fileSystem->remove($imageFile); // Remove any previously generated image
51
52 39
        $this->execute($dotFile, $imageFile);
53
54
        $image = $this->fileSystem->readFile($imageFile);
55
56
        $this->fileSystem->remove($dotFile);
57
        $this->fileSystem->remove($imageFile);
58 51
59
        return new OutputContent($image);
60 51
    }
61 51
62 51
    /**
63 6
     * @throws ImageGenerationFailure If the Graphviz command failed
64
     */
65 45
    private function execute(string $inputFile, string $outputFile): void
66
    {
67
        $process = new Process([$this->name->command(), '-Tpng', '-o', $outputFile, $inputFile]);
68
        $process->run();
69
        if (! $process->isSuccessful()) {
70
            throw ImageGenerationFailure::withOutput($process->getErrorOutput());
71
        }
72
    }
73
}
74