Passed
Pull Request — master (#99)
by Alexander
05:17 queued 02:24
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 1
        parent::__construct(
18 1
            'Failed to create a widget "' . $this->widgetClassName . '". ' . $previous->getMessage() .
19 1
            ' Perhaps you need to initialize "' . WidgetFactory::class . '" with DI container to resolve dependencies.',
20 1
            previous: $previous,
21 1
        );
22
    }
23
24 1
    public function getName(): string
25
    {
26 1
        return 'Failed to create a widget "' . $this->widgetClassName . '". ' . $this->previous->getMessage();
27
    }
28
29 1
    public function getSolution(): ?string
30
    {
31 1
        $widgetFactoryClass = WidgetFactory::class;
32
33 1
        return <<<SOLUTION
34 1
            Perhaps you need to initialize "$widgetFactoryClass" with DI container to resolve dependencies.
35
36
            To initialize the widget factory call `WidgetFactory::initialize()` before using the widget.
37
            It is a good idea to do that for the whole application.
38
39
            Example:
40
41
            ```php
42
            /**
43
             * @var Psr\Container\ContainerInterface \$container
44
             */
45
46
            Yiisoft\Widget\WidgetFactory::initialize(
47
                container: \$container,
48
                definitions: [MyWidget::class => new MyWidget(/*...*/)],
49
                themes: [
50
                    'custom' => [
51
                        MyWidget::class => [
52
                            'setValue()' => [42],
53
                        ],
54
                    ],
55
                ],
56
                validate: true, // Whether definitions need to be validated.
57
            );
58
            ```
59
60
            See Yii example in the configuration file of this package `config/bootstrap.php`.
61 1
            SOLUTION;
62
    }
63
}
64