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

add AbstractService method: getResponsePaginationLinks #59

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Object/PaginationLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Shopify\Object;

class PaginationLink
{
public $previous;

public $next;

public function __construct(string $linkHeader)
{
if (empty($linkHeader)) {
return;
}

foreach (explode(',', $linkHeader) as $item) {
if (preg_match('/<([\s\S]+?)>; rel=\"(next|previous)\"/', $item, $match)) {
$this->{$match[2]} = $match[1];
}
}
}
}
10 changes: 10 additions & 0 deletions src/Service/AbstractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Shopify\ApiInterface;
use Shopify\Object\PaginationLink;

abstract class AbstractService
{
Expand Down Expand Up @@ -106,4 +107,13 @@ function ($object) use ($className) {
}, $data
);
}

/** [fetch pagination link from Shopify Link headers]
* supported only in api version 2019-07 of the API and above
* @return PaginationLink
*/
public function getPaginationLink(): PaginationLink
{
return new PaginationLink($this->getLastResponse()->getHeaderLine('Link'));
}
}
41 changes: 41 additions & 0 deletions test/Service/PaginationLinkTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Shopify\Test\Service;

use Shopify\Object\PaginationLink;
use Shopify\Service\CustomerService;
use Shopify\Test\TestCase;

class PaginationLinkTest extends TestCase
{
public function testGetPaginationLink()
{
$previous = 'https://demo.myshopify.com/admin/api/2021-01/customers.json?limit=5&page_info=xyzprevious';
$next = 'https://demo.myshopify.com/admin/api/2021-01/customers.json?limit=5&page_info=xyznext';

// test previous && next
$api = $this->getApiMock(
['customers' => ['unit test']],
['Link' => '<' . $previous . '>; rel="previous", <' . $next . '>; rel="next"']
);

$service = new CustomerService($api);
$service->all();
$res = $service->getPaginationLink();
$this->assertInstanceOf(PaginationLink::class, $res);
$this->assertTrue($res->next == $next);
$this->assertTrue($res->previous == $previous);

// test only next
$api = $this->getApiMock(
['customers' => ['unit test']],
['Link' => '<' . $next . '>; rel="next"']
);

$service = new CustomerService($api);
$service->all();
$res = $service->getPaginationLink();
$this->assertInstanceOf(PaginationLink::class, $res);
$this->assertTrue($res->next == $next);
}
}
4 changes: 2 additions & 2 deletions test/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function getApi(array $params = array())
return $api;
}

public function getApiMock($file)
public function getApiMock($file, array $headers = ['X-Foo' => 'Bar'])
{
$json = null;
if (is_array($file)) {
Expand All @@ -33,7 +33,7 @@ public function getApiMock($file)
}
$json = file_get_contents($path);
}
$mock = new MockHandler([new Response(200, ['X-Foo' => 'Bar'], $json)]);
$mock = new MockHandler([new Response(200, $headers, $json)]);

$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
Expand Down