Skip to content
Draft
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
35 changes: 35 additions & 0 deletions .phpstan/baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ parameters:
count: 1
path: ../src/MobilePay/AppPayment/Payments/GetPaymentsResponseTest.php

-
message: "#^Parameter \\#1 \\$payload of class MobilePayPhp\\\\MobilePay\\\\AppPayment\\\\Refunds\\\\CreateRefundResponse constructor expects array\\<string, int\\|string\\>, array given\\.$#"
count: 1
path: ../src/MobilePay/AppPayment/Refunds/CreateRefundResponseTest.php

-
message: "#^Parameter \\#1 \\$payload of class MobilePayPhp\\\\MobilePay\\\\AppPayment\\\\Refunds\\\\GetRefundResponse constructor expects array\\<string, int\\|string\\>, array given\\.$#"
count: 1
path: ../src/MobilePay/AppPayment/Refunds/GetRefundResponseTest.php

-
message: "#^Parameter \\#2 \\$array of function array_map expects array, array\\|int\\|string given\\.$#"
count: 1
Expand All @@ -34,3 +44,28 @@ parameters:
message: "#^Parameter \\#1 \\$payload of class MobilePayPhp\\\\MobilePay\\\\AppPayment\\\\Refunds\\\\GetRefundsResponse constructor expects array\\<string, int\\|string\\>, array\\<string, array\\<int, array\\<string, int\\|string\\>\\>\\|int\\> given\\.$#"
count: 1
path: ../src/MobilePay/AppPayment/Refunds/GetRefundsResponseTest.php

-
message: "#^Parameter \\#1 \\$payload of class MobilePayPhp\\\\MobilePay\\\\Webhook\\\\Response\\\\SingleWebhook constructor expects array\\{events\\: array, signatureKey\\: string, url\\: string, webhookId\\: string, paymentPointId\\?\\: string\\}, array given\\.$#"
count: 1
path: ../src/MobilePay/Webhook/Response/SingleWebhook.php

-
message: "#^Parameter \\#1 \\$callback of function array_map expects \\(callable\\(array\\|string\\)\\: mixed\\)\\|null, Closure\\(array\\)\\: MobilePayPhp\\\\MobilePay\\\\Webhook\\\\Response\\\\SingleWebhook given\\.$#"
count: 1
path: ../src/MobilePay/Webhook/Response/WebhookCollection.php

-
message: "#^Parameter \\#1 \\$payload of class MobilePayPhp\\\\MobilePay\\\\Webhook\\\\Response\\\\SingleWebhook constructor expects array\\{events\\: array, signatureKey\\: string, url\\: string, webhookId\\: string, paymentPointId\\?\\: string\\}, array given\\.$#"
count: 1
path: ../src/MobilePay/Webhook/Response/WebhookCollection.php

-
message: "#^Parameter \\#1 \\$payload of class MobilePayPhp\\\\MobilePay\\\\Webhook\\\\Response\\\\WebhookCollection constructor expects array\\{webhooks\\: array\\{events\\: array, signatureKey\\: string, url\\: string, webhookId\\: string, paymentPointId\\?\\: string\\}\\}, array given\\.$#"
count: 1
path: ../src/MobilePay/Webhook/Response/WebhookCollection.php

-
message: "#^Strict comparison using \\=\\=\\= between false and non\\-empty\\-string will always evaluate to false\\.$#"
count: 1
path: ../src/MobilePay/Webhook/Schema/Validator.php
15 changes: 1 addition & 14 deletions .phpstan/cognitive-complexity.neon
Original file line number Diff line number Diff line change
@@ -1,15 +1,2 @@
includes:
- ../vendor/symplify/phpstan-rules/config/packages/cognitive-complexity/cognitive-complexity-services.neon

services:
-
class: Symplify\PHPStanRules\CognitiveComplexity\Rules\FunctionLikeCognitiveComplexityRule
tags: [phpstan.rules.rule]
arguments:
maxMethodCognitiveComplexity: 8

-
class: Symplify\PHPStanRules\CognitiveComplexity\Rules\ClassLikeCognitiveComplexityRule
tags: [phpstan.rules.rule]
arguments:
maxClassCognitiveComplexity: 50
- ../vendor/tomasvotruba/cognitive-complexity/config/extension.neon
1 change: 1 addition & 0 deletions .phpstan/config.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
includes:
- ../vendor/phpstan/phpstan-phpunit/extension.neon
- cognitive-complexity.neon
- type-coverage.neon

