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

[v3] Add PHPStan @ level 8, fix broken endpoints & add recently added endpoints #18

Merged
merged 8 commits into from
May 1, 2024
Merged
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
33 changes: 33 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: "🔍 Check PHPStan"

on:
push:
paths:
- '**.php'
- 'phpstan.neon'
- 'phpstan-baseline.neon'
- '.github/workflows/phpstan.yml'

jobs:
phpstan:
runs-on: ubuntu-latest
timeout-minutes: 5

steps:
- name: "📥 Checkout"
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}

- name: "🐘 Setup PHP"
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
extensions: dom, curl, libxml, mbstring, zip
coverage: xdebug

- name: "📦 Install Composer dependencies"
uses: ramsey/composer-install@v3

- name: Run PHPStan
run: ./vendor/bin/phpstan --error-format=github
20 changes: 13 additions & 7 deletions .github/workflows/ci.yml → .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
name: Build
name: "🧪 Run PHPUnit tests"

on:
push:
pull_request:

jobs:
tests:
runs-on: ubuntu-latest
Expand All @@ -11,18 +13,22 @@ jobs:
php: [ 7.2, 7.3, 7.4, 8.0, 8.1 ]
name: PHP - ${{ matrix.php }}
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup PHP
- name: "📥 Checkout code"
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}

- name: "🐘 Setup PHP"
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip
tools: composer:v2
coverage: none
- name: Install dependencies

- name: "📦 Install dependencies"
run: |
composer update --prefer-dist --no-interaction --no-progress
- name: Execute tests

- name: "⚙️ Execute tests"
if: ${{ matrix.php >= 7.1 }}
run: vendor/bin/phpunit --verbose
46 changes: 46 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
parameters:
ignoreErrors:
-
message: "#^Property Piggy\\\\Api\\\\Http\\\\BaseClient\\:\\:\\$contactIdentifiers \\(Piggy\\\\Api\\\\Resources\\\\OAuth\\\\Contacts\\\\ContactIdentifiersResource\\) does not accept Piggy\\\\Api\\\\Resources\\\\Register\\\\ContactIdentifiersResource\\.$#"
count: 1
path: src/RegisterClient.php

-
message: "#^Property Piggy\\\\Api\\\\Http\\\\BaseClient\\:\\:\\$contacts \\(Piggy\\\\Api\\\\Resources\\\\OAuth\\\\Contacts\\\\ContactsResource\\) does not accept Piggy\\\\Api\\\\Resources\\\\Register\\\\Contacts\\\\ContactsResource\\.$#"
count: 1
path: src/RegisterClient.php

-
message: "#^Property Piggy\\\\Api\\\\Http\\\\BaseClient\\:\\:\\$creditReceptions \\(Piggy\\\\Api\\\\Resources\\\\OAuth\\\\Loyalty\\\\Receptions\\\\CreditReceptionsResource\\) does not accept Piggy\\\\Api\\\\Resources\\\\Register\\\\Loyalty\\\\Receptions\\\\CreditReceptionsResource\\.$#"
count: 1
path: src/RegisterClient.php

-
message: "#^Property Piggy\\\\Api\\\\Http\\\\BaseClient\\:\\:\\$giftcardTransactions \\(Piggy\\\\Api\\\\Resources\\\\OAuth\\\\Giftcards\\\\GiftcardTransactionsResource\\) does not accept Piggy\\\\Api\\\\Resources\\\\Register\\\\Giftcards\\\\GiftcardTransactionsResource\\.$#"
count: 1
path: src/RegisterClient.php

-
message: "#^Property Piggy\\\\Api\\\\Http\\\\BaseClient\\:\\:\\$giftcards \\(Piggy\\\\Api\\\\Resources\\\\OAuth\\\\Giftcards\\\\GiftcardsResource\\) does not accept Piggy\\\\Api\\\\Resources\\\\Register\\\\Giftcards\\\\GiftcardsResource\\.$#"
count: 1
path: src/RegisterClient.php

-
message: "#^Property Piggy\\\\Api\\\\Http\\\\BaseClient\\:\\:\\$loyaltyProgram \\(Piggy\\\\Api\\\\Resources\\\\OAuth\\\\Loyalty\\\\Program\\\\LoyaltyProgramsResource\\) does not accept Piggy\\\\Api\\\\Resources\\\\Register\\\\Loyalty\\\\Program\\\\LoyaltyProgramResource\\.$#"
count: 1
path: src/RegisterClient.php

-
message: "#^Property Piggy\\\\Api\\\\Http\\\\BaseClient\\:\\:\\$prepaidTransactions \\(Piggy\\\\Api\\\\Resources\\\\OAuth\\\\PrepaidTransactionsResource\\) does not accept Piggy\\\\Api\\\\Resources\\\\Register\\\\PrepaidTransactionResource\\.$#"
count: 1
path: src/RegisterClient.php

-
message: "#^Property Piggy\\\\Api\\\\Http\\\\BaseClient\\:\\:\\$rewardReceptions \\(Piggy\\\\Api\\\\Resources\\\\OAuth\\\\Loyalty\\\\Receptions\\\\RewardReceptionsResource\\) does not accept Piggy\\\\Api\\\\Resources\\\\Register\\\\Loyalty\\\\Receptions\\\\RewardReceptionsResource\\.$#"
count: 1
path: src/RegisterClient.php

-
message: "#^Property Piggy\\\\Api\\\\Http\\\\BaseClient\\:\\:\\$rewards \\(Piggy\\\\Api\\\\Resources\\\\OAuth\\\\Loyalty\\\\Rewards\\\\RewardsResource\\) does not accept Piggy\\\\Api\\\\Resources\\\\Register\\\\Loyalty\\\\Rewards\\\\RewardsResource\\.$#"
count: 1
path: src/RegisterClient.php
15 changes: 15 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
includes:
- phpstan-baseline.neon

