Completed
Push — master ( 7c0ec5...af2bde )
by Alessandro
06:40 queued 57s
created

CoverageFetcher   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 90.91%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 56
ccs 20
cts 22
cp 0.9091
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fetch() 0 16 3
A coverageFileIsValid() 0 16 3
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
            unlink($tempFilename);
40 2
        }
41
42 3
        if ($codeCoverage instanceof CodeCoverage) {
43 1
            return $codeCoverage;
44
        }
45
46 2
        return new CodeCoverage();
47
    }
48
49
    /**
50
     * @param string $tempFilename
51
     * @return bool
52
     */
53 3
    private function coverageFileIsValid($tempFilename)
54
    {
55 3
        if (! file_exists($tempFilename)) {
56 1
            return false;
57
        }
58
59
        try {
60 2
            $verificationProcess = new Process('php --syntax-check ' . $tempFilename);
61 2
            $verificationProcess->start();
62 2
            $verificationProcess->wait();
63
64 2
            return $verificationProcess->getExitCode() === 0;
65
        } catch (\Exception $e) {
66
            return false;
67
        }
68
    }
69
}
70