Completed
Pull Request — master (#62)
by Alessandro
05:54
created

CoverageFetcher::fetch()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 10
nc 4
nop 1
crap 3
1
<?php
2
3
namespace Paraunit\Coverage;
4
5
use Paraunit\Configuration\TempFilenameFactory;
6
use Paraunit\Process\AbstractParaunitProcess;
7
use Paraunit\Proxy\Coverage\CodeCoverage;
8
use Paraunit\TestResult\Interfaces\TestResultHandlerInterface;
9
use Symfony\Component\Process\Process;
10
11
/**
12
 * Class CoverageFetcher
13
 * @package Paraunit\Coverage
14
 */
15
class CoverageFetcher
16
{
17
    /** @var  TempFilenameFactory */
18
    private $tempFilenameFactory;
19
20
    /** @var TestResultHandlerInterface */
21
    private $resultHandler;
22
23 3
    /**
24
     * CoverageFetcher constructor.
25 3
     * @param TempFilenameFactory $tempFilenameFactory
26 3
     * @param TestResultHandlerInterface $failureHandler
27
     */
28
    public function __construct(TempFilenameFactory $tempFilenameFactory, TestResultHandlerInterface $failureHandler)
29
    {
30
        $this->tempFilenameFactory = $tempFilenameFactory;
31
        $this->resultHandler = $failureHandler;
32 3
    }
33
34 3
    /**
35 3
     * @param AbstractParaunitProcess $process
36
     * @return CodeCoverage
37 3
     */
38 2
    public function fetch(AbstractParaunitProcess $process)
39 2
    {
40
        $tempFilename = $this->tempFilenameFactory->getFilenameForCoverage($process->getUniqueId());
41 3
        $codeCoverage = null;
42 1
43
        if ($this->coverageFileIsValid($tempFilename)) {
44
            $codeCoverage = require $tempFilename;
45 2
            unlink($tempFilename);
46
        }
47
48
        if ($codeCoverage instanceof CodeCoverage) {
49
            return $codeCoverage;
50
        }
51
52 3
        $this->resultHandler->addProcessToFilenames($process);
53
54 3
        return new CodeCoverage();
55 1
    }
56
57
    /**
58
     * @param string $tempFilename
59 2
     * @return bool
60 2
     */
61 2
    private function coverageFileIsValid($tempFilename)
62
    {
63 2
        if (! file_exists($tempFilename)) {
64
            return false;
65
        }
66
67
        try {
68
            $this->overrideCoverageClassDefinition($tempFilename);
69
70
            $verificationProcess = new Process('php --syntax-check ' . $tempFilename);
71
            $verificationProcess->run();
72
73
            return $verificationProcess->getExitCode() === 0;
74
        } catch (\Exception $e) {
75
            return false;
76
        }
77
    }
78
79
    /**
80
     * @param string $tempFilename
81
     */
82
    private function overrideCoverageClassDefinition($tempFilename)
83
    {
84
        $fileContent = str_replace(
85
            array(
86
                'new SebastianBergmann\CodeCoverage\CodeCoverage',
87
                'new PHP_CodeCoverage'
88
            ),
89
            'new Paraunit\Proxy\Coverage\CodeCoverage',
90
            file_get_contents($tempFilename)
91
        );
92
93
        file_put_contents($tempFilename, $fileContent);
94
    }
95
}
96