Skip to content

Commit

Permalink
Merge pull request #198 from netglue/175-introduce-a-constant-for-the…
Browse files Browse the repository at this point in the history
…-absolute-max-result-set-size

Introduce a constant for the max result set size
  • Loading branch information
gsteel committed Jul 11, 2022
2 parents 98b37ba + 3e3a01c commit e8692a7
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 43 deletions.
11 changes: 1 addition & 10 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.17.0@6f4707aa41c9174353a6434bba3fc8840f981d9c">
<file src="src/Api.php">
<MixedInferredReturnType occurrences="1">
<code>object</code>
</MixedInferredReturnType>
<MixedReturnStatement occurrences="1">
<code>$locator()</code>
</MixedReturnStatement>
</file>
</files>
<files psalm-version="4.24.0@06dd975cb55d36af80f242561738f16c5f58264f"/>
3 changes: 3 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
memoizeMethodCallResults="true"
findUnusedPsalmSuppress="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" errorBaseline="psalm-baseline.xml">
Expand Down
81 changes: 51 additions & 30 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Prismic\Document\Fragment\DocumentLink;
use Prismic\Exception\InvalidArgument;
use Prismic\Exception\InvalidPreviewToken;
use Prismic\Exception\JsonError;
use Prismic\Exception\PrismicError;
Expand Down Expand Up @@ -121,44 +122,64 @@ public static function get(
?ResultSetFactory $resultSetFactory = null,
?CacheItemPoolInterface $cache = null
): self {
/**
* @return ClientInterface|RequestFactoryInterface|UriFactoryInterface
*/
$factory = static function (?object $given, callable $locator, string $message): object {
if ($given) {
return $given;
}

/** @psalm-suppress InvalidCatch */
try {
return $locator();
} catch (DiscoveryError $error) {
throw new RuntimeError(
$message,
(int) $error->getCode(),
$error
);
}
};

/** @psalm-suppress ArgumentTypeCoercion */
/** @psalm-suppress PossiblyInvalidArgument */
return new self(
$apiBaseUri,
$factory($httpClient, static function (): ClientInterface {
return Psr18ClientDiscovery::find();
}, 'An HTTP client cannot be determined.'),
$httpClient ?? self::psrFactory(
ClientInterface::class,
'An HTTP client cannot be determined.'
),
(string) $accessToken === '' ? null : $accessToken,
$factory($requestFactory, static function (): RequestFactoryInterface {
return Psr17FactoryDiscovery::findRequestFactory();
}, 'A request factory cannot be determined'),
$factory($uriFactory, static function (): UriFactoryInterface {
return Psr17FactoryDiscovery::findUriFactory();
}, 'A URI factory cannot be determined'),
$requestFactory ?? self::psrFactory(
RequestFactoryInterface::class,
'A request factory cannot be determined'
),
$uriFactory ?? self::psrFactory(
UriFactoryInterface::class,
'A URI factory cannot be determined'
),
$resultSetFactory ?? new StandardResultSetFactory(),
$cache
);
}

/**
* phpcs:disable Generic.Files.LineLength.TooLong, Squiz.Commenting.FunctionComment.SpacingAfterParamType
* @param T|null $instance
* @param class-string<ClientInterface>|class-string<RequestFactoryInterface>|class-string<UriFactoryInterface> $type
*
* @return ClientInterface|RequestFactoryInterface|UriFactoryInterface
*
* @template T of object
*/
private static function psrFactory(string $type, string $message)
{
try {
switch ($type) {
case ClientInterface::class:
return Psr18ClientDiscovery::find();

case RequestFactoryInterface::class:
return Psr17FactoryDiscovery::findRequestFactory();

case UriFactoryInterface::class:
return Psr17FactoryDiscovery::findUriFactory();

default:
throw new InvalidArgument(sprintf(
'Invalid class-string: "%s"',
$type
));
}
} catch (DiscoveryError $error) {
throw new RuntimeError(
$message,
(int) $error->getCode(),
$error
);
}
}

