Completed
Push — master ( 47e707...0592b2 )
by Alec
08:38
created

Reportable::report()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 3
nop 1
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 5
rs 9.6111
c 0
b 0
f 0
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 report(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
     * @deprecated use report() instead
42
     * @param bool $rebuild Rebuild report object
43
     * @return ReportInterface
44
     */
45 14
    public function getReport(bool $rebuild = true): ReportInterface
46
    {
47 14
        return $this->report($rebuild);
48
    }
49
50
    /**
51
     * @return bool
52
     */
53 5
    public function isNotLaunched(): bool
54
    {
55 5
        return !$this->launched;
56
    }
57
58
59
    /**
60
     * Makes all necessary actions before report
61
     */
62 9
    protected function prepareForReport(): void
63
    {
64 9
    }
65
}
66