Skip to content

Commit

Permalink
Wip.
Browse files Browse the repository at this point in the history
  • Loading branch information
Syndesi committed Oct 26, 2024
1 parent bb95407 commit bafb8a5
Show file tree
Hide file tree
Showing 12 changed files with 669 additions and 27 deletions.
98 changes: 71 additions & 27 deletions src/Command/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

namespace App\Command;

use App\Search\CypherSearchStep;
use App\Search\ElasticsearchSearchStep;
use App\Search\ElementHydrationStep;
use App\Style\EmberNexusStyle;
use Laudis\Neo4j\Databags\Statement;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\OutputStyle;
use Syndesi\CypherEntityManager\Type\EntityManager as CypherEntityManager;

/**
* @psalm-suppress PropertyNotSetInConstructor $io
Expand All @@ -22,7 +23,9 @@ class TestCommand extends Command
private OutputStyle $io;

public function __construct(
private CypherEntityManager $cypherEntityManager
private CypherSearchStep $cypherSearchStep,
private ElasticsearchSearchStep $elasticsearchSearchStep,
private ElementHydrationStep $elementHydrationStep,
) {
parent::__construct();
}
Expand All @@ -33,30 +36,71 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$this->io->title('Test');

$result = $this->cypherEntityManager->getClient()->runStatement(
Statement::create(
sprintf(
"MATCH (parent {id: \$parentId})\n".
"MATCH (parent)-[:OWNS]->(children)\n".
"MATCH (parent)-[relations]->(children)\n".
"WITH children, relations\n".
"LIMIT %d\n".
"WITH children, relations\n".
"ORDER BY children.id, relations.id\n".
"WITH COLLECT([children.id, children.updated]) + COLLECT([relations.id, relations.updated]) AS allTuples\n".
"WITH allTuples\n".
"UNWIND allTuples AS tuple\n".
"WITH tuple ORDER BY tuple[0]\n".
'RETURN COLLECT(tuple) AS sortedTuples;',
100
),
[
'parentId' => '7b80b203-2b82-40f5-accd-c7089fe6114e',
]
)
);

print_r($result->toArray());
$globalParameters = [];

// $steps = [
// [
// 'type' => 'cypher',
// 'query' => 'MATCH (n:Data)-[:OWNS]->(:Data) WITH n.id AS elementId, n.created AS created ORDER BY created DESC LIMIT 5 RETURN collect(elementId) as elementIds',
// 'parameters' => [],
// ],
// [
// 'type' => 'elementHydration',
// 'query' => null,
// 'parameters' => [],
// ],
// ];

// $steps = [
// [
// 'type' => 'elasticsearch',
// 'query' => [
// 'match' => [
// 'scenario' => 'general'
// ]
// ],
// 'parameters' => [],
// ],
// [
// 'type' => 'elementHydration',
// 'query' => null,
// 'parameters' => [],
// ],
// ];

$steps = [
[
'type' => 'elementHydration',
'query' => null,
'parameters' => [
'elementIds' => ['9edb178e-1b4a-4518-a9d3-0fa97e6d1007']
],
],
];

$stepRunners = [
'cypher' => $this->cypherSearchStep,
'elementHydration' => $this->elementHydrationStep,
'elasticsearch' => $this->elasticsearchSearchStep,
];

$previousResults = [];
foreach ($steps as $step) {
$parameters = [...$previousResults, ...$step['parameters'], ...$globalParameters];
$previousResults = $stepRunners[$step['type']]->executeStep($step['query'], $parameters)->getResults();
}
$results = $previousResults;

// $results = $this->cypherSearchStep->executeStep('MATCH (n:Data)-[:OWNS]->(:Data) WITH n.id AS elementId, n.created AS created ORDER BY created DESC LIMIT 10 RETURN collect(elementId) as elementIds', []);
// $results = $this->elementHydrationStep->executeStep(null, [
// 'elementIds' => [
// '2d270f7b-0ced-4830-a1fe-9bbfdede8043'
// ]
// ]);

// $this->io->writeln(json_encode($results->getResults(), JSON_PRETTY_PRINT));
// $this->io->writeln(json_encode($results->getDebugData(), JSON_PRETTY_PRINT));
$this->io->writeln(json_encode($results, JSON_PRETTY_PRINT));

return Command::SUCCESS;
}
Expand Down
12 changes: 12 additions & 0 deletions src/Contract/SearchStepInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace App\Contract;

interface SearchStepInterface
{
public function isDangerous(): bool;

public function executeStep(string|array $query, array $parameters): SearchStepResultInterface;
}
16 changes: 16 additions & 0 deletions src/Contract/SearchStepResultInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace App\Contract;

interface SearchStepResultInterface
{
public function getResults(): array;

public function setResults(array $results): self;

public function getDebugData(): array;

public function setDebugData(array $debugData): self;
}
2 changes: 2 additions & 0 deletions src/Controller/Search/PostSearch2Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public function postSearch2(Request $request): Response
}
$result = $previousResult;

// if () {}

return new JsonResponse($result);
}
}
75 changes: 75 additions & 0 deletions src/Controller/Search/PostSearch3Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace App\Controller\Search;

use App\EventSystem\SearchQuery\Event\SearchQueryEvent;
use App\Factory\Exception\Client400BadContentExceptionFactory;
use App\Factory\Exception\Client400MissingPropertyExceptionFactory;
use App\Factory\Exception\Server500InternalServerErrorExceptionFactory;
use App\Response\JsonResponse;
use App\Security\AccessChecker;
use App\Security\AuthProvider;
use App\Type\SearchQueryType;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
class PostSearch3Controller extends AbstractController
{
public function __construct(
private AuthProvider $authProvider,
private AccessChecker $accessChecker,
private EventDispatcherInterface $eventDispatcher,
private Client400BadContentExceptionFactory $client400BadContentExceptionFactory,
private Client400MissingPropertyExceptionFactory $client400MissingPropertyExceptionFactory,
private Server500InternalServerErrorExceptionFactory $server500InternalServerErrorExceptionFactory
) {
}

#[Route(
'/search3',
name: 'post-search3',
methods: ['POST']
)]
public function postSearch3(Request $request): Response
{
$body = \Safe\json_decode($request->getContent(), true);

if (!array_key_exists('queries', $body)) {
throw $this->client400MissingPropertyExceptionFactory->createFromTemplate('queries', 'list with at least one query object');
}
$queries = $body['queries'];

$previousResult = [];
foreach ($queries as $query) {
if (!array_key_exists('type', $query)) {
throw $this->client400MissingPropertyExceptionFactory->createFromTemplate('queries[].type', 'valid query type');
}
$type = $query['type'];
if (!is_string($type)) {
throw $this->client400BadContentExceptionFactory->createFromTemplate('queries[].type', 'string', $type);
}
$type = SearchQueryType::from($type);
if (!array_key_exists('query', $query)) {
throw $this->client400MissingPropertyExceptionFactory->createFromTemplate('queries[].query', 'valid query');
}
$searchQuery = $query['query'];
$searchQueryEvent = new SearchQueryEvent($type, $searchQuery, $previousResult);
$this->eventDispatcher->dispatch($searchQueryEvent);
$previousResult = $searchQueryEvent->getResult();
}
$result = $previousResult;

// if () {}

return new JsonResponse($result);
}
}
13 changes: 13 additions & 0 deletions src/EventSystem/SearchQuery/Event/SearchQueryEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class SearchQueryEvent implements EventInterface
use StoppableEventTrait;

private array $result = [];
private int $numberOfResults = 0;

public function __construct(
private SearchQueryType $searchQueryType,
Expand All @@ -33,6 +34,18 @@ public function setResult(array $result): self
return $this;
}

public function getNumberOfResults(): int
{
return $this->numberOfResults;
}

public function setNumberOfResults(int $numberOfResults): self
{
$this->numberOfResults = $numberOfResults;

return $this;
}

public function getSearchQueryType(): SearchQueryType
{
return $this->searchQueryType;
Expand Down
Loading

0 comments on commit bafb8a5

Please sign in to comment.