Skip to content

Commit

Permalink
Update exception handling and dependencies
Browse files Browse the repository at this point in the history
The code has been updated to throw SqlFileNotReadableException instead of SqlFileNotFoundException. It also includes the addition of InjectorInterface as a dependency in QueryInterceptor.
  • Loading branch information
koriym committed Jun 13, 2024
1 parent 438538e commit 96340c2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
41 changes: 26 additions & 15 deletions src/QueryInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use BEAR\Resource\ResourceObject;
use Ray\Aop\MethodInterceptor;
use Ray\Aop\MethodInvocation;
use Ray\Aop\ReflectionMethod;
use Ray\Di\Exception\Unbound;
use Ray\Di\InjectorInterface;
use Ray\Query\Annotation\Query;
use Ray\Query\Exception\SqlFileNotFoundException;
use Ray\Query\Exception\SqlFileNotReadableException;

use function assert;
Expand All @@ -30,14 +30,19 @@ class QueryInterceptor implements MethodInterceptor
/** @var FileGetContentsInterface */
private $fileGetContents;

/** @var InjectorInterface */
private $injector;

public function __construct(
ExtendedPdoInterface $pdo,
SqlDir $sqlDir,
FileGetContentsInterface $fileGetContents
FileGetContentsInterface $fileGetContents,
InjectorInterface $injector
) {
$this->sqlDir = $sqlDir;
$this->pdo = $pdo;
$this->fileGetContents = $fileGetContents;
$this->injector = $injector;
}

/** @return ResourceObject|mixed */
Expand All @@ -50,7 +55,24 @@ public function invoke(MethodInvocation $invocation)
$namedArguments = (array) $invocation->getNamedArguments();
[$queryId, $params] = $query->templated ? $this->templated($query, $namedArguments) : [$query->id, $namedArguments];
assert(is_string($queryId));
$sql = $this->getsql($queryId, $method);
$filePath = sprintf('%s/%s.sql', $this->sqlDir->value, $queryId);
try {
$sql = ($this->fileGetContents)($filePath);
} catch (SqlFileNotReadableException $e) {
// For BC
// @codeCoverageIgnoreStart
try {
$sqlQuery = $this->injector->getInstance(RowListInterface::class, $queryId);
// @codeCoverageIgnoreEnd
assert($sqlQuery instanceof QueryInterface);
} catch (Unbound $e) {
throw new SqlFileNotReadableException($filePath);
}

/** @var array<string, mixed> $params */
return $this->getQueryResult($invocation, $sqlQuery, $params);
}

$sqlQuery = $query->type === 'row' ? new SqlQueryRow($this->pdo, $sql) : new SqlQueryRowList($this->pdo, $sql);

/** @var array<string, mixed> $params */
Expand Down Expand Up @@ -112,15 +134,4 @@ private function templated(Query $query, array $namedArguments): array

return [$queryId, $params + $namedArguments];
}

private function getsql(string $queryId, ReflectionMethod $method): string
{
$filePath = sprintf('%s/%s.sql', $this->sqlDir->value, $queryId);

try {
return ($this->fileGetContents)($filePath);
} catch (SqlFileNotReadableException $e) {
throw new SqlFileNotFoundException((string) $method, $queryId);
}
}
}
4 changes: 2 additions & 2 deletions tests/SqlQueryInterceptModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use PHPUnit\Framework\TestCase;
use Ray\Di\AbstractModule;
use Ray\Di\Injector;
use Ray\Query\Exception\SqlFileNotFoundException;
use Ray\Query\Exception\SqlFileNotReadableException;

class SqlQueryInterceptModuleTest extends TestCase
{
Expand Down Expand Up @@ -68,7 +68,7 @@ public function testResourceObject404(): void

public function testNoSqlFile(): void
{
$this->expectException(SqlFileNotFoundException::class);
$this->expectException(SqlFileNotReadableException::class);
$this->fakeRo->noSql();
}

Expand Down

0 comments on commit 96340c2

Please sign in to comment.