Skip to content

Commit 46c3b69

Browse files
committed
wip clean up statements
1 parent 28a7ea3 commit 46c3b69

7 files changed

+23
-78
lines changed

src/EventListener/OpenCypher/SimilarNodeQueueCreateToStatementEventListener.php

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
use Laudis\Neo4j\Databags\Statement;
88
use Psr\Log\LoggerInterface;
99
use Syndesi\CypherDataStructures\Contract\NodeInterface;
10-
use Syndesi\CypherDataStructures\Contract\PropertyNameInterface;
1110
use Syndesi\CypherDataStructures\Helper\ToCypherHelper;
1211
use Syndesi\CypherEntityManager\Contract\OnActionCypherElementToStatementEventListenerInterface;
1312
use Syndesi\CypherEntityManager\Contract\SimilarNodeQueueInterface;
1413
use Syndesi\CypherEntityManager\Contract\SimilarNodeQueueStatementInterface;
1514
use Syndesi\CypherEntityManager\Event\ActionCypherElementToStatementEvent;
15+
use Syndesi\CypherEntityManager\Helper\StructureHelper;
1616
use Syndesi\CypherEntityManager\Type\ActionType;
1717

1818
class SimilarNodeQueueCreateToStatementEventListener implements OnActionCypherElementToStatementEventListenerInterface, SimilarNodeQueueStatementInterface
@@ -50,33 +50,13 @@ public static function similarNodeQueueStatement(SimilarNodeQueueInterface $simi
5050
if (!$firstNode) {
5151
$firstNode = $node;
5252
}
53-
$properties = [];
54-
$identifiers = [];
55-
/** @var PropertyNameInterface $property */
56-
foreach ($node->getProperties() as $property) {
57-
if ($node->hasIdentifier($property)) {
58-
$identifiers[$property->getPropertyName()] = $node->getIdentifier($property);
59-
continue;
60-
}
61-
$properties[$property->getPropertyName()] = $node->getProperty($property);
62-
}
6353
$batch[] = [
64-
'identifier' => $identifiers,
65-
'property' => $properties,
54+
'identifier' => StructureHelper::getIdentifiersFromElementAsArray($node),
55+
'property' => StructureHelper::getPropertiesFromElementAsArray($node),
6656
];
6757
}
6858
if (!$firstNode) {
69-
// aka empty queue
70-
return new Statement("MATCH (n) LIMIT 0", []);
71-
}
72-
$identifiers = [];
73-
/** @var PropertyNameInterface $identifier */
74-
foreach ($firstNode->getIdentifiers() as $identifier) {
75-
$identifiers[] = sprintf(
76-
"%s: row.identifier.%s",
77-
$identifier->getPropertyName(),
78-
$identifier->getPropertyName(),
79-
);
59+
return StructureHelper::getEmptyStatement();
8060
}
8161

