DefinitionException   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
dl 0
loc 76
rs 10
c 1
b 0
f 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDefinition() 0 3 1
A untaggable() 0 8 2
A unavailableMethodCall() 0 8 2
A unavailableConstructorParameters() 0 8 2
A invalidMethodCall() 0 10 3
A unshareable() 0 8 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Habemus\Exception;
5
6
use Habemus\Definition\Definition;
7
8
class DefinitionException extends ContainerException
9
{
10
    /**
11
     * @var Definition
12
     */
13
    protected $definition;
14
15
    public function __construct(Definition $definition, $message = "", $code = 0, $previous = null)
16
    {
17
        $this->definition = $definition;
18
        parent::__construct($message, $code, $previous);
19
    }
20
21
    public function getDefinition(): Definition
22
    {
23
        return $this->definition;
24
    }
25
26
    public static function invalidMethodCall(Definition $definition, $instance, $method): self
27
    {
28
        return new self(
29
            $definition,
30
            sprintf(
31
                "The definition of %s (%s) cannot to call the method (%s::%s).",
32
                $definition->getIdentity() ? "id" : "type",
33
                $definition->getIdentity() ?? get_class($definition),
34
                is_object($instance) ? get_class($instance) : gettype($instance),
35
                $method
36
            )
37
        );
38
    }
39
40
    public static function unavailableConstructorParameters(Definition $definition): self
41
    {
42
        return new self(
43
            $definition,
44
            sprintf(
45
                "The definition of %s (%s) does not accept constructor parameters.",
46
                $definition->getIdentity() ? "id" : "type",
47
                $definition->getIdentity() ?? get_class($definition)
48
            )
49
        );
50
    }
51
52
    public static function unavailableMethodCall(Definition $definition): self
53
    {
54
        return new self(
55
            $definition,
56
            sprintf(
57
                "The definition of %s (%s) does not accept method calls.",
58
                $definition->getIdentity() ? "id" : "type",
59
                $definition->getIdentity() ?? get_class($definition)
60
            )
61
        );
62
    }
63
64
    public static function unshareable(Definition $definition): self
65
    {
66
        return new self(
67
            $definition,
68
            sprintf(
69
                "The definition of %s (%s) is not shareable.",
70
                $definition->getIdentity() ? "id" : "type",
71
                $definition->getIdentity() ?? get_class($definition)
72
            )
73
        );
74
    }
75
76
    public static function untaggable(Definition $definition): self
77
    {
78
        return new self(
79
            $definition,
80
            sprintf(
81
                "The definition of %s (%s) is not taggable.",
82
                $definition->getIdentity() ? "id" : "type",
83
                $definition->getIdentity() ?? get_class($definition)
84
            )
85
        );
86
    }
87
}
88