Expand Down
33 changes: 1 addition & 32 deletions .phpstan/type-coverage.neon
Original file line number Diff line number Diff line change
@@ -1,33 +1,2 @@
includes:
- ../vendor/symplify/phpstan-rules/config/services/services.neon

services:
-
class: Symplify\PHPStanRules\Rules\Explicit\PropertyTypeDeclarationSeaLevelRule
tags: [phpstan.rules.rule]
arguments:
minimalLevel: 0.99
#printSuggestions: false
-
class: Symplify\PHPStanRules\Rules\Explicit\ParamTypeDeclarationSeaLevelRule
tags: [phpstan.rules.rule]
arguments:
minimalLevel: 0.92
#printSuggestions: false

-
class: Symplify\PHPStanRules\Rules\Explicit\ReturnTypeDeclarationSeaLevelRule
tags: [phpstan.rules.rule]
arguments:
minimalLevel: 0.94
#printSuggestions: false

-
class: Symplify\PHPStanRules\Collector\FunctionLike\ParamTypeSeaLevelCollector
tags: [phpstan.collector]
-
class: Symplify\PHPStanRules\Collector\FunctionLike\ReturnTypeSeaLevelCollector
tags: [phpstan.collector]
-
class: Symplify\PHPStanRules\Collector\ClassLike\PropertyTypeSeaLevelCollector
tags: [phpstan.collector]
- ../vendor/tomasvotruba/type-coverage/config/extension.neon
14 changes: 8 additions & 6 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@

## github project structure

-- mobilepay-php
---- mobilepay-php-sdk-src
---- mobilepay-php-sdk
---- mobilepay-bundle
---- mobilepay-recipe
---- omnipay-mobilepay
- mobilepay-php
- mobilepay-php-sdk-src
- mobilepay-php-sdk
- mobilepay-bundle
- mobilepay-recipe
- omnipay-mobilepay
- webhook-provider-symfony
- sylius-mobilepay-plugin
28 changes: 16 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,24 @@
},
"require": {
"php": ">=8.1",
"psr/http-client": "^1.0",
"psr/http-factory": "^1.0",
"ramsey/uuid": "^4.5"
"justinrainbow/json-schema": "^5.2.12",
"psr/http-client": "^1.0.1",
"psr/http-factory": "^1.0.1",
"ramsey/uuid": "^4.7.1"
},
"require-dev": {
"brianium/paratest": "^6.6",
"friendsofphp/php-cs-fixer": "^3.13",
"nyholm/psr7": "^1.5",
"phpstan/phpstan": "^1.9",
"phpunit/phpunit": "^9",
"rector/rector": "^0.14.7",
"spatie/phpunit-snapshot-assertions": "^4.2",
"symfony/http-client": "^6.1",
"symplify/phpstan-rules": "^11.1"
"brianium/paratest": "^6.8",
"friendsofphp/php-cs-fixer": "^3.13.2",
"nyholm/psr7": "^1.5.1",
"phpstan/phpstan": "^1.9.7",
"phpstan/phpstan-phpunit": "^1.3.3",
"phpunit/phpunit": "^9.5.27",
"rector/rector": "^0.14.8",
"spatie/phpunit-snapshot-assertions": "^4.2.16",
"symfony/http-client": "^6.2.2",
"symplify/phpstan-rules": "^11.1.24",
"tomasvotruba/cognitive-complexity": "^0.0.1",
"tomasvotruba/type-coverage": "^0.0.1"
},
"provide": {
"php-http/client-implementation": "*"
Expand Down
10 changes: 10 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,13 @@ includes:
parameters:
paths:
- src

cognitive_complexity:
class: 50
function: 8

type_coverage:
return_type: 78
param_type: 84
property_type: 20
print_suggestions: false
28 changes: 20 additions & 8 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,30 @@
use Rector\Config\RectorConfig;
use Rector\PHPUnit\Rector\Class_\AddSeeTestAnnotationRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src'
__DIR__.'/src',
]);

# php
$rectorConfig->sets([LevelSetList::UP_TO_PHP_80]);
$rectorConfig->sets([LevelSetList::UP_TO_PHP_81]);

$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
$rectorConfig->skip([
// bug: rector ends in an error refactoring this class
__DIR__.'/src/MobilePay/AppPayment/PaymentPoints/PaymentPointState.php',
// bug: rector applies FinalizeClassesWithoutChildrenRector
__DIR__.'/src/Api/Exception/ClientException.php',
]);

