Skip to content

Commit 496ec45

Browse files
Re-worked pagination
1 parent 7f9fed7 commit 496ec45

File tree

7 files changed

+61
-89
lines changed

7 files changed

+61
-89
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
* Dropped support for PHP 7.1
1212
* Updated to latest labels API
1313
* Made builder class final
14+
* Re-worked pagination
1415

1516
[11.0.0-RC1]: https://github.com/GitLabPHP/Client/compare/10.4.0...11.0.0-RC1
1617

phpstan-baseline.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ parameters:
66
path: src/Api/AbstractApi.php
77

88
-
9-
message: "#^Variable method call on Gitlab\\\\Api\\\\ApiInterface\\.$#"
9+
message: "#^Variable method call on Gitlab\\\\Api\\\\AbstractApi\\.$#"
1010
count: 1
1111
path: src/ResultPager.php
1212

psalm-baseline.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@
88
<code>\get_debug_type($data)</code>
99
</UndefinedFunction>
1010
</file>
11+
<file src="src/ResultPager.php">
12+
<InaccessibleProperty occurrences="1">
13+
<code>$clone-&gt;perPage</code>
14+
</InaccessibleProperty>
15+
<PossiblyInvalidFunctionCall occurrences="1">
16+
<code>$closure($api)</code>
17+
</PossiblyInvalidFunctionCall>
18+
</file>
1119
</files>

src/Api/AbstractApi.php

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@
2222
use Http\Message\MultipartStream\MultipartStreamBuilder;
2323
use Psr\Http\Message\StreamInterface;
2424
use Symfony\Component\OptionsResolver\OptionsResolver;
25-
use ValueError;
2625

