Completed
Push — master ( f46373...d10f54 )
by
unknown
04:56
created

AbstractDefinition   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 52.94%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 61
rs 10
c 1
b 0
f 1
ccs 9
cts 17
cp 0.5294

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A end() 0 4 1
A getParentDefinition() 0 8 2
A resolveReferenceValue() 0 12 4
1
<?php declare(strict_types = 1);
2
/**
3
 * Created by PhpStorm.
4
 * User: root
5
 * Date: 02.08.16
6
 * Time: 0:46.
7
 */
8
namespace samsonframework\container\definition;
9
10
use samsonframework\container\definition\reference\ClassReference;
11
use samsonframework\container\definition\reference\ReferenceInterface;
12
use samsonframework\container\definition\reference\ResourceReference;
13
use samsonframework\container\definition\reference\ServiceReference;
14
use samsonframework\container\exception\ParentDefinitionNotFoundException;
15
use samsonframework\container\exception\ReferenceNotImplementsException;
16
17
/**
18
 * Class AbstractDefinition
19
 *
20
 * @package samsonframework\container\definition
21
 */
22
abstract class AbstractDefinition
23
{
24
    /** @var AbstractDefinition Parent definition */
25
    protected $parentDefinition;
26
27
    /**
28
     * AbstractDefinition constructor.
29
     *
30
     * @param AbstractDefinition $parentDefinition
31
     */
32 9
    public function __construct(AbstractDefinition $parentDefinition = null)
33
    {
34 9
        $this->parentDefinition = $parentDefinition;
35 9
    }
36
37
    /**
38
     * End definition and get control to parent
39
     *
40
     * @return AbstractDefinition
41
     * @throws ParentDefinitionNotFoundException
42
     */
43 8
    public function end(): AbstractDefinition
44
    {
45 8
        return $this->getParentDefinition();
46
    }
47
48
    /**
49
     * Get parent definition
50
     *
51
     * @return AbstractDefinition
52
     * @throws ParentDefinitionNotFoundException
53
     */
54 8
    public function getParentDefinition(): AbstractDefinition
55
    {
56 8
        if ($this->parentDefinition === null) {
57 1
            throw new ParentDefinitionNotFoundException();
58
        }
59
60 8
        return $this->parentDefinition;
61
    }
62
63
    /**
64
     * Get correct value from reference
65
     *
66
     * @param ReferenceInterface $reference
67
     * @return string
68
     * @throws ReferenceNotImplementsException
69
     */
70
    protected function resolveReferenceValue(ReferenceInterface $reference): string
71
    {
72
        if ($reference instanceof ClassReference) {
73
            return $reference->getClassName();
74
        } elseif ($reference instanceof ServiceReference) {
75
            return $reference->getName();
76
        } elseif ($reference instanceof ResourceReference) {
77
            return $reference->getValue();
78
        } else {
79
            throw new ReferenceNotImplementsException();
80
        }
81
    }
82
}
83