Test Failed
Pull Request — master (#870)
by Aleksei
06:40
created

Scope   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 15
dl 0
loc 49
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getParentScope() 0 3 1
A setParent() 0 11 4
A destruct() 0 4 1
A getParent() 0 3 1
A __construct() 0 3 1
A getScopeName() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Core\Internal;
6
7
use Spiral\Core\Exception\Scope\NamedScopeDuplicationException;
8
9
/**
10
 * @internal
11
 */
12
final class Scope
13
{
14
    private ?\Spiral\Core\Container $parent = null;
15
    private ?self $parentScope = null;
16
17
    public function __construct(
18
        private readonly ?string $scopeName = null,
19
    ) {
20
    }
21
22
    public function getScopeName(): ?string
23
    {
24
        return $this->scopeName;
25
    }
26
27
    /**
28
     * Link the current scope with its parent scope and container.
29
     *
30
     * @throws NamedScopeDuplicationException
31
     */
32
    public function setParent(\Spiral\Core\Container $parent, self $parentScope): void
33
    {
34
        $this->parent = $parent;
35
        $this->parentScope = $parentScope;
36
37
        // Check a scope with the same name is not already registered
38
        if ($this->scopeName !== null) {
39
            $tmp = $this;
40
            while ($tmp->parentScope !== null) {
41
                $tmp = $tmp->parentScope;
42
                $tmp->scopeName !== $this->scopeName ?: throw new NamedScopeDuplicationException($this->scopeName);
43
            }
44
        }
45
    }
46
47
    public function getParent(): ?\Spiral\Core\Container
48
    {
49
        return $this->parent;
50
    }
51
52
    public function getParentScope(): ?self
53
    {
54
        return $this->parentScope;
55
    }
56
57
    public function destruct(): void
58
    {
59
        $this->parent = null;
60
        $this->parentScope = null;
61
    }
62
}
63