2726
/**
2827
* @author Joseph Bielawski <[email protected]>
2928
* @author Matt Humphrey <[email protected]>
3029
* @author Radu Topala <[email protected]>
3130
*/
32-
abstract class AbstractApi implements ApiInterface
31+
abstract class AbstractApi
3332
{
3433
/**
3534
* The URI prefix.
@@ -55,41 +54,13 @@ abstract class AbstractApi implements ApiInterface
5554
/**
5655
* Create a new API instance.
5756
*
58-
* @param Client $client
59-
* @param int|null $perPage
57+
* @param Client $client
6058
*
6159
* @return void
6260
*/
63-
public function __construct(Client $client, int $perPage = null)
61+
public function __construct(Client $client)
6462
{
65-
if (null !== $perPage && ($perPage < 1 || $perPage > 100)) {
66-
throw new ValueError(\sprintf('%s::__construct(): Argument #2 ($perPage) must be between 1 and 100, or null', self::class));
67-
}
68-
6963
$this->client = $client;
70-
$this->perPage = $perPage;
71-
}
72-
73-
/**
74-
* Create a new instance with the given page parameter.
75-
*
76-
* This must be an integer between 1 and 100.
77-
*
78-
* @param int|null $perPage
79-
*
80-
* @return static
81-
*/
82-
public function perPage(?int $perPage)
83-
{
84-
if (null !== $perPage && ($perPage < 1 || $perPage > 100)) {
85-
throw new ValueError(\sprintf('%s::perPage(): Argument #1 ($perPage) must be between 1 and 100, or null', self::class));
86-
}
87-
88-
$copy = clone $this;
89-
90-
$copy->perPage = $perPage;
91-
92-
return $copy;
9364
}
9465

9566
/**

src/Api/ApiInterface.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/ResultPager.php

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
namespace Gitlab;
1616

17-
use Gitlab\Api\ApiInterface;
17+
use Closure;
18+
use Gitlab\Api\AbstractApi;
1819
use Gitlab\Exception\RuntimeException;
1920
use Gitlab\HttpClient\Message\ResponseMediator;
2021
use ValueError;
@@ -78,17 +79,17 @@ public function __construct(Client $client, int $perPage = null)
7879
/**
7980
* Fetch a single result from an api call.
8081
*
81-
* @param ApiInterface $api
82-
* @param string $method
83-
* @param array $parameters
82+
* @param AbstractApi $api
83+
* @param string $method
84+
* @param array $parameters
8485
*
8586
* @throws \Http\Client\Exception
8687
*
8788
* @return array
8889
*/
89-
public function fetch(ApiInterface $api, string $method, array $parameters = [])
90+
public function fetch(AbstractApi $api, string $method, array $parameters = [])
9091
{
91-
$result = $api->perPage($this->perPage)->$method(...$parameters);
92+
$result = self::bindPerPage($api, $this->perPage)->$method(...$parameters);
9293

9394
if (!\is_array($result)) {
9495
throw new RuntimeException('Pagination of this endpoint is not supported.');
@@ -102,31 +103,31 @@ public function fetch(ApiInterface $api, string $method, array $parameters = [])
102103
/**
103104
* Fetch all results from an api call.
104105
*
105-
* @param ApiInterface $api
106-
* @param string $method
107-
* @param array $parameters
106+
* @param AbstractApi $api
107+
* @param string $method
108+
* @param array $parameters
108109
*
109110
* @throws \Http\Client\Exception
110111
*
111112
* @return array
112113
*/
113-
public function fetchAll(ApiInterface $api, string $method, array $parameters = [])
114+
public function fetchAll(AbstractApi $api, string $method, array $parameters = [])
114115
{
115116
return \iterator_to_array($this->fetchAllLazy($api, $method, $parameters));
116117
}
117118

118119
/**
119120
* Lazily fetch all results from an api call.
120121
*
121-
* @param ApiInterface $api
122-
* @param string $method
123-
* @param array $parameters
122+
* @param AbstractApi $api
123+
* @param string $method
124+
* @param array $parameters
124125
*
125126
* @throws \Http\Client\Exception
126127
*
127128
* @return \Generator
128129
*/
129-
public function fetchAllLazy(ApiInterface $api, string $method, array $parameters = [])
130+
public function fetchAllLazy(AbstractApi $api, string $method, array $parameters = [])
130131
{
131132
/** @var mixed $value */
132133
foreach ($this->fetch($api, $method, $parameters) as $value) {
@@ -248,4 +249,24 @@ private function get(string $key)
248249

249250
return $content;
250251
}
252+
253+
/**
254+
* @param \Gitlab\Api\AbstractApi $api
255+
* @param int $perPage
256+
*
257+
* @return \Gitlab\Api\AbstractApi
258+
*/
259+
private static function bindPerPage(AbstractApi $api, int $perPage)
260+
{
261+
$closure = Closure::bind(static function (AbstractApi $api) use ($perPage): AbstractApi {
262+
$clone = clone $api;
263+
264+
$clone->perPage = $perPage;
265+
266+
return $clone;
267+
}, null, AbstractApi::class);
268+
269+
/** @var AbstractApi */
270+
return $closure($api);
271+
}
251272
}

src/ResultPagerInterface.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace Gitlab;
1616

17-
use Gitlab\Api\ApiInterface;
17+
use Gitlab\Api\AbstractApi;
1818

1919
/**
2020
* This is the result pager interface.
@@ -28,41 +28,41 @@ interface ResultPagerInterface
2828
/**
2929
* Fetch a single result from an api call.
3030
*
31-
* @param ApiInterface $api
32-
* @param string $method
33-
* @param array $parameters
31+
* @param AbstractApi $api
32+
* @param string $method
33+
* @param array $parameters
3434
*
3535
* @throws \Http\Client\Exception
3636
*
3737
* @return array
3838
*/
39-
public function fetch(ApiInterface $api, string $method, array $parameters = []);
39+
public function fetch(AbstractApi $api, string $method, array $parameters = []);
4040

4141
/**
4242
* Fetch all results from an api call.
4343
*
44-
* @param ApiInterface $api
45-
* @param string $method
46-
* @param array $parameters
44+
* @param AbstractApi $api
45+
* @param string $method
46+
* @param array $parameters
4747
*
4848
* @throws \Http\Client\Exception
4949
*
5050
* @return array
5151
*/
52-
public function fetchAll(ApiInterface $api, string $method, array $parameters = []);
52+
public function fetchAll(AbstractApi $api, string $method, array $parameters = []);
5353

5454
/**
5555
* Lazily fetch all results from an api call.
5656
*
57-
* @param ApiInterface $api
58-
* @param string $method
59-
* @param array $parameters
57+
* @param AbstractApi $api
58+
* @param string $method
59+
* @param array $parameters
6060
*
6161
* @throws \Http\Client\Exception
6262
*
6363
* @return \Generator
6464
*/
65-
public function fetchAllLazy(ApiInterface $api, string $method, array $parameters = []);
65+
public function fetchAllLazy(AbstractApi $api, string $method, array $parameters = []);
6666

6767
/**
6868
* Check to determine the availability of a next page.

0 commit comments

Comments
 (0)