Completed
Push — symfony-console-application ( 3187e2...c3ee2a )
by Luis
10:39
created

ImageProcessor   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

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