Passed
Push — master ( 11230f...5f5cdb )
by Jakub
12:32
created

Collector::finish()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 3
c 2
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
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
    }
26
27
    /**
28
     * @throws Exception
29
     */
30 1
    public function start(): void
31
    {
32 1
        $engine = $this->selectEngine();
33 1
        $engine->start();
34
    }
35
36
    /**
37
     * @throws Exception
38
     */
39 1
    public function finish(): array
40
    {
41 1
        if ($this->currentEngine === null) {
42 1
            throw new Exception("Code coverage collector has not been started.", Exception::COLLECTOR_NOT_STARTED);
43
        }
44 1
        return $this->currentEngine->collect();
45
    }
46
47
    /**
48
     * @throws Exception
49
     */
50 1
    private function selectEngine(): ICodeCoverageEngine
51
    {
52 1
        if ($this->currentEngine !== null) {
53
            return $this->currentEngine;
54
        }
55 1
        foreach ($this->engines as $engine) {
56 1
            if ($engine->isAvailable()) {
57 1
                $this->currentEngine = $engine;
58 1
                return $engine;
59
            }
60
        }
61 1
        throw new Exception("No code coverage engine is available.", Exception::NO_ENGINE_AVAILABLE);
62
    }
63
}
64