Skip to content

Commit

Permalink
Add countAllModels() to Data Providers
Browse files Browse the repository at this point in the history
  • Loading branch information
cundd committed Nov 14, 2018
1 parent 593e5fb commit 98b8cdb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 63 deletions.
10 changes: 8 additions & 2 deletions Classes/DataProvider/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Cundd\Rest\Domain\Model\ResourceType;
use Cundd\Rest\ObjectManagerInterface;
use Cundd\Rest\Persistence\Generic\RestQuerySettings;
use Cundd\Rest\SingletonInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use TYPO3\CMS\Core\Log\LogManager;
Expand All @@ -16,9 +17,9 @@
use TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationBuilder;

/**
* DataProvider instance
* Data Provider implementation for Extbase based Models
*/
class DataProvider implements DataProviderInterface, ClassLoadingInterface
class DataProvider implements DataProviderInterface, ClassLoadingInterface, SingletonInterface
{
/**
* @var \Cundd\Rest\ObjectManagerInterface
Expand Down Expand Up @@ -121,6 +122,11 @@ public function fetchAllModels(ResourceType $resourceType)
return $this->getRepositoryForResourceType($resourceType)->findAll();
}

public function countAllModels(ResourceType $resourceType)
{
return $this->getRepositoryForResourceType($resourceType)->countAll();
}

public function fetchModel($identifier, ResourceType $resourceType)
{
if ($identifier && is_scalar($identifier)) { // If it is a scalar treat it as identity
Expand Down
11 changes: 9 additions & 2 deletions Classes/DataProvider/DataProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
namespace Cundd\Rest\DataProvider;

use Cundd\Rest\Domain\Model\ResourceType;
use Cundd\Rest\SingletonInterface;
use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;

interface DataProviderInterface extends SingletonInterface
interface DataProviderInterface
{
/**
* Return all Domain Models for the given API resource type
Expand All @@ -17,6 +16,14 @@ interface DataProviderInterface extends SingletonInterface
*/
public function fetchAllModels(ResourceType $resourceType);

/**
* Return the number of all Domain Models for the given API resource type
*
* @param ResourceType $resourceType API resource type to get the repository for
* @return int
*/
public function countAllModels(ResourceType $resourceType);

/**
* Return a Domain Model for the given API resource type and data
*
Expand Down
51 changes: 1 addition & 50 deletions Classes/Handler/CrudHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Cundd\Rest\Handler;

use Countable;
use Cundd\Rest\DataProvider\DataProviderInterface;
use Cundd\Rest\DataProvider\Utility;
use Cundd\Rest\Http\RestRequestInterface;
Expand All @@ -18,22 +17,6 @@
*/
class CrudHandler implements CrudHandlerInterface, HandlerDescriptionInterface
{
/**
* Current request
*
* @var RestRequestInterface
* @deprecated will be removed in 4.0.0
*/
protected $request;

/**
* Unique identifier of the currently matching Domain Model
*
* @var string
* @deprecated will be removed in 4.0.0
*/
protected $identifier;

/**
* Object Manager
*
Expand Down Expand Up @@ -68,11 +51,6 @@ public function __construct(
$this->logger = $logger;
}

/**
* Return the description of the handler
*
* @return string
*/
public function getDescription()
{
return 'Default Handler for CRUD requests';
Expand Down Expand Up @@ -165,12 +143,6 @@ public function delete(RestRequestInterface $request, $identifier)
return $this->responseFactory->createSuccessResponse('Deleted', 200, $request);
}

/**
* List all Models
*
* @param RestRequestInterface $request
* @return array Returns all Models
*/
public function listAll(RestRequestInterface $request)
{
$dataProvider = $this->getDataProvider();
Expand All @@ -183,24 +155,9 @@ public function listAll(RestRequestInterface $request)
return $this->prepareResult($request, array_map([$dataProvider, 'getModelData'], $allModels), false);
}

/**
* Count all Models
*
* @param RestRequestInterface $request
* @return int Returns the number of models
*/
public function countAll(RestRequestInterface $request)
{
$allModels = $this->getDataProvider()->fetchAllModels($request->getResourceType());

if (is_array($allModels) || $allModels instanceof Countable) {
return count($allModels);
}
if ($allModels instanceof Traversable) {
return count(iterator_to_array($allModels));
}

return NAN;
return $this->getDataProvider()->countAllModels($request->getResourceType());
}

/**
Expand All @@ -212,12 +169,6 @@ public function options()
return true;
}

/**
* Let the handler configure the routes
*
* @param RouterInterface $router
* @param RestRequestInterface $request
*/
public function configureRoutes(RouterInterface $router, RestRequestInterface $request)
{
$resourceType = $request->getResourceType();
Expand Down
17 changes: 8 additions & 9 deletions Classes/Handler/CrudHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@ public function show(RestRequestInterface $request, $identifier);
*/
public function update(RestRequestInterface $request, $identifier);

/**
* Update the requested Model with the data from the request
*
* @param RestRequestInterface $request
* @param int|string $identifier
* @return array|int|ResponseInterface Returns the Model's data on success, otherwise a descriptive error code
*/
//public function update(RestRequestInterface $request, $identifier);

/**
* Delete the requested Model
*
Expand All @@ -71,4 +62,12 @@ public function create(RestRequestInterface $request);
* @return array Returns all Models
*/
public function listAll(RestRequestInterface $request);

/**
* Count all Models
*
* @param RestRequestInterface $request
* @return int Returns the number of models
*/
public function countAll(RestRequestInterface $request);
}

0 comments on commit 98b8cdb

Please sign in to comment.