# phpunit
$rectorConfig->rule(AddSeeTestAnnotationRector::class);
$rectorConfig->rules([
InlineConstructorDefaultToPropertyRector::class,
AddSeeTestAnnotationRector::class,
]);
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_81,
SetList::CODE_QUALITY,
SetList::DEAD_CODE,
SetList::EARLY_RETURN,
SetList::TYPE_DECLARATION,
SetList::PRIVATIZATION,
]);
};
15 changes: 13 additions & 2 deletions src/Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,25 @@ public function request(RequestInterface $request): ResponseInterface
{
$psr7Request = $this->requestFactory->createRequest($request->getMethod(), $this->apiHost.$request->getUri());

// todo: add custom header
// foreach($request->getHeaders() as $header) {
// $psr7Request = $psr7Request->withHeader($header->getName(), $header->getValue());
// }

$psr7Request = $psr7Request
->withHeader('Accept', 'application/json')
->withHeader('Content-Type', 'application/json')
->withHeader('Authorization', 'Bearer '.$this->apiKey)
;

$requestBody = $request->getBody();
if (!empty($requestBody)) {
$psr7Request = $psr7Request->withBody($this->streamFactory->createStream(json_encode($requestBody, JSON_THROW_ON_ERROR)));
if (null !== $requestBody) {
$psr7Request = $psr7Request->withBody(
$this->streamFactory->createStream(
// todo: add custom encoder
json_encode($requestBody, JSON_THROW_ON_ERROR)
)
);
}

$psr7Response = $this->httpClient->sendRequest($psr7Request);
Expand All @@ -42,6 +52,7 @@ public function request(RequestInterface $request): ResponseInterface

return $this->responseHandler->handle(
$statusCode,
// todo: add custom decoder
empty($contents) ? [] : json_decode($contents, true, 512, JSON_THROW_ON_ERROR)
);
}
Expand Down
3 changes: 3 additions & 0 deletions src/Api/Exception/ClientException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace MobilePayPhp\Api\Exception;

/**
* @see \MobilePayPhp\Api\Exception\ClientExceptionTest
*/
class ClientException extends ResponseException
{
public function __construct(int $code, string $message)
Expand Down
5 changes: 4 additions & 1 deletion src/Api/Exception/ServerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace MobilePayPhp\Api\Exception;

class ServerException extends ResponseException
/**
* @see \MobilePayPhp\Api\Exception\ServerExceptionTest
*/
final class ServerException extends ResponseException
{
public function __construct(int $code, string $message)
{
Expand Down
21 changes: 21 additions & 0 deletions src/Api/IsDeleteTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace MobilePayPhp\Api;

trait IsDeleteTrait
{
public function getMethod(): string
{
return 'DELETE';
}

/**
* @return mixed[]|null
*/
public function getBody(): ?array
{
return null;
}
}
13 changes: 13 additions & 0 deletions src/Api/IsPutTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace MobilePayPhp\Api;

trait IsPutTrait
{
public function getMethod(): string
{
return 'PUT';
}
}
79 changes: 79 additions & 0 deletions src/Api/JsonSchemaValidationTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace MobilePayPhp\Api;

use JsonSchema\Validator;

trait JsonSchemaValidationTrait
{
private Validator $validator;

public function __construct()
{
$this->validator = new Validator();
}

private function validate(array $payload, string $schemaPath): void
{
// JsonSchema Validator needs an object
if (false === $json = json_encode($payload, JSON_THROW_ON_ERROR)) {
// todo throw ValidationException
// throw ValidationException::jsonError();
}

$data = json_decode($json, false, 512, JSON_THROW_ON_ERROR);

$this->validator->validate($data, (object) ['$ref' => 'file://'.$schemaPath]);

if (!$this->validator->isValid()) {
throw ValidationException::fromValidationErrors(array_map(static fn (array $error): \MobilePayPhp\Api\ValidationError => ValidationError::fromArray($error), $this->validator->getErrors()));
}
}
}

final class ValidationException extends \RuntimeException
{
public function __construct(
private readonly array $validationErrors
) {
}

public static function fromValidationErrors(array $validationErrors): self
{
return new self($validationErrors);
}

/**
* @return mixed[]
*/
public function getValidationErrors(): array
{
return $this->validationErrors;
}
}

final class ValidationError
{
public function __construct(
private readonly string $property,
private readonly string $message
) {
}

public static function fromArray(array $error): self
{
return new self($error['property'], $error['message']);
}

public function getProperty(): string
{
return $this->property;
}

public function getMessage(): string
{
return $this->message;
}
}
3 changes: 3 additions & 0 deletions src/Api/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public function getStatusCode(): int
return $this->statusCode;
}

/**
* @return mixed[]
*/
public function getBody(): array
{
return $this->body;
Expand Down
Loading