Passed
Branch refactor (dbb736)
by Mihail
03:14
created

DIException::forMissingArgument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 4
c 1
b 0
f 1
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Koded package.
5
 *
6
 * (c) Mihail Binev <[email protected]>
7
 *
8
 * Please view the LICENSE distributed with this source code
9
 * for the full copyright and license information.
10
 *
11
 */
12
13
namespace Koded;
14
15
use Koded\Exceptions\KodedException;
16
use Psr\Container\{ContainerExceptionInterface, NotFoundExceptionInterface};
17
18
class DIException extends KodedException implements ContainerExceptionInterface
19
{
20
    public const
21
        E_CIRCULAR_DEPENDENCY    = 1,
22
        E_NON_PUBLIC_METHOD      = 2,
23
        E_CANNOT_INSTANTIATE     = 3,
24
        E_INVALID_PARAMETER_NAME = 4,
25
        E_INSTANCE_NOT_FOUND     = 5,
26
        E_MISSING_ARGUMENT       = 6;
27
28
    protected $messages = [
29
        self::E_CIRCULAR_DEPENDENCY    => 'Circular dependency detected while creating an instance for ":class"',
30
        self::E_NON_PUBLIC_METHOD      => 'Failed to create an instance, because the method ":method" is not public',
31
        self::E_CANNOT_INSTANTIATE     => 'Cannot instantiate the ":type" :class',
32
        self::E_INVALID_PARAMETER_NAME => 'Provide a valid name for the global parameter',
33
        self::E_INSTANCE_NOT_FOUND     => 'The requested instance :id is not found in the container',
34
        self::E_MISSING_ARGUMENT       => 'Required parameter "$:name" is missing at position :position in :function()',
35
    ];
36
37
38 1
    public static function forCircularDependency(string $class): ContainerExceptionInterface
39
    {
40 1
        return new self(self::E_CIRCULAR_DEPENDENCY, [':class' => $class]);
41
    }
42
43 1
    public static function forNonPublicMethod(string $method): ContainerExceptionInterface
44
    {
45 1
        return new self(self::E_NON_PUBLIC_METHOD, [':method' => $method]);
46
    }
47
48 4
    public static function cannotInstantiate(string $class, string $type): ContainerExceptionInterface
49
    {
50 4
        return new self(self::E_CANNOT_INSTANTIATE, [':class' => $class, ':type' => $type]);
51
    }
52
53 7
    public static function forInvalidParameterName(): ContainerExceptionInterface
54
    {
55 7
        return new self(self::E_INVALID_PARAMETER_NAME);
56
    }
57
58 1
    public static function forMissingArgument(string $name, int $position, string $function): ContainerExceptionInterface
59
    {
60 1
        return new self(self::E_MISSING_ARGUMENT, [
61 1
            ':name' => $name,
62 1
            ':position' => $position,
63 1
            ':function' => $function,
64
        ]);
65
    }
66
}
67
68
69
class DIInstanceNotFound extends DIException implements NotFoundExceptionInterface
70
{
71 1
    public static function for(string $id): NotFoundExceptionInterface
72
    {
73 1
        return new self(self::E_INSTANCE_NOT_FOUND, [':id' => $id]);
74
    }
75
}
76