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

getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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