Passed
Pull Request — master (#1)
by Mihail
02:55
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 Psr\Container\{ContainerExceptionInterface, NotFoundExceptionInterface};
16
use LogicException;
17
use Throwable;
18
19
class DIException extends LogicException implements ContainerExceptionInterface
20
{
21
    public const
22
        E_CIRCULAR_DEPENDENCY    = 7001,
23
        E_NON_PUBLIC_METHOD      = 7002,
24
        E_CANNOT_INSTANTIATE     = 7003,
25
        E_INVALID_PARAMETER_NAME = 7004,
26
        E_INSTANCE_NOT_FOUND     = 7005,
27
        E_MISSING_ARGUMENT       = 7006;
28
29
    protected $messages = [
30
        self::E_CIRCULAR_DEPENDENCY    => 'Circular dependency detected while creating an instance for ":class"',
31
        self::E_NON_PUBLIC_METHOD      => 'Failed to create an instance, because the method ":method" is not public',
32
        self::E_CANNOT_INSTANTIATE     => 'Cannot instantiate the ":type :class"',
33
        self::E_INVALID_PARAMETER_NAME => 'Provide a valid name for the global parameter: ":name"',
34
        self::E_INSTANCE_NOT_FOUND     => 'The requested instance :id is not found in the container',
35
        self::E_MISSING_ARGUMENT       => 'Required parameter "$:name" is missing at position :position in :function()',
36
    ];
37
38 15
    public function __construct(int $code, array $arguments = [], Throwable $previous = null)
39
    {
40 15
        parent::__construct(strtr(
41 15
            $this->messages[$code] ?? ':message',
42 15
            $arguments + [':message' => $this->message]
43
        ), $code, $previous);
44 15
    }
45
46 1
    public static function forCircularDependency(string $class): ContainerExceptionInterface
47
    {
48 1
        return new self(self::E_CIRCULAR_DEPENDENCY, [':class' => $class]);
49
    }
50
51 1
    public static function forNonPublicMethod(string $method): ContainerExceptionInterface
52
    {
53 1
        return new self(self::E_NON_PUBLIC_METHOD, [':method' => $method]);
54
    }
55
56 4
    public static function cannotInstantiate(string $class, string $type): ContainerExceptionInterface
57
    {
58 4
        return new self(self::E_CANNOT_INSTANTIATE, [':class' => $class, ':type' => $type]);
59
    }
60
61 7
    public static function forInvalidParameterName(string $name): ContainerExceptionInterface
62
    {
63 7
        return new self(self::E_INVALID_PARAMETER_NAME, [':name' => $name]);
64
    }
65
66 1
    public static function forMissingArgument(string $name, int $position, string $function): ContainerExceptionInterface
67
    {
68 1
        return new self(self::E_MISSING_ARGUMENT, [
69 1
            ':name' => $name,
70 1
            ':position' => $position,
71 1
            ':function' => $function,
72
        ]);
73
    }
74
}
75
76
77
class DIInstanceNotFound extends DIException implements NotFoundExceptionInterface
78
{
79 1
    public static function for(string $id): NotFoundExceptionInterface
80
    {
81 1
        return new self(self::E_INSTANCE_NOT_FOUND, [':id' => $id]);
82
    }
83
}
84