parameters:
level: 8

paths:
- src

ignoreErrors:
- message: '#^Argument of an invalid type stdClass supplied for foreach, only iterables are supported\.#'
- message: "#^Parameter \\#\\d+ \\$\\w+ of class .* constructor expects .*, array\\|bool\\|float\\|int\\|string\\|null given\\.$#"

excludePaths:
- src/StaticMappers/*
113 changes: 33 additions & 80 deletions src/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,153 +19,113 @@ class ApiClient
use OAuthResources;

/**
* @var
* @var GuzzleClient
*/
private static $httpClient;

/**
* @var string
*/
private static $baseUrl = "https://api.piggy.nl";
private static $baseUrl = 'https://api.piggy.nl';

/**
* @var array
* @var array<string, string>
*/
private static $headers = [
'Accept' => 'application/json',
];

/**
* @var string
*/
private $apiKey;

/**
* @param string $apiKey
* @param string $baseUrl
*/
public function __construct(string $apiKey, string $baseUrl)
{
self::setApiKey($apiKey);
self::setBaseUrl($baseUrl);
self::$httpClient = new GuzzleClient();
}

/**
* @param $apiKey
* @param $baseUrl
* @return void
*/
public static function configure($apiKey, $baseUrl)
public static function configure(string $apiKey, string $baseUrl): void
{
new self($apiKey, $baseUrl);
}

/**
* @param string $apiKey
* @return void
*/
public static function setApiKey(string $apiKey)
public static function setApiKey(string $apiKey): void
{
self::addHeader("Authorization", "Bearer $apiKey");
self::addHeader('Authorization', "Bearer $apiKey");
}

/**
* @param string $method
* @param string $endpoint
* @param array $queryOptions
* @return Response
* @param mixed[] $queryOptions
*
* @throws Exceptions\MaintenanceModeException
* @throws Exceptions\PiggyRequestException
* @throws GuzzleException
*/
public static function request(string $method, string $endpoint, array $queryOptions = []): Response
{
if (!array_key_exists('Authorization', self::$headers)) {
if (! array_key_exists('Authorization', self::$headers)) {
throw new Exception('Authorization not set yet.');
}

$url = self::$baseUrl . $endpoint;

$url = self::$baseUrl.$endpoint;

try {
$rawResponse = self::getResponse($method, $url, [
"headers" => self::$headers,
"form_params" => $queryOptions,
'headers' => self::$headers,
'form_params' => $queryOptions,
]);

return self::parseResponse($rawResponse);
} catch (Exception $e) {
$exceptionMapper = new ExceptionMapper();
$exception = $exceptionMapper->map($e);
throw $exception;

throw $exceptionMapper->map($e);
}
}

/**
* @param $response
* @return Response
* @throws MalformedResponseException
*/
private static function parseResponse($response): Response
private static function parseResponse(ResponseInterface $response): Response
{
try {
$content = json_decode($response->getBody()->getContents());
} catch (Throwable $exception) {
throw new MalformedResponseException("Could not decode response");
throw new MalformedResponseException('Could not decode response');
}

if (!property_exists($content, "data")) {
throw new MalformedResponseException("Invalid response given. Data property was missing from response.");
if (! property_exists($content, 'data')) {
throw new MalformedResponseException('Invalid response given. Data property was missing from response.');
}

// if (!property_exists($content, "meta")) {
// throw new MalformedResponseException("Invalid response given. Meta property was missing from response.");
// }

return new Response($content->data, $content->meta);
return new Response($content->data, $content->meta ?? []);
}

/**
* @return string
*/
public static function getBaseUrl(): string
{

return self::$baseUrl;
}

/**
* @param $baseUrl
* @return void
*/
public static function setBaseUrl($baseUrl): void
public static function setBaseUrl(string $baseUrl): void
{
self::$baseUrl = $baseUrl;
}

/**
* @param $key
* @param $value
* @return void
*/
public static function addHeader($key, $value): void
public static function addHeader(string $key, string $value): void
{
self::$headers[$key] = $value;
}

/**
* @return array
* @return string[]
*/
public static function getHeaders(): array
{
return self::$headers;
}

/**
* @param string $url
* @param array $body
* @return Response
* @param mixed[] $body
*
* @throws MaintenanceModeException
* @throws GuzzleException
* @throws PiggyRequestException
Expand All @@ -176,23 +136,20 @@ public static function post(string $url, array $body): Response
}

/**
* @param string $url
* @param array $body
* @return Response
* @param mixed[] $body
*
* @throws MaintenanceModeException
* @throws GuzzleException
* @throws PiggyRequestException
*
*/
public static function put(string $url, array $body): Response
{
return self::request('PUT', $url, $body);
}

/**
* @param string $url
* @param array $params
* @return Response
* @param mixed[] $params
*
* @throws MaintenanceModeException
* @throws GuzzleException
* @throws PiggyRequestException
Expand All @@ -209,9 +166,8 @@ public static function get(string $url, array $params = []): Response
}

/**
* @param string $url
* @param array $body
* @return Response
* @param mixed[] $body
*
* @throws MaintenanceModeException
* @throws GuzzleException
* @throws PiggyRequestException
Expand All @@ -228,15 +184,12 @@ public static function delete(string $url, array $body = []): Response
}

/**
* @param $method
* @param $url
* @param array $options
* @return ResponseInterface
* @param mixed[] $options
*
* @throws GuzzleException
*/
private static function getResponse($method, $url, array $options = []): ResponseInterface
private static function getResponse(string $method, string $url, array $options = []): ResponseInterface
{
return self::$httpClient->request($method, $url, $options);
}

}
Loading
Loading