Skip to content

Commit

Permalink
Add file existence check and exception handling
Browse files Browse the repository at this point in the history
Imported the file_exists function in FileGetContents.php and implemented a check for file existence before getting its content. In QueryInterceptor.php, added InjectorInterface for dependency injection and handled SqlFileNotReadableException to maintain backward compatibility.
  • Loading branch information
koriym committed Jun 12, 2024
1 parent 8252364 commit e9b0797
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/FileGetContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Ray\Query\Exception\SqlFileNotReadableException;

use function file_exists;
use function file_get_contents;

final class FileGetContents implements FileGetContentsInterface
Expand All @@ -15,6 +16,10 @@ final class FileGetContents implements FileGetContentsInterface
*/
public function __invoke(string $filePath): string
{
if (! file_exists($filePath)) {
throw new SqlFileNotReadableException($filePath);
}

$content = file_get_contents($filePath);
if ($content === false) {
// @codeCoverageIgnoreStart
Expand Down
21 changes: 19 additions & 2 deletions src/QueryInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Ray\Aop\MethodInterceptor;
use Ray\Aop\MethodInvocation;
use Ray\Aop\ReflectionMethod;
use Ray\Di\InjectorInterface;
use Ray\Query\Annotation\Query;
use Ray\Query\Exception\SqlFileNotFoundException;
use Ray\Query\Exception\SqlFileNotReadableException;
Expand All @@ -30,14 +31,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 +56,18 @@ 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
$sqlQuery = $this->injector->getInstance(RowListInterface::class, $queryId);

return $this->getQueryResult($invocation, $sqlQuery, $params);

Check failure on line 67 in src/QueryInterceptor.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

Parameter #3 $param of method Ray\Query\QueryInterceptor::getQueryResult() expects array<string, mixed>, mixed given.

Check failure on line 67 in src/QueryInterceptor.php

View workflow job for this annotation

GitHub Actions / sa / Psalm

MixedArgument: Argument 3 of Ray\Query\QueryInterceptor::getQueryResult cannot be mixed, expecting array<string, mixed>

Check failure on line 67 in src/QueryInterceptor.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

Parameter #3 $param of method Ray\Query\QueryInterceptor::getQueryResult() expects array<string, mixed>, mixed given.

Check failure on line 67 in src/QueryInterceptor.php

View workflow job for this annotation

GitHub Actions / sa / Psalm

MixedArgument: Argument 3 of Ray\Query\QueryInterceptor::getQueryResult cannot be mixed, expecting array<string, mixed>
// @codeCoverageIgnoreEnd
}

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

/** @var array<string, mixed> $params */
Expand Down

0 comments on commit e9b0797

Please sign in to comment.