Passed
Pull Request — master (#99)
by Sergei
02:37
created

getSolution()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Widget;
6
7
use Throwable;
8
use Yiisoft\Definitions\Exception\NotInstantiableException;
9
use Yiisoft\FriendlyException\FriendlyExceptionInterface;
10
11
final class NotInstantiableWithoutWidgetFactoryInitializationException extends NotInstantiableException implements FriendlyExceptionInterface
12
{
13 1
    public function __construct(
14
        private string $widgetClassName,
15
        private Throwable $previous,
16
    )
17
    {
18 1
        parent::__construct(
19 1
            'Failed to create a widget "' . $this->widgetClassName . '". '.  $previous->getMessage() .
20 1
            ' Perhaps you need to initialize "' . WidgetFactory::class . '" with DI container to resolve dependencies.',
21 1
            previous: $previous,
22 1
        );
23
    }
24
25 1
    public function getName(): string
26
    {
27 1
        return 'Failed to create a widget "' . $this->widgetClassName . '". ' . $this->previous->getMessage();
28
    }
29
30 1
    public function getSolution(): ?string
31
    {
32 1
        $widgetFactoryClass = WidgetFactory::class;
33
34 1
        return <<<SOLUTION
35 1
            Perhaps you need to initialize "$widgetFactoryClass" with DI container to resolve dependencies.
36
37
            To initialize the widget factory call `WidgetFactory::initialize()` before using the widget.
38
            It is a good idea to do that for the whole application.
39
40
            Example:
41
42
            ```php
43
            /**
44
             * @var Psr\Container\ContainerInterface \$container
45
             */
46
47
            Yiisoft\Widget\WidgetFactory::initialize(
48
                container: \$container,
49
                definitions: [MyWidget::class => new MyWidget(/*...*/)],
50
                themes: [
51
                    'custom' => [
52
                        MyWidget::class => [
53
                            'setValue()' => [42],
54
                        ],
55
                    ],
56
                ],
57
                validate: true, // Whether definitions need to be validated.
58
            );
59
            ```
60
61
            See Yii example in the configuration file of this package `config/bootstrap.php`.
62 1
            SOLUTION;
63
    }
64
}
65