8262
return new Statement(
@@ -85,7 +65,7 @@ public static function similarNodeQueueStatement(SimilarNodeQueueInterface $simi
8565
"CREATE (n%s {%s})\n".
8666
"SET n += row.property",
8767
ToCypherHelper::nodeLabelStorageToCypherLabelString($firstNode->getNodeLabels()),
88-
join(', ', $identifiers)
68+
StructureHelper::getIdentifiersFromElementAsCypherVariableString($firstNode, 'row.identifier')
8969
),
9070
[
9171
'batch' => $batch,

src/EventListener/OpenCypher/SimilarNodeQueueDeleteToStatementEventListener.php

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
use Laudis\Neo4j\Databags\Statement;
88
use Psr\Log\LoggerInterface;
99
use Syndesi\CypherDataStructures\Contract\NodeInterface;
10-
use Syndesi\CypherDataStructures\Contract\PropertyNameInterface;
1110
use Syndesi\CypherDataStructures\Helper\ToCypherHelper;
1211
use Syndesi\CypherEntityManager\Contract\OnActionCypherElementToStatementEventListenerInterface;
1312
use Syndesi\CypherEntityManager\Contract\SimilarNodeQueueInterface;
1413
use Syndesi\CypherEntityManager\Contract\SimilarNodeQueueStatementInterface;
1514
use Syndesi\CypherEntityManager\Event\ActionCypherElementToStatementEvent;
15+
use Syndesi\CypherEntityManager\Helper\StructureHelper;
1616
use Syndesi\CypherEntityManager\Type\ActionType;
1717

1818
class SimilarNodeQueueDeleteToStatementEventListener implements OnActionCypherElementToStatementEventListenerInterface, SimilarNodeQueueStatementInterface
@@ -50,28 +50,10 @@ public static function similarNodeQueueStatement(SimilarNodeQueueInterface $simi
5050
if (!$firstNode) {
5151
$firstNode = $node;
5252
}
53-
$identifiers = [];
54-
/** @var PropertyNameInterface $property */
55-
foreach ($node->getProperties() as $property) {
56-
if ($node->hasIdentifier($property)) {
57-
$identifiers[$property->getPropertyName()] = $node->getIdentifier($property);
58-
continue;
59-
}
60-
}
61-
$batch[] = $identifiers;
53+
$batch[] = StructureHelper::getIdentifiersFromElementAsArray($node);
6254
}
6355
if (!$firstNode) {
64-
// aka empty queue
65-
return new Statement("MATCH (n) LIMIT 0", []);
66-
}
67-
$identifiers = [];
68-
/** @var PropertyNameInterface $identifier */
69-
foreach ($firstNode->getIdentifiers() as $identifier) {
70-
$identifiers[] = sprintf(
71-
"%s: row.%s",
72-
$identifier->getPropertyName(),
73-
$identifier->getPropertyName(),
74-
);
56+
return StructureHelper::getEmptyStatement();
7557
}
7658

7759
return new Statement(
@@ -80,7 +62,7 @@ public static function similarNodeQueueStatement(SimilarNodeQueueInterface $simi
8062
"MATCH (n%s {%s})\n".
8163
"DETACH DELETE n",
8264
ToCypherHelper::nodeLabelStorageToCypherLabelString($firstNode->getNodeLabels()),
83-
join(', ', $identifiers)
65+
StructureHelper::getIdentifiersFromElementAsCypherVariableString($firstNode, 'row')
8466
),
8567
[
8668
'batch' => $batch,

src/EventListener/OpenCypher/SimilarNodeQueueMergeToStatementEventListener.php

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
use Laudis\Neo4j\Databags\Statement;
88
use Psr\Log\LoggerInterface;
99
use Syndesi\CypherDataStructures\Contract\NodeInterface;
10-
use Syndesi\CypherDataStructures\Contract\PropertyNameInterface;
1110
use Syndesi\CypherDataStructures\Helper\ToCypherHelper;
1211
use Syndesi\CypherEntityManager\Contract\OnActionCypherElementToStatementEventListenerInterface;
1312
use Syndesi\CypherEntityManager\Contract\SimilarNodeQueueInterface;
1413
use Syndesi\CypherEntityManager\Contract\SimilarNodeQueueStatementInterface;
1514
use Syndesi\CypherEntityManager\Event\ActionCypherElementToStatementEvent;
15+
use Syndesi\CypherEntityManager\Helper\StructureHelper;
1616
use Syndesi\CypherEntityManager\Type\ActionType;
1717

1818
class SimilarNodeQueueMergeToStatementEventListener implements OnActionCypherElementToStatementEventListenerInterface, SimilarNodeQueueStatementInterface
@@ -50,33 +50,13 @@ public static function similarNodeQueueStatement(SimilarNodeQueueInterface $simi
5050
if (!$firstNode) {
5151
$firstNode = $node;
5252
}
53-
$properties = [];
54-
$identifiers = [];
55-
/** @var PropertyNameInterface $property */
56-
foreach ($node->getProperties() as $property) {
57-
if ($node->hasIdentifier($property)) {
58-
$identifiers[$property->getPropertyName()] = $node->getIdentifier($property);
59-
continue;
60-
}
61-
$properties[$property->getPropertyName()] = $node->getProperty($property);
62-
}
6353
$batch[] = [
64-
'identifier' => $identifiers,
65-
'property' => $properties,
54+
'identifier' => StructureHelper::getIdentifiersFromElementAsArray($node),
55+
'property' => StructureHelper::getPropertiesFromElementAsArray($node),
6656
];
6757
}
6858
if (!$firstNode) {
69-
// aka empty queue
70-
return new Statement("MATCH (n) LIMIT 0", []);
71-
}
72-
$identifiers = [];
73-
/** @var PropertyNameInterface $identifier */
74-
foreach ($firstNode->getIdentifiers() as $identifier) {
75-
$identifiers[] = sprintf(
76-
"%s: row.identifier.%s",
77-
$identifier->getPropertyName(),
78-
$identifier->getPropertyName(),
79-
);
59+
return StructureHelper::getEmptyStatement();
8060
}
8161

8262
return new Statement(
@@ -88,7 +68,7 @@ public static function similarNodeQueueStatement(SimilarNodeQueueInterface $simi
8868
"ON MATCH\n".
8969
" SET n += row.property",
9070
ToCypherHelper::nodeLabelStorageToCypherLabelString($firstNode->getNodeLabels()),
91-
join(', ', $identifiers)
71+
StructureHelper::getIdentifiersFromElementAsCypherVariableString($firstNode, 'row.identifier')
9272
),
9373
[
9474
'batch' => $batch,

src/EventListener/OpenCypher/SimilarRelationQueueCreateToStatementEventListener.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ public static function similarRelationQueueStatement(SimilarRelationQueueInterfa
7171
];
7272
}
7373
if (!$firstRelation) {
74-
// aka empty queue
75-
return new Statement("MATCH (n) LIMIT 0", []);
74+
return StructureHelper::getEmptyStatement();
7675
}
7776
if (!$firstRelationStartNode) {
7877
throw new InvalidArgumentException('start node of relations can not be null');

src/EventListener/OpenCypher/SimilarRelationQueueDeleteToStatementEventListener.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ public static function similarRelationQueueStatement(SimilarRelationQueueInterfa
7070
];
7171
}
7272
if (!$firstRelation) {
73-
// aka empty queue
74-
return new Statement("MATCH (n) LIMIT 0", []);
73+
return StructureHelper::getEmptyStatement();
7574
}
7675
if (!$firstRelationStartNode) {
7776
throw new InvalidArgumentException('start node of relations can not be null');

src/EventListener/OpenCypher/SimilarRelationQueueMergeToStatementEventListener.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ public static function similarRelationQueueStatement(SimilarRelationQueueInterfa
7171
];
7272
}
7373
if (!$firstRelation) {
74-
// aka empty queue
75-
return new Statement("MATCH (n) LIMIT 0", []);
74+
return StructureHelper::getEmptyStatement();
7675
}
7776
if (!$firstRelationStartNode) {
7877
throw new InvalidArgumentException('start node of relations can not be null');

src/Helper/StructureHelper.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Syndesi\CypherEntityManager\Helper;
66

7+
use Laudis\Neo4j\Databags\Statement;
78
use Syndesi\CypherDataStructures\Contract\HasIdentifiersInterface;
89
use Syndesi\CypherDataStructures\Contract\NodeInterface;
910
use Syndesi\CypherDataStructures\Contract\PropertyNameInterface;
@@ -14,6 +15,11 @@
1415

1516
class StructureHelper
1617
{
18+
public static function getEmptyStatement(): Statement
19+
{
20+
return Statement::create('MATCH (n) LIMIT 0');
21+
}
22+
1723
public static function identifierStorageToString(PropertyStorageInterface $identifiers): string
1824
{
1925
$internalIdentifiers = [];

0 commit comments

Comments
 (0)