Passed
Push — master ( d8c5da...177fc5 )
by Jesse
01:27
created

ExtractionRequest::forProperty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 12
rs 9.9666
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\EntityState\Internal;
4
5
use function is_object;
6
use Stratadox\IdentityMap\MapsObjectsByIdentity as Map;
7
use Stratadox\IdentityMap\NoSuchObject;
8
9
final class ExtractionRequest
10
{
11
    private $value;
12
    private $owner;
13
    private $name;
14
    private $map;
15
    private $visited;
16
17
    private function __construct(
18
        $value,
19
        object $owner,
20
        Name $name,
21
        Map $map,
22
        Visited $visited
23
    ) {
24
        $this->value = $value;
25
        $this->owner = $owner;
26
        $this->name = $name;
27
        $this->map = $map;
28
        $this->visited = $visited;
29
    }
30
31
    public static function for($entity, Map $identityMap): self
32
    {
33
        return new self(
34
            $entity,
35
            $entity,
36
            Name::start(),
37
            $identityMap,
38
            Visited::noneYet()
39
        );
40
    }
41
42
    public function value()
43
    {
44
        return $this->value;
45
    }
46
47
    public function isTheOwner(): bool
48
    {
49
        return $this->value === $this->owner;
50
    }
51
52
    public function objectName(): Name
53
    {
54
        return $this->name->for($this->value);
55
    }
56
57
    public function nameForCounting(): Name
58
    {
59
        return $this->name->toCount($this->value);
60
    }
61
62
    public function name(): Name
63
    {
64
        return $this->name;
65
    }
66
67
    public function pointsToAnotherEntity(): bool
68
    {
69
        return $this->pointsToAKnownEntity() && !$this->isTheOwner();
70
    }
71
72
    public function pointsToAKnownEntity(): bool
73
    {
74
        return is_object($this->value) && $this->map->hasThe($this->value);
75
    }
76
77
    /** @throws NoSuchObject */
78
    public function otherEntityId(): string
79
    {
80
        return $this->map->idOf($this->value);
81
    }
82
83
    public function isRecursive(): bool
84
    {
85
        return $this->visited->already($this->value);
86
    }
87
88
    public function visitedName(): string
89
    {
90
        return $this->visited->name($this->value);
91
    }
92
93
    public function withVisitation(): self
94
    {
95
        return new self(
96
            $this->value,
97
            $this->owner,
98
            $this->name,
99
            $this->map,
100
            $this->visited->add($this->value, $this->name)
101
        );
102
    }
103
104
    public function forCollectionItem(
105
        iterable $collection,
106
        string $key,
107
        $value
108
    ): self {
109
        return new self(
110
            $value,
111
            $this->owner,
112
            $this->name->forItem($collection, $key),
113
            $this->map,
114
            $this->visited
115
        );
116
    }
117
118
    public function forProperty(ReflectionProperty $property): self
119
    {
120
        $name = $this->isTheOwner() ?
121
            $this->name :
122
            $this->name->for($this->value);
123
124
        return new self(
125
            $property->getValue($this->value),
126
            $this->owner,
127
            $name->forReflected($property),
128
            $this->map,
129
            $this->visited
130
        );
131
    }
132
}
133