Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoray committed Jan 10, 2025
1 parent 042a8e3 commit 23316fe
Show file tree
Hide file tree
Showing 47 changed files with 766 additions and 494 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
"friendsofphp/php-cs-fixer": "^3.66",
"guzzlehttp/guzzle": "^7.6",
"phpstan/phpstan": "^2.0",
"phpunit/phpunit": "^9.6"
"phpunit/phpunit": "^9.6",
"symfony/var-dumper": "^6.0"
},
"suggest": {
"mollie/oauth2-mollie-php": "Use OAuth to authenticate with the Mollie API. This is needed for some endpoints. Visit https://docs.mollie.com/ for more information."
Expand Down
6 changes: 0 additions & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -366,12 +366,6 @@ parameters:
count: 1
path: tests/EndpointCollection/SubscriptionEndpointCollectionTest.php

-
message: '#^Call to method PHPUnit\\Framework\\Assert\:\:assertTrue\(\) with true will always evaluate to true\.$#'
identifier: method.alreadyNarrowedType
count: 1
path: tests/Http/Middleware/GuardResponseTest.php

-
message: '#^Access to an undefined property Mollie\\Api\\Resources\\AnyResource\:\:\$customField\.$#'
identifier: property.notFound
Expand Down
16 changes: 4 additions & 12 deletions src/Contracts/SupportsDebuggingContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,9 @@

interface SupportsDebuggingContract
{
/**
* Enable debugging for the current request.
*
* @return HttpAdapterContract|Connector
*/
public function enableDebugging();
public function debugRequest(?callable $debugger = null, bool $die = false): self;

/**
* Disable debugging for the current request.
*
* @return HttpAdapterContract|Connector
*/
public function disableDebugging();
public function debugResponse(?callable $debugger = null, bool $die = false): self;

public function debug(bool $die = false): self;
}
2 changes: 1 addition & 1 deletion src/Contracts/SupportsTestmodeInPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Mollie\Api\Contracts;

interface SupportsTestmodeInPayload extends SupportsTestmode
interface SupportsTestmodeInPayload extends SupportsTestmode, HasPayload
{
//
}
19 changes: 3 additions & 16 deletions src/Exceptions/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
use Psr\Http\Message\ResponseInterface;
use Throwable;

class ApiException extends \Exception
class ApiException extends MollieException
{
protected ?string $field = null;

protected string $plainMessage;

protected ?RequestInterface $request;

protected ?ResponseInterface $response;
protected ?ResponseInterface $response = null;

/**
* ISO8601 representation of the moment this exception was thrown
Expand All @@ -30,7 +28,6 @@ class ApiException extends \Exception
public function __construct(
string $message = '',
int $code = 0,
?string $field = null,
?RequestInterface $request = null,
?ResponseInterface $response = null,
?Throwable $previous = null
Expand All @@ -40,12 +37,7 @@ public function __construct(
$this->raisedAt = new DateTimeImmutable;

$formattedRaisedAt = $this->raisedAt->format(DateTimeImmutable::ATOM);
$message = "[{$formattedRaisedAt}] ".$message;

if (! empty($field)) {
$this->field = (string) $field;
$message .= ". Field: {$this->field}";
}
$message = "[{$formattedRaisedAt}] " . $message;

if (! empty($response)) {
$this->response = $response;
Expand Down Expand Up @@ -75,11 +67,6 @@ public function __construct(
parent::__construct($message, $code, $previous);
}

public function getField(): ?string
{
return $this->field;
}

public function getDocumentationUrl(): ?string
{
return $this->getUrl('documentation');
Expand Down
5 changes: 5 additions & 0 deletions src/Exceptions/ClientException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Mollie\Api\Exceptions;

class ClientException extends MollieException {}
7 changes: 0 additions & 7 deletions src/Exceptions/CurlConnectTimeoutException.php

This file was deleted.

21 changes: 21 additions & 0 deletions src/Exceptions/ForbiddenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Mollie\Api\Exceptions;

use Mollie\Api\Http\Response;

class ForbiddenException extends ApiException
{
public static function fromResponse(Response $response): self
{
$body = $response->json();

return new self(
'Your request was understood but not allowed. ' .
sprintf('Error executing API call (%d: %s): %s', 403, $body->title, $body->detail),
403,
$response->getPsrRequest(),
$response->getPsrResponse()
);
}
}

This file was deleted.

19 changes: 19 additions & 0 deletions src/Exceptions/InvalidAuthenticationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Mollie\Api\Exceptions;

class InvalidAuthenticationException extends ClientException
{
private string $token;

public function __construct(string $token, string $message = '')
{
$this->token = $token;
parent::__construct($message ?: "Invalid authentication token: '{$token}'");
}

public function getToken(): string
{
return $this->token;
}
}
21 changes: 21 additions & 0 deletions src/Exceptions/MethodNotAllowedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Mollie\Api\Exceptions;

use Mollie\Api\Http\Response;

class MethodNotAllowedException extends ApiException
{
public static function fromResponse(Response $response): self
{
$body = $response->json();

return new self(
'The HTTP method is not supported. ' .
sprintf('Error executing API call (%d: %s): %s', 405, $body->title, $body->detail),
405,
$response->getPsrRequest(),
$response->getPsrResponse()
);
}
}
11 changes: 11 additions & 0 deletions src/Exceptions/MissingAuthenticationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Mollie\Api\Exceptions;

class MissingAuthenticationException extends ClientException
{
public function __construct()
{
parent::__construct('You have not set an API key or OAuth access token. Please use setApiKey() to set the API key.');
}
}
5 changes: 5 additions & 0 deletions src/Exceptions/MollieException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Mollie\Api\Exceptions;

class MollieException extends \Exception {}
21 changes: 21 additions & 0 deletions src/Exceptions/NotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Mollie\Api\Exceptions;

use Mollie\Api\Http\Response;

class NotFoundException extends ApiException
{
public static function fromResponse(Response $response): self
{
$body = $response->json();

return new self(
'The object referenced by your API request does not exist. ' .
sprintf('Error executing API call (%d: %s): %s', 404, $body->title, $body->detail),
404,
$response->getPsrRequest(),
$response->getPsrResponse()
);
}
}
26 changes: 26 additions & 0 deletions src/Exceptions/RequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Mollie\Api\Exceptions;

use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\RequestInterface;
use Throwable;

class RequestException extends MollieException implements ClientExceptionInterface
{
protected RequestInterface $request;

public function __construct(
string $message,
RequestInterface $request,
?Throwable $previous = null
) {
parent::__construct($message, 0, $previous);
$this->request = $request;
}

public function getRequest(): RequestInterface
{
return $this->request;
}
}
21 changes: 21 additions & 0 deletions src/Exceptions/RequestTimeoutException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Mollie\Api\Exceptions;

use Mollie\Api\Http\Response;

class RequestTimeoutException extends ApiException
{
public static function fromResponse(Response $response): self
{
$body = $response->json();

return new self(
'The request took too long to complete. ' .
sprintf('Error executing API call (%d: %s): %s', 408, $body->title, $body->detail),
408,
$response->getPsrRequest(),
$response->getPsrResponse()
);
}
}
40 changes: 40 additions & 0 deletions src/Exceptions/ResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Mollie\Api\Exceptions;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class ResponseException extends ClientException
{
private ?ResponseInterface $response;
private ?RequestInterface $request;
private ?string $field;

public function __construct(
string $message,
?ResponseInterface $response = null,
?RequestInterface $request = null,
?string $field = null
) {
$this->response = $response;
$this->request = $request;
$this->field = $field;
parent::__construct($message);
}

public function getResponse(): ?ResponseInterface
{
return $this->response;
}

public function getRequest(): ?RequestInterface
{
return $this->request;
}

public function getField(): ?string
{
return $this->field;
}
}
21 changes: 21 additions & 0 deletions src/Exceptions/ServiceUnavailableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Mollie\Api\Exceptions;

use Mollie\Api\Http\Response;

class ServiceUnavailableException extends ApiException
{
public static function fromResponse(Response $response): self
{
$body = $response->json();

return new self(
'The service is temporarily unavailable. ' .
sprintf('Error executing API call (%d: %s): %s', 503, $body->title, $body->detail),
503,
$response->getPsrRequest(),
$response->getPsrResponse()
);
}
}
21 changes: 21 additions & 0 deletions src/Exceptions/TooManyRequestsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Mollie\Api\Exceptions;

use Mollie\Api\Http\Response;

class TooManyRequestsException extends ApiException
{
public static function fromResponse(Response $response): self
{
$body = $response->json();

return new self(
'Your request exceeded the rate limit. ' .
sprintf('Error executing API call (%d: %s): %s', 429, $body->title, $body->detail),
429,
$response->getPsrRequest(),
$response->getPsrResponse()
);
}
}
21 changes: 21 additions & 0 deletions src/Exceptions/UnauthorizedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Mollie\Api\Exceptions;

use Mollie\Api\Http\Response;

class UnauthorizedException extends ApiException
{
public static function fromResponse(Response $response): self
{
$body = $response->json();

return new self(
'Your request wasn\'t executed due to failed authentication. Check your API key. ' .
sprintf('Error executing API call (%d: %s): %s', 401, $body->title, $body->detail),
401,
$response->getPsrRequest(),
$response->getPsrResponse()
);
}
}
Loading

0 comments on commit 23316fe

Please sign in to comment.