Skip to content

Commit b9190bc

Browse files
committed
chore: Split the Connection::run() method into two new methods
1 parent 701529b commit b9190bc

1 file changed

Lines changed: 33 additions & 13 deletions

File tree

src/Service/Connection.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,45 @@ public function __construct(
2424
);
2525
}
2626

27-
public function run(r\Query $query, $deepToArray = false): r\Query
27+
private function throwOnQueryError(mixed $result): void
2828
{
29-
$query = $query->run($this->connection);
29+
if (
30+
$result instanceof \ArrayObject
31+
&& $result->offsetExists('errors')
32+
&& $result->offsetGet('errors') > 0
33+
) {
34+
throw new QueryException($result->offsetGet('first_error'));
35+
}
36+
}
37+
38+
public function getQueryResultAsArray(r\Query $query): array
39+
{
40+
$result = $query->run($this->connection);
41+
42+
$this->throwOnQueryError($query);
3043

31-
if (is_object($query) && get_class($query) == 'ArrayObject' && $query->offsetExists('errors') && $query->offsetGet('errors') > 0) {
32-
throw new QueryException($query->offsetGet('first_error'));
44+
if(null === $result) {
45+
return [];
3346
}
3447

35-
if($deepToArray && !is_null($query)) {
36-
if(is_array($query)) {
37-
$query = $this->deepToArray($query);
38-
} elseif(get_class($query) == 'ArrayObject') {
39-
$query = $this->deepToArray($query->getArrayCopy());
40-
} elseif(method_exists($query, 'toArray')) {
41-
$query = $this->deepToArray($query->toArray());
42-
}
48+
if(is_array($result)) {
49+
return $this->deepToArray($result);
50+
} elseif ($result instanceof \Traversable) {
51+
return $this->deepToArray($result->getArrayCopy());
52+
} elseif (method_exists($result, 'toArray')) {
53+
return $this->deepToArray($result->toArray());
4354
}
4455

45-
return $query;
56+
return (array) $result;
57+
}
58+
59+
public function getQueryResult(r\Query $query): r\Query
60+
{
61+
$result = $query->run($this->connection);
62+
63+
$this->throwOnQueryError($result);
64+
65+
return $result;
4666
}
4767

4868
private function deepToArray($value): mixed

0 commit comments

Comments
 (0)