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

ImageProcessor   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 60%

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 15
cts 25
cp 0.6
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 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