Passed
Pull Request — master (#30)
by
unknown
46:28 queued 21:27
created

ImageProcessor::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
4
 */
5
6
namespace PhUml\Processors;
7
8
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...
9
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...
10
11
/**
12
 * It generates a `png` class diagram from a digraph in DOT format
13
 *
14
 * It takes a digraph in DOT format, saves it to a temporary location and creates a `png` class
15
 * diagram out of it.
16
 * It uses either the `dot` or `neato` command to create the image
17
 */
18
final class ImageProcessor implements Processor
19
{
20 8
    public static function neato(SmartFileSystem $filesystem): ImageProcessor
21
    {
22 8
        return new ImageProcessor(new ImageProcessorName('neato'), $filesystem);
23
    }
24
25 4
    public static function dot(SmartFileSystem $filesystem): ImageProcessor
26
    {
27 4
        return new ImageProcessor(new ImageProcessorName('dot'), $filesystem);
28
    }
29
30 12
    private function __construct(private readonly ImageProcessorName $name, private readonly SmartFileSystem $fileSystem)
31
    {
32
    }
33
34 9
    public function name(): string
35
    {
36 9
        return $this->name->value();
37
    }
38
39
    /**
40
     * It returns the contents of a `png` class diagram
41
     */
42 9
    public function process(OutputContent $digraphInDotFormat): OutputContent
43
    {
44 9
        $dotFile = $this->fileSystem->tempnam(sys_get_temp_dir(), 'phuml');
45 9
        $imageFile = $this->fileSystem->tempnam(sys_get_temp_dir(), 'phuml');
46
47 9
        $this->fileSystem->dumpFile($dotFile, $digraphInDotFormat->value());
48
49 9
        $this->execute($dotFile, $imageFile);
50
51 7
        $image = $this->fileSystem->readFile($imageFile);
52
53 7
        $this->fileSystem->remove($dotFile);
54 7
        $this->fileSystem->remove($imageFile);
55
56 7
        return new OutputContent($image);
57
    }
58
59
    /**
60
     * @throws ImageGenerationFailure If the Graphviz command failed
61
     */
62 9
    private function execute(string $inputFile, string $outputFile): void
63
    {
64 9
        $process = new Process([$this->name->command(), '-Tpng', '-o', $outputFile, $inputFile]);
65 9
        $process->run();
66 9
        if (! $process->isSuccessful()) {
67 2
            throw ImageGenerationFailure::withOutput($process->getErrorOutput());
68
        }
69
    }
70
}
71