Skip to content

Commit

Permalink
RepoInterface::getResource*ById*() API changed
Browse files Browse the repository at this point in the history
The `?string $class` parameter replaced with `?SearchConfig $config` as
in other `getResourcesBy*()` methods. This also allows to specify amount
of metadata fetched by the `getResource*ById*()` calls
  • Loading branch information
zozlak committed Oct 1, 2024
1 parent b4d8d49 commit 5cfe0ae
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 52 deletions.
44 changes: 15 additions & 29 deletions src/acdhOeaw/arche/lib/Repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,64 +438,50 @@ public function map(iterable $iter, callable $func, int $concurrency = 1,
* matching the search, an error is thrown.
*
* @param array<string> $ids an array of identifiers (being strings)
* @param string $class|null an optional class of the resulting object representing the resource
* (to be used by extension libraries)
* @param ?SearchConfig $config
* @return RepoResource
* @throws NotFound
* @throws AmbiguousMatch
*/
public function getResourceByIds(array $ids, ?string $class = null): RepoResource {
return $this->getResourceByIdsAsync($ids, $class)->wait(true) ?? throw new RuntimeException('Promise returned null');
public function getResourceByIds(array $ids, ?SearchConfig $config = null): RepoResource {
return $this->getResourceByIdsAsync($ids, $config)->wait(true) ?? throw new RuntimeException('Promise returned null');
}

/**
* Asynchronous version of getResourceByIds()
*
* @param string $id
* @param string|null $class
* @param ?SearchConfig $config
* @return RepoResourcePromise
* @see getResourceByIds()
*/
public function getResourceByIdAsync(string $id, ?string $class = null): RepoResourcePromise {
return $this->getResourceByIdsAsync([$id], $class);
public function getResourceByIdAsync(string $id,
?SearchConfig $config = null): RepoResourcePromise {
return $this->getResourceByIdsAsync([$id], $config);
}

/**
* Asynchronous version of getResourceByIds()
*
* @param array<string> $ids
* @param string|null $class
* @param ?SearchConfig $config
* @return RepoResourcePromise
* @see getResourceByIds()
*/
public function getResourceByIdsAsync(array $ids, ?string $class = null): RepoResourcePromise {
$url = $this->baseUrl . 'search';
$headers = [
'Content-Type' => 'application/x-www-form-urlencoded',
];
public function getResourceByIdsAsync(array $ids,
?SearchConfig $config = null): RepoResourcePromise {
$placeholders = substr(str_repeat('?, ', count($ids)), 0, -2);
$query = "SELECT DISTINCT id FROM identifiers WHERE ids IN ($placeholders)";
$body = http_build_query([
'sql' => $query,
'sqlParam' => $ids,
]);
$req = new Request('post', $url, $headers, $body);
$promise = $this->sendRequestAsync($req);
$promise = $promise->then(function (ResponseInterface $resp) use ($class) {
$graph = new Dataset();
$graph->add(RdfIoUtil::parse($resp, new DF()));
$config ??= new SearchConfig();
$promise = $this->getGraphBySqlQueryAsync($query, $ids, $config);
$promise = $promise->then(function (Dataset $graph)use ($config) {
$matches = $graph->listSubjects(new QT(predicate: $this->schema->searchMatch))->getValues();
switch (count($matches)) {
case 0:
return new RejectedPromise(new NotFound());
case 1;
$class = $class ?? self::$resourceClass;
$res = new $class($matches[0], $this);
$graph->delete(new PT($this->schema->searchMatch));
$graph->delete(new PT($this->schema->searchOrder));
$graph->delete(new PT($this->schema->searchOrderValue));
$res->setGraph($graph);
return $res;
$res = $this->extractResourcesFromGraph($graph, $config);
return $res->current();
default:
return new RejectedPromise(new AmbiguousMatch("Many resources match the search: " . implode(', ', $matches)));
}
Expand Down
22 changes: 9 additions & 13 deletions src/acdhOeaw/arche/lib/RepoDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,29 +140,25 @@ public function getSmartSearch(): SmartSearch {
* matching the search, an error is thrown.
*
* @param array<string> $ids an array of identifiers (being strings)
* @param string|null $class an optional class of the resulting object representing the resource
* (to be used by extension libraries)
* @param ?SearchConfig $config
* @return RepoResourceDb
* @throws NotFound
* @throws AmbiguousMatch
*/
public function getResourceByIds(array $ids, ?string $class = null): RepoResourceDb {
public function getResourceByIds(array $ids, ?SearchConfig $config = null): RepoResourceDb {
$placeholders = substr(str_repeat('?, ', count($ids)), 0, -2);
$query = "SELECT DISTINCT id FROM identifiers WHERE ids IN ($placeholders)";
$query = $this->pdo->prepare($query);
$query->execute($ids);
$id = $query->fetchColumn();
if ($id === false) {
$config ??= new SearchConfig();
$generator = $this->getResourcesBySqlQuery($query, $ids, $config);
if (!$generator->valid()) {
throw new NotFound();
}
if (($id2 = $query->fetchColumn()) !== false) {
$res = $generator->current();
$generator->next();
if ($generator->valid()) {
throw new AmbiguousMatch("Both resource $id and $id2 match the search");

Check failure on line 159 in src/acdhOeaw/arche/lib/RepoDb.php

View workflow job for this annotation

GitHub Actions / phpstan

Undefined variable: $id

Check failure on line 159 in src/acdhOeaw/arche/lib/RepoDb.php

View workflow job for this annotation

GitHub Actions / phpstan

Undefined variable: $id2

Check failure on line 159 in src/acdhOeaw/arche/lib/RepoDb.php

View workflow job for this annotation

GitHub Actions / phpstan

Undefined variable: $id

Check failure on line 159 in src/acdhOeaw/arche/lib/RepoDb.php

View workflow job for this annotation

GitHub Actions / phpstan

Undefined variable: $id2
}
$url = $this->getBaseUrl() . $id;
$class = $class ?? self::$resourceClass;
$obj = new $class($url, $this);
/** @var RepoResourceDb $obj */
return $obj;
return $res;

Check failure on line 161 in src/acdhOeaw/arche/lib/RepoDb.php

View workflow job for this annotation

GitHub Actions / phpstan

Method acdhOeaw\arche\lib\RepoDb::getResourceByIds() should return acdhOeaw\arche\lib\RepoResourceDb but returns acdhOeaw\arche\lib\RepoResourceInterface.

Check failure on line 161 in src/acdhOeaw/arche/lib/RepoDb.php

View workflow job for this annotation

GitHub Actions / phpstan

Method acdhOeaw\arche\lib\RepoDb::getResourceByIds() should return acdhOeaw\arche\lib\RepoResourceDb but returns acdhOeaw\arche\lib\RepoResourceInterface.
}

/**
Expand Down
10 changes: 4 additions & 6 deletions src/acdhOeaw/arche/lib/RepoInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ public function getSchema(): Schema;
* Throws an error on failure.
*
* @param string $id
* @param string|null $class an optional class of the resulting object representing the resource
* (to be used by extension libraries)
* @param ?SearchConfig $config
* @return RepoResourceInterface
*/
public function getResourceById(string $id, ?string $class = null): RepoResourceInterface;
public function getResourceById(string $id, ?SearchConfig $config = null): RepoResourceInterface;

/**
* Tries to find a single repository resource matching provided identifiers.
Expand All @@ -72,11 +71,10 @@ public function getResourceById(string $id, ?string $class = null): RepoResource
* matching the search, an error is thrown.
*
* @param array<string> $ids an array of identifiers (being strings)
* @param string|null $class an optional class of the resulting object representing the resource
* (to be used by extension libraries)
* @param ?SearchConfig $config
* @return RepoResourceInterface
*/
public function getResourceByIds(array $ids, ?string $class = null): RepoResourceInterface;
public function getResourceByIds(array $ids, ?SearchConfig $config = null): RepoResourceInterface;

/**
* Returns repository resources matching a given SQL search query.
Expand Down
7 changes: 3 additions & 4 deletions src/acdhOeaw/arche/lib/RepoTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,11 @@ public function getHeaderName(string $purpose): string {
* Throws an error on failure.
*
* @param string $id
* @param string|null $class an optional class of the resulting object representing the resource
* (to be used by extension libraries)
* @param ?SearchConfig $config
* @return RepoResourceInterface
*/
public function getResourceById(string $id, ?string $class = null): RepoResourceInterface {
return $this->getResourceByIds([$id], $class);
public function getResourceById(string $id, ?SearchConfig $config = null): RepoResourceInterface {
return $this->getResourceByIds([$id], $config);
}

/**
Expand Down

0 comments on commit 5cfe0ae

Please sign in to comment.