Passed
Push — develop ( 1472b6...daeadb )
by Alec
02:47
created

Reportable   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 43
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isNotLaunched() 0 3 1
A getReport() 0 15 5
A prepareForReport() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Tools\Reports\Traits;
6
7
use AlecRabbit\Tools\Benchmark;
8
use AlecRabbit\Tools\Reports\Contracts\ReportInterface;
9
use AlecRabbit\Tools\Reports\Factory;
10
11
trait Reportable
12
{
13
    /** @var ReportInterface|null */
14
    protected $reportObject;
15
16
    /** @var bool */
17
    private $launched = false;
18
19
    /**
20
     * @param bool $rebuild Rebuild report object
21
     * @return ReportInterface
22
     */
23 14
    public function getReport(bool $rebuild = true): ReportInterface
24
    {
25 14
        if ($this instanceof Benchmark && $this->isNotLaunched()) {
26 1
            throw new \RuntimeException('You should launch a benchmark by run() before getting a report');
27
        }
28 14
        if (null === $this->reportObject || true === $rebuild) {
29 14
            $this->prepareForReport();
30 14
            $this->reportObject =
31 14
                Factory::makeReport(
32
                    /** @scrutinizer ignore-type */
33 14
                    $this
34
                );
35
        }
36
        return
37 14
            $this->reportObject;
38
    }
39
40
    /**
41
     * @return bool
42
     */
43 5
    public function isNotLaunched(): bool
44
    {
45 5
        return !$this->launched;
46
    }
47
48
49
    /**
50
     * Makes all necessary actions before report
51
     */
52 9
    protected function prepareForReport(): void
53
    {
54 9
    }
55
}
56