Passed
Push — master ( 8e874c...48b189 )
by Aleksei
05:44 queued 21s
created

Scope::getParentFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Core\Internal;
6
7
use Spiral\Core\Exception\Scope\NamedScopeDuplicationException;
8
use Spiral\Core\FactoryInterface;
9
10
/**
11
 * @internal
12
 */
13
final class Scope
14
{
15
    private ?\Spiral\Core\Container $parent = null;
16
    private ?self $parentScope = null;
17
    private ?FactoryInterface $parentFactory = null;
18
19 1306
    public function __construct(
20
        private readonly ?string $scopeName = null,
21
    ) {
22 1306
    }
23
24 1144
    public function getScopeName(): ?string
25
    {
26 1144
        return $this->scopeName;
27
    }
28
29
    /**
30
     * Link the current scope with its parent scope and container.
31
     *
32
     * @throws NamedScopeDuplicationException
33
     */
34 37
    public function setParent(\Spiral\Core\Container $parent, self $parentScope, FactoryInterface $factory): void
35
    {
36 37
        $this->parent = $parent;
37 37
        $this->parentScope = $parentScope;
38 37
        $this->parentFactory = $factory;
39
40
        // Check a scope with the same name is not already registered
41 37
        if ($this->scopeName !== null) {
42 18
            $tmp = $this;
43 18
            while ($tmp->parentScope !== null) {
44 18
                $tmp = $tmp->parentScope;
45 18
                $tmp->scopeName !== $this->scopeName ?: throw new NamedScopeDuplicationException($this->scopeName);
46
            }
47
        }
48
    }
49
50 549
    public function getParent(): ?\Spiral\Core\Container
51
    {
52 549
        return $this->parent;
53
    }
54
55 921
    public function getParentFactory(): ?FactoryInterface
56
    {
57 921
        return $this->parentFactory;
58
    }
59
60
    /**
61
     * Return list of parent scope names.
62
     * The first element is the current scope name, and the next is the closest parent scope name...
63
     *
64
     * @return array<int<0, max>, string|null>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int<0, max>, string|null> at position 2 could not be parsed: Expected '>' at position 2, but found 'int'.
Loading history...
65
     */
66
    public function getParentScopeNames(): array
67
    {
68
        $result = [$this->scopeName];
69
70
        $parent = $this;
71
        while ($parent->parentScope !== null) {
72
            $parent = $parent->parentScope;
73
            $result[] = $parent->scopeName;
74
        }
75
76
        return $result;
77
    }
78
79 884
    public function getParentScope(): ?self
80
    {
81 884
        return $this->parentScope;
82
    }
83
84 634
    public function destruct(): void
85
    {
86 634
        $this->parent = null;
87 634
        $this->parentScope = null;
88
    }
89
}
90