-
-
Notifications
You must be signed in to change notification settings - Fork 224
/
Copy pathHierarchyRelation.php
144 lines (132 loc) · 5.31 KB
/
HierarchyRelation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/*
* This file is part of the Neos.ContentGraph.DoctrineDbalAdapter package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
declare(strict_types=1);
namespace Neos\ContentGraph\DoctrineDbalAdapter\Domain\Projection;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception as DBALException;
use Neos\ContentGraph\DoctrineDbalAdapter\ContentGraphTableNames;
use Neos\ContentRepository\Core\DimensionSpace\DimensionSpacePoint;
use Neos\ContentGraph\DoctrineDbalAdapter\Domain\Repository\DimensionSpacePointsRepository;
use Neos\ContentRepository\Core\Projection\ContentGraph\NodeTags;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
/**
* The active record for reading and writing hierarchy relations from and to the database
*
* @internal
*/
final readonly class HierarchyRelation
{
public function __construct(
public NodeRelationAnchorPoint $parentNodeAnchor,
public NodeRelationAnchorPoint $childNodeAnchor,
public ContentStreamId $contentStreamId,
public DimensionSpacePoint $dimensionSpacePoint,
public string $dimensionSpacePointHash,
public int $position,
public NodeTags $subtreeTags,
) {
}
public function addToDatabase(Connection $databaseConnection, ContentGraphTableNames $tableNames): void
{
$dimensionSpacePoints = new DimensionSpacePointsRepository($databaseConnection, $tableNames);
$dimensionSpacePoints->insertDimensionSpacePoint($this->dimensionSpacePoint);
try {
$subtreeTagsJson = json_encode($this->subtreeTags, JSON_THROW_ON_ERROR | JSON_FORCE_OBJECT);
} catch (\JsonException $e) {
throw new \RuntimeException(sprintf('Failed to JSON-encode Subtree Tags: %s', $e->getMessage()), 1716484752, $e);
}
try {
$databaseConnection->insert($tableNames->hierarchyRelation(), [
'parentnodeanchor' => $this->parentNodeAnchor->value,
'childnodeanchor' => $this->childNodeAnchor->value,
'contentstreamid' => $this->contentStreamId->value,
'dimensionspacepointhash' => $this->dimensionSpacePointHash,
'position' => $this->position,
'subtreetags' => $subtreeTagsJson,
]);
} catch (DBALException $e) {
throw new \RuntimeException(sprintf('Failed to add hierarchy relation to database: %s', $e->getMessage()), 1716484789, $e);
}
}
public function removeFromDatabase(Connection $databaseConnection, ContentGraphTableNames $tableNames): void
{
try {
$databaseConnection->delete($tableNames->hierarchyRelation(), $this->getDatabaseId());
} catch (DBALException $e) {
throw new \RuntimeException(sprintf('Failed to remove hierarchy relation from database: %s', $e->getMessage()), 1716484823, $e);
}
}
public function assignNewChildNode(
NodeRelationAnchorPoint $childAnchorPoint,
Connection $databaseConnection,
ContentGraphTableNames $tableNames
): void {
try {
$databaseConnection->update(
$tableNames->hierarchyRelation(),
[
'childnodeanchor' => $childAnchorPoint->value
],
$this->getDatabaseId()
);
} catch (DBALException $e) {
throw new \RuntimeException(sprintf('Failed to update hierarchy relation: %s', $e->getMessage()), 1716484843, $e);
}
}
public function assignNewParentNode(
NodeRelationAnchorPoint $parentAnchorPoint,
?int $position,
Connection $databaseConnection,
ContentGraphTableNames $tableNames
): void {
$data = [
'parentnodeanchor' => $parentAnchorPoint->value
];
if (!is_null($position)) {
$data['position'] = $position;
}
try {
$databaseConnection->update(
$tableNames->hierarchyRelation(),
$data,
$this->getDatabaseId()
);
} catch (DBALException $e) {
throw new \RuntimeException(sprintf('Failed to update hierarchy relation: %s', $e->getMessage()), 1716478609, $e);
}
}
public function assignNewPosition(int $position, Connection $databaseConnection, ContentGraphTableNames $tableNames): void
{
try {
$databaseConnection->update(
$tableNames->hierarchyRelation(),
[
'position' => $position
],
$this->getDatabaseId()
);
} catch (DBALException $e) {
throw new \RuntimeException(sprintf('Failed to update hierarchy relation: %s', $e->getMessage()), 1716485014, $e);
}
}
/**
* @return array<string,mixed>
*/
public function getDatabaseId(): array
{
return [
'parentnodeanchor' => $this->parentNodeAnchor->value,
'childnodeanchor' => $this->childNodeAnchor->value,
'contentstreamid' => $this->contentStreamId->value,
'dimensionspacepointhash' => $this->dimensionSpacePointHash
];
}
}