Passed
Push — master ( aa061c...2e1140 )
by Jakub
02:07
created

Collector   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
eloc 18
c 4
b 0
f 2
dl 0
loc 55
rs 10
ccs 20
cts 20
cp 1
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A start() 0 4 1
A registerEngine() 0 3 1
A finish() 0 6 2
A selectEngine() 0 12 4
A getEngineName() 0 4 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
    }
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
    public function getEngineName(): string
51
    {
52 1
        $engine = $this->selectEngine();
53 1
        return $engine->getName();
54
    }
55
56
    /**
57
     * @throws Exception
58
     */
59 1
    private function selectEngine(): ICodeCoverageEngine
60
    {
61 1
        if ($this->currentEngine !== null) {
62 1
            return $this->currentEngine;
63
        }
64 1
        foreach ($this->engines as $engine) {
65 1
            if ($engine->isAvailable()) {
66 1
                $this->currentEngine = $engine;
67 1
                return $engine;
68
            }
69
        }
70 1
        throw new Exception("No code coverage engine is available.", Exception::NO_ENGINE_AVAILABLE);
71
    }
72
}
73