Passed
Push — parser-refactoring ( e758c6...b54cbd )
by Luis
11:11
created

ImageProcessor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 6 2
A process() 0 16 1
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
    public function __construct(Process $process = null, Filesystem $fileSystem = null)
21
    {
22
        $this->process = $process ?? new Process($this->command());
23
        $this->fileSystem = $fileSystem ?? new Filesystem();
24
    }
25
26
    public function process(string $digraphInDotFormat): string
27
    {
28
        $dotFile = $this->fileSystem->tempnam('/tmp', 'phuml');
29
        $imageFile = $this->fileSystem->tempnam('/tmp', 'phuml');
30
31
        $this->fileSystem->dumpFile($dotFile, $digraphInDotFormat);
32
        $this->fileSystem->remove($imageFile);
33
34
        $this->execute($dotFile, $imageFile);
35
36
        $image = file_get_contents($imageFile);
37
38
        $this->fileSystem->remove($dotFile);
39
        $this->fileSystem->remove($imageFile);
40
41
        return $image;
42
    }
43
44
    public function execute(string $inputFile, string $outputFile): void
45
    {
46
        $this->process->setCommandLine([$this->command(), '-Tpng', '-o', $outputFile, $inputFile]);
47
        $this->process->run();
48
        if (!$this->process->isSuccessful()) {
49
            throw new ImageGenerationFailure($this->process->getErrorOutput());
50
        }
51
    }
52
53
    abstract public function command(): string;
54
}
55