Passed
Push — master ( 9bbc25...797142 )
by Luis
02:39
created

ImageProcessor::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 16
ccs 10
cts 10
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP version 7.1
4
 *
5
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
6
 */
7
namespace PhUml\Processors;
8
9
use Symfony\Component\Filesystem\Filesystem;
10
use Symfony\Component\Process\Process;
11
12
abstract class ImageProcessor extends Processor
13
{
14
    /** @var Process */
15
    protected $process;
16
17
    /** @var Filesystem */
18
    private $fileSystem;
19
20 24
    public function __construct(Process $process = null, Filesystem $fileSystem = null)
21
    {
22 24
        $this->process = $process ?? new Process($this->command());
23 24
        $this->fileSystem = $fileSystem ?? new Filesystem();
24 24
    }
25
26 6
    public function process(string $digraphInDotFormat): string
27
    {
28 6
        $dotFile = $this->fileSystem->tempnam('/tmp', 'phuml');
29 6
        $imageFile = $this->fileSystem->tempnam('/tmp', 'phuml');
30
31 6
        $this->fileSystem->dumpFile($dotFile, $digraphInDotFormat);
32 6
        $this->fileSystem->remove($imageFile);
33
34 6
        $this->execute($dotFile, $imageFile);
35
36 6
        $image = file_get_contents($imageFile);
37
38 6
        $this->fileSystem->remove($dotFile);
39 6
        $this->fileSystem->remove($imageFile);
40
41 6
        return $image;
42
    }
43
44 18
    public function execute(string $inputFile, string $outputFile): void
45
    {
46 18
        $this->process->setCommandLine([$this->command(), '-Tpng', '-o', $outputFile, $inputFile]);
47 18
        $this->process->run();
48 18
        if (!$this->process->isSuccessful()) {
49 6
            throw new ImageGenerationFailure($this->process->getErrorOutput());
50
        }
51 12
    }
52
53
    abstract public function command(): string;
54
}
55