Test Setup Failed
Push — master ( 82bc16...cbe1a2 )
by Alec
03:13
created

AbstractReportable   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setFormatter() 0 10 1
A __construct() 0 3 1
A report() 0 3 1
A setBindings() 0 9 1
A setReportFormatterDependencies() 0 20 3
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Reports\Core;
4
5
use AlecRabbit\Formatters\Contracts\FormatterInterface;
6
use AlecRabbit\Formatters\Core\AbstractFormatter;
7
use AlecRabbit\Formatters\DefaultFormatter;
8
use AlecRabbit\Reports\Contracts\ReportableInterface;
9
use AlecRabbit\Reports\DefaultReport;
10
use Illuminate\Contracts\Container\BindingResolutionException;
11
use function AlecRabbit\container;
12
13
abstract class AbstractReportable implements ReportableInterface
14
{
15
    /** @var string */
16
    protected $reportClass;
17
    /** @var string */
18
    protected $formatterClass;
19
20
    public function __construct()
21
    {
22
        $this->setBindings();
23
    }
24
25
    protected function setBindings(string $reportClass = null, string $formatterClass = null): void
26
    {
27
        $this->reportClass = $reportClass ?? DefaultReport::class;
28
        $this->formatterClass = $formatterClass ?? DefaultFormatter::class;
29
        $this->setReportFormatterDependencies(
30
            $this->reportClass,
31
            FormatterInterface::class,
32
            $this->formatterClass,
33
            null
34
        );
35
    }
36
37
    /**
38
     * @param string $reportClass
39
     * @param string $formatterClass
40
     * @param AbstractFormatter|string|\Closure $formatter
41
     * @param null|int $options
42
     *
43
     * @psalm-suppress InvalidScalarArgument
44
     */
45
    protected function setReportFormatterDependencies(
46
        string $reportClass,
47
        string $formatterClass,
48
        $formatter,
49
        ?int $options
50
    ): void {
51
        if ($formatter instanceof AbstractFormatter) {
52
            $formatter = static function () use ($formatter): AbstractFormatter {
53
                return $formatter;
54
            };
55
        }
56
        container()
57
            ->when($reportClass)
58
            ->needs($formatterClass)
59
            ->give($formatter);
60
        if (is_string($formatter)) {
61
            container()
62
                ->when($formatter)
63
                ->needs('$options')
64
                ->give($options);
65
        }
66
    }
67
68
    /**
69
     * @param null|AbstractFormatter|string|\Closure $formatter
70
     * @param null|int $options
71
     */
72
    public function setFormatter($formatter = null, ?int $options = null): void
73
    {
74
        /** @noinspection ProperNullCoalescingOperatorUsageInspection */
75
        $formatter = $formatter ?? $this->formatterClass;
76
77
        $this->setReportFormatterDependencies(
78
            $this->reportClass,
79
            FormatterInterface::class,
80
            $formatter,
81
            $options
82
        );
83
    }
84
85
    /**
86
     * @noinspection ReturnTypeCanBeDeclaredInspection
87
     *
88
     * @return AbstractReport
89
     * @throws BindingResolutionException
90
     *
91
     */
92
    public function report(): AbstractReport
93
    {
94
        return container()->make($this->reportClass, ['reportable' => $this]);
95
    }
96
}
97