Skip to content

Commit

Permalink
[1.x] Improve client factory code quality (#200)
Browse files Browse the repository at this point in the history
* chore(ci): add psalm helper scripts

* chore: improve client factory code quality

* chore: update pr template

* chore(ci): add psalm php unit plugin

* chore(client): deprecate $sandbox parameter

* Update UPGRADE-2.0.md
  • Loading branch information
imdhemy authored Jul 1, 2024
1 parent c82aa2a commit 2a36f74
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 119 deletions.
21 changes: 5 additions & 16 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
| Q | A |
|---------------|----------------------------------------------------------|
| Branch? | 1.x (current) or main (for breaking changes) |
| Deprecations? | yes/no <!-- please update UPGRADE-2.md |
| Tickets | Fix #... <!-- prefix each issue number with "Fix #", --> |
| License | MIT |
| Q | A |
|---------|-------|
| Tickets | Fix # |
| License | MIT |

<!--
Replace this notice with a short README for your feature/bugfix.
This will help reviewers and should be a good start for the documentation.
For bug fixes and new features, that does not break BC, please base your PR
on the `release/y.x` branch. For BC breaks, please base your PR on the
`main` branch.
Always add tests and ensure they pass.
-->
<!-- Describe your changes below in as much detail as possible -->
8 changes: 6 additions & 2 deletions UPGRADE-2.0.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Upgrade from 1.0 to 2.0

## Client Factory

- The `$sandbox` parameter in the `ClientFactory::create()` method is removed, use the `ClientFactory::createSandbox()`
method instead.

## Value objects

- [DEPRECATED] use `\Imdhemy\AppStore\ValueObjects\Time::toCarbon` instead
of `\Imdhemy\AppStore\ValueObjects\Time::getCarbon`
- Use `\Imdhemy\AppStore\ValueObjects\Time::toCarbon` instead of `\Imdhemy\AppStore\ValueObjects\Time::getCarbon`
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"fakerphp/faker": "^1.22",
"friendsofphp/php-cs-fixer": "^3.16",
"phpunit/phpunit": "^9.6",
"psalm/plugin-phpunit": "^0.19.0",
"roave/security-advisories": "dev-latest",
"vimeo/psalm": "^5.11"
},
Expand All @@ -36,13 +37,13 @@
}
},
"scripts": {
"psalm-info": "vendor/bin/psalm --show-info=true",
"test": "vendor/bin/phpunit --testdox",
"keygen": "sh ./bin/keygen.sh",
"security-advice": "composer update --dry-run roave/security-advisories",
"cs-check": "vendor/bin/php-cs-fixer fix --dry-run --diff --verbose",
"cs-fix": "php-cs-fixer fix --verbose",
"psalm": "psalm --no-cache",
"psalm-f": "psalm --ignore-baseline --output-format=compact",
"psalm-u": "psalm --update-baseline",
"keygen": "sh ./bin/keygen.sh",
"post-autoload-dump": [
"@keygen"
]
Expand Down
135 changes: 134 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.25.0@01a8eb06b9e9cc6cfb6a320bf9fb14331919d505">
<file src="src/ClientFactory.php">
<MixedArgumentTypeCoercion>
<code><![CDATA[$responseQueue]]></code>
</MixedArgumentTypeCoercion>
</file>
<file src="src/Jws/AppStoreJwsVerifier.php">
<ArgumentTypeCoercion>
<code><![CDATA[$exportedCertificate]]></code>
Expand Down
6 changes: 4 additions & 2 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
errorBaseline="psalm-baseline.xml"
>
<projectFiles>
<directory name="src" />
<directory name="src"/>
<ignoreFiles>
<directory name="vendor" />
<directory name="vendor"/>
</ignoreFiles>
</projectFiles>
<plugins>
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/></plugins>
</psalm>
59 changes: 19 additions & 40 deletions src/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Imdhemy\AppStore;

use ArrayAccess;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Handler\MockHandler;
Expand All @@ -10,90 +11,68 @@
use Psr\Http\Client\RequestExceptionInterface;
use Psr\Http\Message\ResponseInterface;

/**
* Class ClientFactory
*/
class ClientFactory
{
public const BASE_URI = 'https://buy.itunes.apple.com';
public const BASE_URI_SANDBOX = 'https://sandbox.itunes.apple.com';

public const STORE_KIT_PRODUCTION_URI = 'https://api.storekit.itunes.apple.com';
public const STORE_KIT_SANDBOX_URI = 'https://api.storekit-sandbox.itunes.apple.com';

/**
* @param bool $sandbox
* @param array $options
*
* @return ClientInterface
*/
public static function create(bool $sandbox = false, array $options = []): ClientInterface
{
if (empty($options['base_uri'])) {
$options['base_uri'] = $sandbox ? self::BASE_URI_SANDBOX : self::BASE_URI;
if ($sandbox) {
trigger_error('The $sandbox parameter is deprecated and will be removed in the next major version. Use createSandbox instead.', E_USER_DEPRECATED);
}

$options = array_merge(['base_uri' => $sandbox ? self::BASE_URI_SANDBOX : self::BASE_URI], $options);

return new Client($options);
}

/**
* @return ClientInterface
*/
public static function createSandbox(): ClientInterface
public static function createSandbox(array $options = []): ClientInterface
{
return self::create(true);
$options = array_merge(['base_uri' => self::BASE_URI_SANDBOX], $options);

return new Client($options);
}

/**
* Creates a client that returns the specified response
*
* @param ResponseInterface $responseMock
* @param array $transactions
*
* @psalm-suppress ReferenceConstraintViolation
* @return ClientInterface
* @param array|ArrayAccess<int, array> $container Container to hold the history (by reference).
*/
public static function mock(ResponseInterface $responseMock, array &$transactions = []): ClientInterface
public static function mock(ResponseInterface $responseMock, array|ArrayAccess &$container = []): ClientInterface
{
$mockHandler = new MockHandler([$responseMock]);
$handlerStack = HandlerStack::create($mockHandler);
$handlerStack->push(Middleware::history($transactions));
$handlerStack->push(Middleware::history($container));

return new Client(['handler' => $handlerStack]);
}

/**
* Creates a client that returns the specified array of responses in queue order
*
* @param array|ResponseInterface[]|RequestExceptionInterface[] $responseQueue
* @param array $transactions
*
* @psalm-suppress ReferenceConstraintViolation
* @return ClientInterface
* @param array<int, ResponseInterface|RequestExceptionInterface> $responseQueue
* @param array|ArrayAccess<int, array> $container Container to hold the history (by reference).
*/
public static function mockQueue(array $responseQueue, array &$transactions = []): ClientInterface
public static function mockQueue(array $responseQueue, array|ArrayAccess &$container = []): ClientInterface
{
$mockHandler = new MockHandler($responseQueue);
$handlerStack = HandlerStack::create($mockHandler);
$handlerStack->push(Middleware::history($transactions));
$handlerStack->push(Middleware::history($container));

return new Client(['handler' => $handlerStack]);
}

/**
* Creates a client that returns the specified request exception
*
* @param RequestExceptionInterface $error
* @param array $transactions
*
* @psalm-suppress ReferenceConstraintViolation
* @return ClientInterface
* @param array|ArrayAccess<int, array> $container Container to hold the history (by reference).
*/
public static function mockError(RequestExceptionInterface $error, array &$transactions = []): ClientInterface
public static function mockError(RequestExceptionInterface $error, array|ArrayAccess &$container = []): ClientInterface
{
$mockHandler = new MockHandler([$error]);
$handlerStack = HandlerStack::create($mockHandler);
$handlerStack->push(Middleware::history($transactions));
$handlerStack->push(Middleware::history($container));

return new Client(['handler' => $handlerStack]);
}
Expand Down
Loading

0 comments on commit 2a36f74

Please sign in to comment.