Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,6 @@ parameters:
count: 1
path: src/GraphDatabasePlugins/Neo4j/Persistence/Neo4jQueryStore.php

-
message: "#^Cannot call method get\\(\\) on mixed\\.$#"
count: 1
path: src/GraphDatabasePlugins/Neo4j/Persistence/Neo4jSubjectUpdater.php

-
message: "#^Method ProfessionalWiki\\\\NeoWiki\\\\GraphDatabasePlugins\\\\Neo4j\\\\Persistence\\\\Neo4jSubjectUpdater\\:\\:getNodeLabels\\(\\) should return array\\<string\\> but returns array\\<int, mixed\\>\\.$#"
count: 1
path: src/GraphDatabasePlugins/Neo4j/Persistence/Neo4jSubjectUpdater.php

-
message: "#^Cannot access property \\$page_title on array\\|stdClass\\|false\\.$#"
count: 1
Expand Down
52 changes: 52 additions & 0 deletions src/GraphDatabasePlugins/Neo4j/Persistence/Neo4jNodeLabels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare( strict_types = 1 );

namespace ProfessionalWiki\NeoWiki\GraphDatabasePlugins\Neo4j\Persistence;

use Laudis\Neo4j\Contracts\TransactionInterface;
use Laudis\Neo4j\Databags\SummarizedResult;

/**
* Reads and removes the labels of a Neo4j node identified by its id, within a given transaction.
*
* Shared by Neo4jSubjectUpdater (reconciling a subject's labels with its schema) and
* Neo4jProjectionStore (stripping a subject down to a stub). Those classes hold their transaction
* differently, so the transaction is passed in per call rather than owned here.
*/
class Neo4jNodeLabels {

/**
* @return string[]
*/
public static function read( TransactionInterface $transaction, string $nodeId ): array {
/**
* @var SummarizedResult $result
*/
$result = $transaction->run(
'MATCH (n {id: $id}) RETURN labels(n) AS labels',
[ 'id' => $nodeId ]
);

if ( $result->isEmpty() ) {
return [];
}

return $result->first()->get( 'labels' )->toArray();
}

/**
* @param string[] $labels
*/
public static function remove( TransactionInterface $transaction, string $nodeId, array $labels ): void {
if ( $labels === [] ) {
return;
}

$transaction->run(
'MATCH (n {id: $id}) REMOVE n:' . Cypher::buildLabelList( $labels ),
[ 'id' => $nodeId ]
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -217,38 +217,11 @@ private function reduceSubjectToStub( TransactionInterface $transaction, Subject
}

private function removeNonStubLabels( TransactionInterface $transaction, SubjectId $subjectId ): void {
$labelsToRemove = array_diff(
$this->getSubjectLabels( $transaction, $subjectId ),
[ 'Subject' ]
Neo4jNodeLabels::remove(
$transaction,
$subjectId->text,
array_diff( Neo4jNodeLabels::read( $transaction, $subjectId->text ), [ 'Subject' ] )
);

if ( $labelsToRemove === [] ) {
return;
}

$transaction->run(
'MATCH (subject {id: $subjectId}) REMOVE subject:' . Cypher::buildLabelList( $labelsToRemove ),
[ 'subjectId' => $subjectId->text ]
);
}

/**
* @return string[]
*/
private function getSubjectLabels( TransactionInterface $transaction, SubjectId $subjectId ): array {
/**
* @var SummarizedResult $result
*/
$result = $transaction->run(
'MATCH (subject {id: $subjectId}) RETURN labels(subject) AS labels',
[ 'subjectId' => $subjectId->text ]
);

if ( $result->isEmpty() ) {
return [];
}

return $result->first()->get( 'labels' )->toArray();
}

private function subjectHasIncomingRelations( TransactionInterface $transaction, SubjectId $subjectId ): bool {
Expand Down
38 changes: 2 additions & 36 deletions src/GraphDatabasePlugins/Neo4j/Persistence/Neo4jSubjectUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
namespace ProfessionalWiki\NeoWiki\GraphDatabasePlugins\Neo4j\Persistence;

use Laudis\Neo4j\Contracts\TransactionInterface;
use Laudis\Neo4j\Databags\SummarizedResult;
use Laudis\Neo4j\Types\CypherList;
use ProfessionalWiki\NeoWiki\Application\SchemaLookup;
use ProfessionalWiki\NeoWiki\Domain\Page\PageId;
use ProfessionalWiki\NeoWiki\Domain\Schema\Schema;
use ProfessionalWiki\NeoWiki\Domain\Statement;
use ProfessionalWiki\NeoWiki\Domain\Subject\StatementList;
use ProfessionalWiki\NeoWiki\Domain\Subject\Subject;
use ProfessionalWiki\NeoWiki\Domain\Subject\SubjectId;
use Psr\Log\LoggerInterface;

class Neo4jSubjectUpdater {
Expand Down Expand Up @@ -124,17 +121,10 @@ private function updateHasSubjectRelation( Subject $subject, bool $isMainSubject
}

private function updateNodeLabels( Subject $subject ): void {
$oldLabels = $this->getNodeLabels( $subject->id );
$oldLabels = Neo4jNodeLabels::read( $this->transaction, $subject->id->text );
$newLabels = [ 'Subject', $subject->getSchemaName()->getText() ];

$labelsToRemove = array_diff( $oldLabels, $newLabels );

if ( $labelsToRemove !== [] ) {
$this->transaction->run(
'MATCH (n {id: $id}) REMOVE n:' . Cypher::buildLabelList( $labelsToRemove ),
[ 'id' => $subject->id->text ]
);
}
Neo4jNodeLabels::remove( $this->transaction, $subject->id->text, array_diff( $oldLabels, $newLabels ) );

$labelsToAdd = array_diff( $newLabels, $oldLabels );

Expand All @@ -146,30 +136,6 @@ private function updateNodeLabels( Subject $subject ): void {
}
}

/**
* @return string[]
*/
private function getNodeLabels( SubjectId $id ): array {
/**
* @var SummarizedResult $result
*/
$result = $this->transaction->run(
'MATCH (n) WHERE n.id = $id RETURN labels(n)',
[ 'id' => $id->text ]
);

if ( $result->isEmpty() ) {
return [];
}

/**
* @var CypherList $labels
*/
$labels = $result->first()->get( 'labels(n)' );

return $labels->toArray();
}

private function updateRelations( Subject $subject, Schema $schema ): void {
$updater = new Neo4jSubjectRelationUpdater(
$subject->getId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

/**
* @covers \ProfessionalWiki\NeoWiki\GraphDatabasePlugins\Neo4j\Persistence\Neo4jProjectionStore
* @covers \ProfessionalWiki\NeoWiki\GraphDatabasePlugins\Neo4j\Persistence\Neo4jNodeLabels
* @group Database
*/
class Neo4jProjectionStoreTest extends NeoWikiIntegrationTestCase {
Expand Down
Loading