Completed
Push — master ( 3f2b21...64f10e )
by Alessandro
06:51
created

CoverageFetcher   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 93.94%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 8
c 4
b 0
f 0
lcom 1
cbo 5
dl 0
loc 81
ccs 31
cts 33
cp 0.9394
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fetch() 0 18 3
A coverageFileIsValid() 0 17 3
A overrideCoverageClassDefinition() 0 13 1
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
    /**
24
     * CoverageFetcher constructor.
25
     * @param TempFilenameFactory $tempFilenameFactory
26
     * @param TestResultHandlerInterface $failureHandler
27
     */
28 5
    public function __construct(TempFilenameFactory $tempFilenameFactory, TestResultHandlerInterface $failureHandler)
29
    {
30 5
        $this->tempFilenameFactory = $tempFilenameFactory;
31 5
        $this->resultHandler = $failureHandler;
32 5
    }
33
34
    /**
35
     * @param AbstractParaunitProcess $process
36
     * @return CodeCoverage
37
     */
38 5
    public function fetch(AbstractParaunitProcess $process)
39
    {
40 5
        $tempFilename = $this->tempFilenameFactory->getFilenameForCoverage($process->getUniqueId());
41 5
        $codeCoverage = null;
42
43 5
        if ($this->coverageFileIsValid($tempFilename)) {
44 4
            $codeCoverage = require $tempFilename;
45 4
            unlink($tempFilename);
46 4
        }
47
48 5
        if ($codeCoverage instanceof CodeCoverage) {
49 3
            return $codeCoverage;
50
        }
51
52 2
        $this->resultHandler->addProcessToFilenames($process);
53
54 2
        return new CodeCoverage();
55
    }
56
57
    /**
58
     * @param string $tempFilename
59
     * @return bool
60
     */
61 5
    private function coverageFileIsValid($tempFilename)
62
    {
63 5
        if (! file_exists($tempFilename)) {
64 1
            return false;
65
        }
66
67
        try {
68 4
            $this->overrideCoverageClassDefinition($tempFilename);
69
70 4
            $verificationProcess = new Process('php --syntax-check ' . $tempFilename);
71 4
            $verificationProcess->run();
72
73 4
            return $verificationProcess->getExitCode() === 0;
74
        } catch (\Exception $e) {
75
            return false;
76
        }
77
    }
78
79
    /**
80
     * @param string $tempFilename
81
     */
82 4
    private function overrideCoverageClassDefinition($tempFilename)
83
    {
84 4
        $fileContent = str_replace(
85
            array(
86 4
                'new SebastianBergmann\CodeCoverage\CodeCoverage',
87
                'new PHP_CodeCoverage'
88 4
            ),
89 4
            'new Paraunit\Proxy\Coverage\CodeCoverage',
90 4
            file_get_contents($tempFilename)
91 4
        );
92
93 4
        file_put_contents($tempFilename, $fileContent);
94 4
    }
95
}
96