Skip to content

Commit

Permalink
Making pagination more efficient.
Browse files Browse the repository at this point in the history
  • Loading branch information
joshforbes committed Jun 14, 2016
1 parent 0ee51e5 commit 87b17e9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
29 changes: 5 additions & 24 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace NavJobs\Transmit;

use Illuminate\Pagination\Paginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Input;
use NavJobs\Transmit\Traits\QueryHelperTrait;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Routing\Controller as BaseController;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;

Expand Down Expand Up @@ -118,15 +117,16 @@ protected function respondWithCollection($collection, $callback, $resourceKey =
* Returns a json response that contains the specified paginated collection
* passed through fractal and optionally a transformer.
*
* @param $collection
* @param $builder
* @param $callback
* @param int $perPage
* @param null $resourceKey
* @return \Illuminate\Http\JsonResponse
*/
protected function respondWithPaginatedCollection($collection, $callback, $perPage = 10, $resourceKey = null)
protected function respondWithPaginatedCollection(Builder $builder, $callback, $perPage = 10, $resourceKey = null)
{
$paginator = $this->paginateCollection($collection, $perPage);
$paginator = $builder->paginate($perPage);
$paginator->appends($this->getQueryParameters());

$rootScope = $this->fractal
->collection($paginator->getCollection(), $callback, $resourceKey)
Expand All @@ -135,25 +135,6 @@ protected function respondWithPaginatedCollection($collection, $callback, $perPa
return $this->respondWithArray($rootScope->toArray());
}

/**
* @param $collection
* @param $perPage
* @return LengthAwarePaginator
*/
protected function paginateCollection($collection, $perPage)
{
$paginator = new LengthAwarePaginator(
$collection->forPage(Paginator::resolveCurrentPage(), $perPage),
$collection->count(),
$perPage,
Paginator::resolveCurrentPage(),
['path' => Paginator::resolveCurrentPath()]
);
$paginator->appends($this->getQueryParameters());

return $paginator;
}

/**
* Returns an array of Query Parameters, not including pagination.
*
Expand Down
8 changes: 7 additions & 1 deletion tests/Integration/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace NavJobs\Transmit\Test\Integration;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Pagination\LengthAwarePaginator;
use Mockery;
use ReflectionClass;
use ReflectionMethod;

Expand Down Expand Up @@ -137,8 +140,11 @@ public function it_can_respond_with_a_paginated_collection()
{
$respondWithPaginatedCollection = $this->getMethod('respondWithPaginatedCollection');
$testController = new TestController();
$lengthAwarePaginator = new LengthAwarePaginator($this->testBooks, count($this->testBooks), 10);
$builder = Mockery::mock(Builder::class);
$builder->shouldReceive('paginate')->once()->andReturn($lengthAwarePaginator);

$response = $respondWithPaginatedCollection->invokeArgs($testController, [collect($this->testBooks), new TestTransformer()]);
$response = $respondWithPaginatedCollection->invokeArgs($testController, [$builder, new TestTransformer(), 10]);
$array = json_decode(json_encode($response->getData()), true);

$expectedArray = [
Expand Down

0 comments on commit 87b17e9

Please sign in to comment.