NotFoundException::getId()   A
last analyzed

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\Di;
6
7
use Exception;
8
use Psr\Container\NotFoundExceptionInterface;
9
use Throwable;
10
11
/**
12
 * `NotFoundException` is thrown when no definition or class was found in the container for a given ID.
13
 */
14
final class NotFoundException extends Exception implements NotFoundExceptionInterface
15
{
16
    /**
17
     * @param string $id ID of the definition or name of the class that was not found.
18
     * @param string[] $buildStack Stack of IDs of services requested definition or class that was not found.
19
     */
20 19
    public function __construct(
21
        private string $id,
22
        private array $buildStack = [],
23
        ?Throwable $previous = null,
24
    ) {
25 19
        if (empty($this->buildStack)) {
26 3
            $message = sprintf('No definition or class found or resolvable for "%s".', $id);
27 16
        } elseif ($this->buildStack === [$id]) {
28 11
            $message = sprintf('No definition or class found or resolvable for "%s" while building it.', $id);
29
        } else {
30 6
            $message = sprintf(
31 6
                'No definition or class found or resolvable for "%s" while building "%s".',
32 6
                end($this->buildStack),
33 6
                implode('" -> "', $buildStack),
34 6
            );
35
        }
36
37 19
        parent::__construct($message, previous: $previous);
38
    }
39
40 12
    public function getId(): string
41
    {
42 12
        return $this->id;
43
    }
44
45
    /**
46
     * @return string[]
47
     */
48 2
    public function getBuildStack(): array
49
    {
50 2
        return $this->buildStack;
51
    }
52
}
53