Completed
Pull Request — master (#59)
by Alessandro
05:25
created

CoverageFetcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Paraunit\Coverage;
4
5
use Paraunit\Configuration\TempFilenameFactory;
6
use Paraunit\Process\AbstractParaunitProcess;
7
use SebastianBergmann\CodeCoverage\CodeCoverage;
8
use Symfony\Component\Process\Process;
9
10
/**
11
 * Class CoverageFetcher
12
 * @package Paraunit\Coverage
13
 */
14
class CoverageFetcher
15
{
16
    /** @var  TempFilenameFactory */
17
    private $tempFilenameFactory;
18
19
    /**
20
     * CoverageFetcher constructor.
21
     * @param TempFilenameFactory $tempFilenameFactory
22
     */
23 3
    public function __construct(TempFilenameFactory $tempFilenameFactory)
24
    {
25 3
        $this->tempFilenameFactory = $tempFilenameFactory;
26 3
    }
27
28
    /**
29
     * @param AbstractParaunitProcess $process
30
     * @return CodeCoverage
31
     */
32 3
    public function fetch(AbstractParaunitProcess $process)
33
    {
34 3
        $tempFilename = $this->tempFilenameFactory->getFilenameForCoverage($process->getUniqueId());
35 3
        $codeCoverage = null;
36
37 3
        if ($this->coverageFileIsValid($tempFilename)) {
38 2
            $codeCoverage = require $tempFilename;
39 2
        }
40
41 3
        if ($codeCoverage instanceof CodeCoverage) {
42 1
            return $codeCoverage;
43
        }
44
45 2
        return new CodeCoverage();
46
    }
47
48
    /**
49
     * @param string $tempFilename
50
     * @return bool
51
     */
52 3
    private function coverageFileIsValid($tempFilename)
53
    {
54 3
        if (! file_exists($tempFilename)) {
55 1
            return false;
56
        }
57
58
        try {
59 2
            $verificationProcess = new Process('php --syntax-check ' . $tempFilename);
60 2
            $verificationProcess->start();
61 2
            $verificationProcess->wait();
62
63 2
            return $verificationProcess->getExitCode() === 0;
64
        } catch (\Exception $e) {
65
            return false;
66
        }
67
    }
68
}
69