Completed
Push — master ( c11c60...f0cca7 )
by Alec
03:39 queued 18s
created

Reportable::checkConditions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 2
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Traits\ForReports\Core;
4
5
use AlecRabbit\Traits\ForReports\Contracts\ReportableInterface;
6
use AlecRabbit\Traits\ForReports\Contracts\ReportInterface;
7
8
abstract class Reportable implements ReportableInterface
9
{
10
    /** @var ReportInterface */
11
    protected $report;
12
13
    /**
14
     * @param bool $rebuild Rebuild report object
15
     * @return ReportInterface
16
     */
17 1
    public function report(bool $rebuild = true): ReportInterface
18
    {
19 1
        $rebuild = $this->checkReport() || $rebuild;
20 1
        if (true === $rebuild) {
21 1
            $this->checkConditions();
22 1
            $this->beforeReport();
23
            /** @noinspection PhpParamsInspection */
24 1
            $this->report->buildOn($this);
25
        }
26
        return
27 1
            $this->report;
28
    }
29
30
    /**
31
     * Checks if all conditions needed for report are met
32
     */
33 1
    protected function checkConditions(): void
34
    {
35 1
    }
36
37
    /**
38
     * Makes all necessary actions before report
39
     */
40 1
    protected function beforeReport(): void
41
    {
42 1
    }
43
44
    abstract protected function createEmptyReport(): ReportInterface;
45
46
    /**
47
     * @return bool
48
     */
49 1
    protected function checkReport(): bool
50
    {
51 1
        if (null === $this->report) {
52 1
            $this->report = $this->createEmptyReport();
53 1
            return true;
54
        }
55
        return false;
56
    }
57
}
58