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

ImageProcessor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 20
cts 20
cp 1
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 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