Completed
Push — image-processors-refactoring ( dcd98f )
by Luis
05:57
created

ImageProcessor::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 2
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 plProcessor;
10
use plProcessorOptions;
11
use Symfony\Component\Filesystem\Filesystem;
12
use Symfony\Component\Process\Process;
13
14
abstract class ImageProcessor extends plProcessor
15
{
16
    /** @var plProcessorOptions */
17
    public $options;
18
19
    /** @var Process */
20
    protected $process;
21
22
    /** @var Filesystem */
23
    private $fileSystem;
24
25 30
    public function __construct(Process $process = null, Filesystem $fileSystem = null)
26
    {
27 30
        $this->options = new plProcessorOptions();
28 30
        $this->process = $process ?? new Process($this->command());
29 30
        $this->fileSystem = $fileSystem ?? new Filesystem();
30 30
    }
31
32 24
    public function getInputType(): string
33
    {
34 24
        return 'text/dot';
35
    }
36
37 6
    public function getOutputType(): string
38
    {
39 6
        return 'image/png';
40
    }
41
42
    public function process($input)
43
    {
44
        $dotFile = $this->fileSystem->tempnam('/tmp', 'phuml');
45
        $imageFile = $this->fileSystem->tempnam('/tmp', 'phuml');
46
47
        $this->fileSystem->dumpFile($dotFile, $input);
48
        $this->fileSystem->remove($imageFile);
49
50
        $this->execute($dotFile, $imageFile);
51
52
        $image = file_get_contents($imageFile);
53
54
        $this->fileSystem->remove($dotFile);
55
        $this->fileSystem->remove($imageFile);
56
57
        return $image;
58
    }
59
60 12
    public function execute(string $inputFile, string $outputFile): void
61
    {
62 12
        $this->process->setCommandLine([$this->command(), '-Tpng', '-o', $outputFile, $inputFile]);
63 12
        $this->process->run();
64 12
        if (!$this->process->isSuccessful()) {
65 6
            throw new ImageGenerationFailure($this->process->getErrorOutput());
66
        }
67 6
    }
68
69
    abstract public function command(): string;
70
}
71