1 | <?php declare(strict_types=1); |
||
2 | |||
3 | namespace AlecRabbit\Reports\Core; |
||
4 | |||
5 | use AlecRabbit\Reports\Contracts\ReportableInterface; |
||
6 | use AlecRabbit\Reports\Contracts\ReportInterface; |
||
7 | |||
8 | abstract class Reportable implements ReportableInterface |
||
9 | { |
||
10 | /** @var ReportInterface */ |
||
11 | protected $report; |
||
12 | |||
13 | 2 | public function __construct() |
|
14 | { |
||
15 | 2 | $this->report = $this->createEmptyReport(); |
|
16 | 2 | } |
|
17 | |||
18 | abstract protected function createEmptyReport(): ReportInterface; |
||
19 | |||
20 | /** |
||
21 | * @param bool $rebuild Rebuild report object |
||
22 | * @return ReportInterface |
||
23 | */ |
||
24 | 2 | public function report(bool $rebuild = true): ReportInterface |
|
25 | { |
||
26 | 2 | $rebuild = $this->checkReport() || $rebuild; |
|
27 | 2 | if (true === $rebuild) { |
|
28 | 2 | $this->checkConditions(); |
|
29 | 2 | $this->beforeReport(); |
|
30 | /** @noinspection PhpParamsInspection */ |
||
31 | 2 | $this->report->buildOn($this); |
|
32 | } |
||
33 | return |
||
34 | 2 | $this->report; |
|
35 | } |
||
36 | |||
37 | /** |
||
38 | * @return bool |
||
39 | * @psalm-suppress RedundantConditionGivenDocblockType |
||
40 | */ |
||
41 | 2 | protected function checkReport(): bool |
|
42 | { |
||
43 | 2 | if ($this->report instanceof ReportInterface) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
44 | 1 | return false; |
|
45 | } |
||
46 | 1 | $this->report = $this->createEmptyReport(); |
|
47 | 1 | return true; |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * Checks if all conditions needed for report are met |
||
52 | */ |
||
53 | 2 | protected function checkConditions(): void |
|
54 | { |
||
55 | 2 | } |
|
56 | |||
57 | /** |
||
58 | * Makes all necessary actions before report |
||
59 | */ |
||
60 | 2 | protected function beforeReport(): void |
|
61 | { |
||
62 | 2 | } |
|
63 | } |
||
64 |