Skip to content

Commit

Permalink
Update execute() method of query classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed May 12, 2024
1 parent 915544b commit b6a8f1d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 47 deletions.
18 changes: 18 additions & 0 deletions src/Datasource/Query/CreateQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Muffin\Webservice\Datasource\Query;
use Muffin\Webservice\Datasource\QueryType;
use Muffin\Webservice\Model\Resource;

class CreateQuery extends Query
{
Expand All @@ -21,6 +22,23 @@ class CreateQuery extends Query
'set' => [],
];

/**
* Execute the query
*
* @return \Muffin\Webservice\Model\Resource|bool
*/
public function execute(): Resource|bool
{
$return = $this->_webservice->execute($this);

assert(
$return instanceof Resource || is_bool($return),
sprintf('CreateQuery execution must return a resource or a boolean, got `%s`', get_debug_type($return))
);

return $return;
}

/**
* Return a handy representation of the query
*
Expand Down
18 changes: 13 additions & 5 deletions src/Datasource/Query/DeleteQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ class DeleteQuery extends Query
protected QueryType $_type = QueryType::DELETE;

/**
* Parts being used to in the query
* Execute the query
*
* @var array
* @return int|bool
*/
protected array $_parts = [
'where' => [],
];
public function execute(): int|bool
{
$return = $this->_webservice->execute($this);

assert(
is_int($return) || is_bool($return),
sprintf('DeleteQuery execution must return an integer or a boolean, got `%s`', get_debug_type($return))
);

return $return;
}
}
60 changes: 21 additions & 39 deletions src/Datasource/Query/ReadQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Muffin\Webservice\Datasource\QueryType;
use Muffin\Webservice\Datasource\ResultSet;
use Muffin\Webservice\Model\Endpoint;
use Muffin\Webservice\Model\Resource;
use Traversable;

/**
Expand Down Expand Up @@ -82,9 +81,9 @@ class ReadQuery extends Query implements IteratorAggregate, JsonSerializable, Qu
/**
* The result from the webservice
*
* @var \Muffin\Webservice\Model\Resource|\Cake\Datasource\ResultSetInterface|int|bool|null
* @var \Cake\Datasource\ResultSetInterface|null
*/
protected Resource|ResultSetInterface|int|bool|null $_results = null;
protected ?ResultSetInterface $_results = null;

/**
* Instance of a endpoint object this query is bound to
Expand Down Expand Up @@ -158,30 +157,26 @@ public function aliasFields(array $fields, ?string $defaultAlias = null): array
*/
public function all(): ResultSetInterface
{
if (is_iterable($this->_results)) {
if (!$this->_results instanceof ResultSetInterface) {
$this->_results = $this->decorateResults($this->_results);
}

if ($this->_results !== null) {
return $this->_results;
}

/** @psalm-suppress InternalMethod Could not find a better way apart from implementing it as a custom class **/
$results = $this->_cache?->fetch($this);
if ($results === null) {
$res = $this->execute();
$results = $this->execute();

if (!is_iterable($res)) {
return $this->_results = new ResultSet([$res], 1);
if (is_bool($results)) {
$results = new ResultSet([], 0);
} else {
$results = $this->decorateResults($results);
}

$results = $this->decorateResults($res);
/** @psalm-suppress InternalMethod Could not find a better way apart from implementing it as a custom class **/
$this->_cache?->store($this, $results);
}
$this->_results = $results;

return $this->_results;
return $this->_results = $results;
}

/**
Expand Down Expand Up @@ -378,22 +373,7 @@ public function offset(?int $offset)
*/
public function count(): int
{
if ($this->_results === null) {
$this->execute();
}

if ($this->_results instanceof ResultSet) {
return (int)$this->_results->total();
}
if ($this->_results instanceof ResultSetInterface) {
return $this->_results->count();
}
if ($this->_results === null) {
return 0;
}

// There is a single integer or boolean value
return 1;
return $this->all()->count();
}

/**
Expand Down Expand Up @@ -538,22 +518,24 @@ public function triggerBeforeFind(): void
/**
* Execute the query
*
* @return \Muffin\Webservice\Model\Resource|\Cake\Datasource\ResultSetInterface|int|bool
* @return \Cake\Datasource\ResultSetInterface|bool
*/
public function execute(): bool|int|Resource|ResultSetInterface
public function execute(): ResultSetInterface|bool
{
$this->triggerBeforeFind();

if ($this->_results !== null) {
$decorator = $this->decoratorClass();
if (is_iterable($this->_results) && !($this->_results instanceof $decorator)) {
$this->_results = new $decorator($this->_results);
}

if ($this->_results) {
return $this->_results;
}

return $this->_results = $this->_webservice->execute($this);
$return = $this->_webservice->execute($this);

assert(
$return instanceof ResultSetInterface || is_bool($return),
sprintf('CreateQuery execution must return a ResultSet or a boolean, got `%s`', get_debug_type($return))
);

return $return;
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/Datasource/Query/UpdateQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Muffin\Webservice\Datasource\Query;
use Muffin\Webservice\Datasource\QueryType;
use Muffin\Webservice\Model\Resource;

class UpdateQuery extends Query
{
Expand All @@ -21,4 +22,24 @@ class UpdateQuery extends Query
'where' => [],
'set' => [],
];

/**
* Execute the query
*
* @return \Muffin\Webservice\Model\Resource|int|bool
*/
public function execute(): Resource|bool|int
{
$return = $this->_webservice->execute($this);

assert(
is_int($return) || is_bool($return) || $return instanceof Resource,
sprintf(
'UpdateQuery execution must return a resource, or an integer or a boolean, got `%s`',
get_debug_type($return)
)
);

return $return;
}
}
6 changes: 3 additions & 3 deletions tests/TestCase/Datasource/Query/ReadQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function testOrderBy()
], $this->query->clause('order'));
}

public function testExecuteTwice()
public function testAllTwice()
{
$mockWebservice = $this
->getMockBuilder('\TestApp\Webservice\StaticWebservice')
Expand Down Expand Up @@ -137,10 +137,10 @@ public function testExecuteTwice()
$this->query
->setWebservice($mockWebservice);

$this->query->execute();
$this->query->all();

// This webservice shouldn't be called a second time
$this->query->execute();
$this->query->all();
}

public function testDebugInfo()
Expand Down

0 comments on commit b6a8f1d

Please sign in to comment.