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

AbstractDefinition::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
eloc 2
nc 1
nop 1
crap 1
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