Passed
Push — master ( d40832...b64ee5 )
by Jakub
01:41
created

Collector::start()   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 2
Bugs 0 Features 1
Metric Value
eloc 2
c 2
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MyTester\CodeCoverage;
6
7
use MyTester\CodeCoverageException as Exception;
8
use MyTester\ICodeCoverageEngine;
9
10
/**
11
 * Code coverage collector
12
 *
13
 * @author Jakub Konečný
14
 * @internal
15
 */
16
final class Collector
17
{
18
    /** @var ICodeCoverageEngine[] */
19
    private array $engines;
20
    private ?ICodeCoverageEngine $currentEngine = null;
21
22 1
    public function registerEngine(ICodeCoverageEngine $engine): void
23
    {
24 1
        $this->engines[] = $engine;
25 1
    }
26
27
    /**
28
     * @throws Exception
29
     */
30 1
    public function start(): void
31
    {
32 1
        $engine = $this->selectEngine();
33 1
        $engine->start();
34 1
    }
35
36 1
    public function finish(): array
37
    {
38 1
        if ($this->currentEngine === null) {
39
            throw new Exception("Code coverage collector has not been started.", Exception::COLLECTOR_NOT_STARTED);
40
        }
41 1
        return $this->currentEngine->collect();
42
    }
43
44 1
    private function selectEngine(): ICodeCoverageEngine
45
    {
46 1
        if ($this->currentEngine !== null) {
47
            return $this->currentEngine;
48
        }
49 1
        foreach ($this->engines as $engine) {
50 1
            if ($engine->isAvailable()) {
51 1
                $this->currentEngine = $engine;
52 1
                return $engine;
53
            }
54
        }
55
        throw new Exception("No code coverage engine is available.", Exception::NO_ENGINE_AVAILABLE);
56
    }
57
}
58