Passed
Push — union-types ( b56600 )
by Luis
14:03
created

ImageProcessor::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php declare(strict_types=1);
2
/**
3
 * PHP version 7.4
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
    private ImageProcessorName $name;
23
24
    private SmartFileSystem $fileSystem;
25
26
    public static function neato(SmartFileSystem $filesystem): ImageProcessor
27
    {
28
        return new ImageProcessor(new ImageProcessorName('neato'), $filesystem);
29
    }
30
31
    public static function dot(SmartFileSystem $filesystem): ImageProcessor
32
    {
33
        return new ImageProcessor(new ImageProcessorName('dot'), $filesystem);
34
    }
35
36
    private function __construct(ImageProcessorName $name, SmartFileSystem $fileSystem)
37
    {
38
        $this->name = $name;
39
        $this->fileSystem = $fileSystem;
40
    }
41
42
    public function name(): string
43
    {
44
        return $this->name->value();
45
    }
46
47
    /**
48
     * It returns the contents of a `png` class diagram
49
     */
50
    public function process(OutputContent $digraphInDotFormat): OutputContent
51
    {
52
        $dotFile = $this->fileSystem->tempnam('/tmp', 'phuml');
53
        $imageFile = $this->fileSystem->tempnam('/tmp', 'phuml');
54
55
        $this->fileSystem->dumpFile($dotFile, $digraphInDotFormat->value());
56
        $this->fileSystem->remove($imageFile); // Remove any previously generated image
57
58
        $this->execute($dotFile, $imageFile);
59
60
        $image = $this->fileSystem->readFile($imageFile);
61
62
        $this->fileSystem->remove($dotFile);
63
        $this->fileSystem->remove($imageFile);
64
65
        return new OutputContent($image);
66
    }
67
68
    /**
69
     * @throws ImageGenerationFailure If the Graphviz command failed
70
     */
71
    private function execute(string $inputFile, string $outputFile): void
72
    {
73
        $process = new Process([$this->name->command(), '-Tpng', '-o', $outputFile, $inputFile]);
74
        $process->run();
75
        if (! $process->isSuccessful()) {
76
            throw ImageGenerationFailure::withOutput($process->getErrorOutput());
77
        }
78
    }
79
}
80