public function host(): string
{
return $this->baseUri->getHost();
Expand Down
5 changes: 5 additions & 0 deletions src/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ interface ApiClient
*/
public const EXPERIMENTS_COOKIE = 'io.prismic.experiment';

/**
* The maximum page size of result sets returned by the API
*/
public const MAX_PAGE_SIZE = 100;

/** Return the host name of the api endpoint */
public function host(): string;

Expand Down
1 change: 0 additions & 1 deletion src/Document/Fragment/Embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ private function setAttributes(iterable $attributes): void
private function setAttribute(string $name, $value): void
{
/**
* @psalm-suppress RedundantConditionGivenDocblockType
* @psalm-suppress DocblockTypeContradiction
*/
if ($value !== null && ! is_scalar($value)) {
Expand Down
5 changes: 5 additions & 0 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

/**
* @psalm-type QueryParams = array<string, string|int|list<string>|null>
* @final
*/
class Query
{
Expand Down Expand Up @@ -145,6 +146,8 @@ private function defaultParameters(): array
* Limit document count per page.
*
* The default is 20 per page and the maximum is 100
*
* @param positive-int $count
*/
public function resultsPerPage(int $count): self
{
Expand All @@ -153,6 +156,8 @@ public function resultsPerPage(int $count): self

/**
* Set the result page to retrieve.
*
* @param positive-int $page
*/
public function page(int $page): self
{
Expand Down
17 changes: 16 additions & 1 deletion test/Smoke/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
use Psr\Http\Client\ClientInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

use function assert;
use function file_exists;
use function getenv;
use function is_array;
use function is_string;

class TestCase extends PHPUnitTestCase
{
Expand Down Expand Up @@ -65,11 +68,23 @@ protected function compileEndPoints(): array

/** @psalm-suppress MissingFile $content */
$content = require $configPath;
if (! is_array($content)) {
return $endpoints;
}

$configured = $content['endpoints'] ?? [];
assert(is_array($configured));

foreach ($configured as $spec) {
$endpoints[$spec['url']] = $spec['token'];
assert(is_array($spec));
$url = isset($spec['url']) && is_string($spec['url']) ? $spec['url'] : null;
$token = isset($spec['token']) && is_string($spec['token']) ? $spec['token'] : null;

if (! $url) {
continue;
}

$endpoints[$url] = $token;
}

return $endpoints;
Expand Down
12 changes: 11 additions & 1 deletion test/Unit/ResultSet/StandardResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace PrismicTest\ResultSet;

use DateTimeImmutable;
use DateTimeZone;
use Prismic\Document;
use Prismic\Json;
use Prismic\ResultSet\StandardResultSet;
Expand All @@ -27,13 +29,21 @@ public function testBasicAccessors(): void
$this->assertSame(1, $this->resultSet->currentPageNumber());
$this->assertSame(99, $this->resultSet->totalResults());
$this->assertSame(99, $this->resultSet->pageCount());
$this->assertNotNull($this->resultSet->expiresAt());
$this->assertSame('https://example.com/next', $this->resultSet->nextPage());
$this->assertSame('https://example.com/prev', $this->resultSet->previousPage());
$this->assertCount(2, $this->resultSet->results());
$this->assertContainsOnlyInstancesOf(DocumentData::class, $this->resultSet->results());
}

public function testThatTheCacheExpiryDateWillBeTheCurrentTimeInUTC(): void
{
$now = new DateTimeImmutable('now', new DateTimeZone('UTC'));
self::assertInstanceOf(DateTimeImmutable::class, $now);

self::assertEqualsWithDelta($now, $this->resultSet->expiresAt(), 0.0001);
self::assertEquals('UTC', $this->resultSet->expiresAt()->getTimezone()->getName());
}

public function testThatResultSetsAreCountable(): void
{
$this->assertCount(2, $this->resultSet);
Expand Down

0 comments on commit e8692a7

Please sign in to comment.