Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Discussion] Paginating data #2

Open
adamdyson opened this issue Sep 18, 2018 · 0 comments
Open

[Discussion] Paginating data #2

adamdyson opened this issue Sep 18, 2018 · 0 comments

Comments

@adamdyson
Copy link
Contributor

Hi Paul, we briefly talked on Twitter about pagination and whether this is something Atlas or even Atlas.Trasit could/should offer.

Having integrated Atlas into a Zend Expressive application, where paginating Entities was a requirement; I definitely feel there's scope-creep, as you like to call it. So I'm reluctant at this stage to begin developing something, encase a feature like this is too project specific.

In my case, having the ability to paginate data with an Atlas.x package wouldn't have saved me much time or eased development, because of how zend-expressive-hal handles generating resources which is tied directly to a Paginator instance. See Line 54.

I've posted the relevant code below encase you're interested. I figure having a use-case might help with any architectural considerations and planning, or if this is even something you'd want under the Atlas umbrella.

Happy to discuss further and assist with development.

<?php
declare(strict_types=1);

namespace App;

use Atlas\Orm;
use Atlas\Transit;
use Zend\Expressive\Authentication;
use Zend\Expressive\Hal\Metadata\MetadataMap;
use Zend\Expressive\Hal\Metadata\RouteBasedCollectionMetadata;
use Zend\Expressive\Hal\Metadata\RouteBasedResourceMetadata;
use Zend\Hydrator\ArraySerializable;

class ConfigProvider
{
    ...

   public function getHalConfig() : array
    {
        return [
            [
                '__class__' => RouteBasedResourceMetadata::class,
                'resource_class' => Domain\Entity\User\User::class,
                'route' => 'api.user.read',
                'extractor' => ArraySerializable::class,
            ],
            [
                '__class__' => RouteBasedCollectionMetadata::class,
                'collection_class' => Domain\Entity\User\UserCollectionPaginator::class,
                'collection_relation' => 'users',
                'pagination_param_type' => 'query',
                'pagination_param' => 'offset',
                'route' => 'api.user.browse',
            ]
        ];
    }
}
<?php
declare(strict_types=1);

namespace App\Handler\User;

use App\DataSource\User\UserRepository;
use App\Handler\Traits\RestDispatchTrait;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Expressive\Hal\HalResponseFactory;
use Zend\Expressive\Hal\ResourceGenerator;

class ReadUserHandler implements RequestHandlerInterface
{
    private $userRepository;

    use RestDispatchTrait;

    public function __construct(
        ResourceGenerator $resourceGenerator,
        HalResponseFactory $responseFactory,
        UserRepository $userRepository
    ) {
        $this->resourceGenerator = $resourceGenerator;
        $this->responseFactory = $responseFactory;
        $this->userRepository = $userRepository;
    }

    public function get(ServerRequestInterface $request) : ResponseInterface
    {
        $id = (int) $request->getAttribute('id', false);

        return $this->createResponse($request, $this->userRepository->fetchUserById($id));
    }
}
<?php
declare(strict_types=1);

namespace App\Handler\User;

use App\DataSource\User\UserRepository;
use App\Handler\Traits\RestDispatchTrait;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Expressive\Hal\HalResponseFactory;
use Zend\Expressive\Hal\ResourceGenerator;

class BrowseUserHandler implements RequestHandlerInterface
{
    private $userRepository;

    use RestDispatchTrait;

    public function __construct(
        ResourceGenerator $resourceGenerator,
        HalResponseFactory $responseFactory,
        UserRepository $userRepository
    ) {
        $this->resourceGenerator = $resourceGenerator;
        $this->responseFactory = $responseFactory;
        $this->userRepository = $userRepository;
    }

    public function get(ServerRequestInterface $request) : ResponseInterface
    {
        $offset = (int) $request->getQueryParams()['offset'] ?? 1;
        $limit = (int) $request->getQueryParams()['limit'] ?? 25;

        return $this->createResponse($request, $this->userRepository->browseUsers($offset, $limit));
    }
}
<?php
declare(strict_types=1);

namespace App\DataSource\User;

use App\Atlas\AtlasPaginatorAdapter;
use App\Domain\Entity\User\User;
use App\Domain\Entity\User\UserCollection;
use App\Domain\Entity\User\UserCollectionPaginator;
use Atlas\Transit\Transit;

class UserRepository
{
    protected $transit;

    public function __construct(Transit $transit)
    {
        $this->transit = $transit;
    }

    public function fetchUserById(int $id)
    {
        return $this->transit
            ->select(User::class)
            ->where('id = ', $id)
            ->fetchDomain();
    }

    public function browseUsers(int $offset = 1, int $limit = 25, array $whereEquals = [])
    {
        $select = $this->transit->select(UserCollection::class, $whereEquals);
        $adapter = new AtlasPaginatorAdapter($select);
        $paginator = new UserCollectionPaginator($adapter);
        $paginator->setCurrentPageNumber($offset);
        $paginator->setItemCountPerPage($limit);

        return $paginator;
    }
}
<?php
declare(strict_types=1);

namespace App\Atlas;

use Atlas\Transit\TransitSelect;
use Zend\Paginator\Adapter\AdapterInterface;

class AtlasPaginatorAdapter implements AdapterInterface
{
    protected $select;

    public function __construct(TransitSelect $select)
    {
        $this->select = $select;
    }

    public function getItems($offset, $limit)
    {
        $items = $this->select
            ->offset((int) $offset)
            ->limit((int) $limit)
            ->fetchDomain();

        return $items;
    }

    public function count()
    {
        return $this->select->fetchCount();
    }
}
<?php
declare(strict_types=1);

namespace App\Domain\Entity\User;

use Zend\Paginator\Paginator;

class UserCollectionPaginator extends Paginator
{

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant