Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedghanem00 committed Jun 21, 2023
1 parent 71246bb commit 5b53a32
Show file tree
Hide file tree
Showing 28 changed files with 246 additions and 329 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ name: PHP Test

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

permissions:
contents: read
Expand All @@ -21,7 +19,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: 8.2

- name: Validate composer.json and composer.lock
run: composer validate --strict

Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# tempnumber-api-client
# TempNumber Api Client

An API client for Temp-Number service ( https://temp-number.org/ ) written in PHP.

Expand All @@ -7,7 +7,7 @@ If you encounter a bug or have an idea to improve the code, feel free to open an
## Installation

````
$ Composer require ahmedghanem00/tempnumber-api-client
$ composer require ahmedghanem00/tempnumber-api-client
````

## Usage
Expand Down Expand Up @@ -64,7 +64,7 @@ In case you have specific activation that needs to be retried

````php
try {
$client->retryActivation($newActivation->getId());
$client->retryActivation(181822);
} catch (Exception $e) {
echo $e->getMessage();
}
Expand Down Expand Up @@ -139,5 +139,6 @@ foreach ($result->activations() as $activation) {
}
````

## LICENSE

##
Package is licensed under the [MIT License](http://opensource.org/licenses/MIT).
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@
"phpstan": "@php vendor/bin/phpstan analyse src tests"
},

"minimum-stability": "dev"
"minimum-stability": "stable"
}
45 changes: 2 additions & 43 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,7 @@
use ahmedghanem00\TempNumberClient\Enum\Country;
use ahmedghanem00\TempNumberClient\Enum\Service;
use ahmedghanem00\TempNumberClient\Enum\TempNumberServer;
use ahmedghanem00\TempNumberClient\Exception\Api\AccountOnHoldException;
use ahmedghanem00\TempNumberClient\Exception\Api\ApiException;
use ahmedghanem00\TempNumberClient\Exception\Api\ExpectedPriceException;
use ahmedghanem00\TempNumberClient\Exception\Api\GoneException;
use ahmedghanem00\TempNumberClient\Exception\Api\InvalidRequestParamsException;
use ahmedghanem00\TempNumberClient\Exception\Api\LowSuccessRateException;
use ahmedghanem00\TempNumberClient\Exception\Api\PaymentRequiredException;
use ahmedghanem00\TempNumberClient\Exception\Api\ResourceBadStateException;
use ahmedghanem00\TempNumberClient\Exception\Api\ResourceNotFoundException;
use ahmedghanem00\TempNumberClient\Exception\Api\ServiceUnavailableException;
use ahmedghanem00\TempNumberClient\Exception\Api\TooManyActivationsPendingException;
use ahmedghanem00\TempNumberClient\Exception\Api\TooManyRequestsException;
use ahmedghanem00\TempNumberClient\Exception\Api\UnauthorizedServiceException;
use ahmedghanem00\TempNumberClient\Exception\API\APIException;
use ahmedghanem00\TempNumberClient\Exception\ClientException;
use ahmedghanem00\TempNumberClient\Result\ActivationHistoryResult;
use ahmedghanem00\TempNumberClient\Result\ActivationResult;
Expand Down Expand Up @@ -162,41 +150,12 @@ private function checkAndGetResultDataFromResponse(ResponseInterface $response):
$resultData = $response->toArray(false);

if (@$errorName = $resultData['errorName']) {
$this->handleApiError($errorName, $resultData, $response);
throw APIException::newFromErrorName($errorName, $response);
}

return $resultData;
}

/**
* @param string $errorName
* @param array $resultData
* @param ResponseInterface $response
* @return void
*/
private function handleApiError(string $errorName, array $resultData, ResponseInterface $response): void
{
$exception = match ($errorName) {
'UnauthorizedException' => new UnauthorizedServiceException(),
'InvalidRequestParamsException' => new InvalidRequestParamsException(),
'paymentRequired' => new PaymentRequiredException(),
'AccountOnHoldException' => new AccountOnHoldException(),
'ResourceNotFoundException' => new ResourceNotFoundException(),
'ResourceBadStateException' => new ResourceBadStateException(),
'TooManyActivationsPendingException' => new TooManyActivationsPendingException(),
'TooManyRequestsException' => new TooManyRequestsException(),
'expectedPriceErrorException' => new ExpectedPriceException($resultData['newPrice']),
'GoneException' => new GoneException(),
'lowSuccessRateException' => new LowSuccessRateException(),
'ServiceUnavailableException' => new ServiceUnavailableException(),
default => new ApiException()
};

$exception->setResponse($response);

throw $exception;
}

/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
Expand Down
84 changes: 84 additions & 0 deletions src/Exception/API/APIException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php declare(strict_types=1);
/*
* This file is part of the TempNumberClient package.
*
* (c) Ahmed Ghanem <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ahmedghanem00\TempNumberClient\Exception\API;

use ahmedghanem00\TempNumberClient\Exception\ClientException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

/**
*
*/
class APIException extends ClientException
{
public const Description = "API Failed Action";

/**
* @param ResponseInterface $response
*/
public function __construct(
private ResponseInterface $response
)
{
parent::__construct(static::Description);
}

/**
* @param string $errorName
* @param ResponseInterface $response
* @return static
*/
public static function newFromErrorName(string $errorName, ResponseInterface $response): self
{
$exceptionClass = match (strtolower($errorName)) {
'unauthorizedexception' => UnauthorizedServiceException::class,
'invalidrequestparamsexception' => InvalidRequestParamsException::class,
'paymentrequired' => PaymentRequiredException::class,
'accountonholdexception' => AccountOnHoldException::class,
'resourcenotfoundexception' => ResourceNotFoundException::class,
'resourcebadstateexception' => ResourceBadStateException::class,
'toomanyactivationspendingexception' => TooManyActivationsPendingException::class,
'toomanyrequestsexception' => TooManyRequestsException::class,
'expectedpriceerrorexception' => ExpectedPriceException::class,
'goneexception' => GoneException::class,
'lowsuccessrateexception' => LowSuccessRateException::class,
'serviceunavailableexception' => ServiceUnavailableException::class,

default => self::class
};

return new $exceptionClass($response);
}

/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
*/
public function getResultData(): array
{
return $this->getResponse()->toArray(false);
}

/**
* @return ResponseInterface
*/
public function getResponse(): ResponseInterface
{
return $this->response;
}
}
15 changes: 15 additions & 0 deletions src/Exception/API/AccountOnHoldException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);
/*
* This file is part of the TempNumberClient package.
*
* (c) Ahmed Ghanem <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ahmedghanem00\TempNumberClient\Exception\API;

class AccountOnHoldException extends APIException
{
}
32 changes: 32 additions & 0 deletions src/Exception/API/ExpectedPriceException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types=1);
/*
* This file is part of the TempNumberClient package.
*
* (c) Ahmed Ghanem <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ahmedghanem00\TempNumberClient\Exception\API;

use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;

class ExpectedPriceException extends APIException
{
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
*/
public function getNewPrice(): float
{
return (float)$this->getResultData()['newPrice'];
}
}
16 changes: 16 additions & 0 deletions src/Exception/API/GoneException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);
/*
* This file is part of the TempNumberClient package.
*
* (c) Ahmed Ghanem <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ahmedghanem00\TempNumberClient\Exception\API;

class GoneException extends APIException
{
public const Description = "The target resource is no longer available";
}
15 changes: 15 additions & 0 deletions src/Exception/API/InvalidRequestParamsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);
/*
* This file is part of the TempNumberClient package.
*
* (c) Ahmed Ghanem <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ahmedghanem00\TempNumberClient\Exception\API;

class InvalidRequestParamsException extends APIException
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,16 @@
* file that was distributed with this source code.
*/

namespace ahmedghanem00\TempNumberClient\Exception\Api;

use ahmedghanem00\TempNumberClient\Exception\ClientException;
namespace ahmedghanem00\TempNumberClient\Exception\API;

/**
*
* Example: 100 activations requested for Whatsapp in Germany but only 1 activation got sms.
* This is 1% delivery success rate. No more activations allowed in 24 hours to Whatsapp in the Netherlands.
* This is 1% delivery success rate. No more activations allowed in 24 hours to Whatsapp in the Germany.
*
*/
class LowSuccessRateException extends ApiException implements TemporaryExceptionInterface
class LowSuccessRateException extends APIException implements TemporaryErrorInterface
{
/**
*
*/
public function __construct()
{
ClientException::__construct("Activations low success rate");
}

/**
* @return int
*/
Expand Down
18 changes: 18 additions & 0 deletions src/Exception/API/PaymentRequiredException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);
/*
* This file is part of the TempNumberClient package.
*
* (c) Ahmed Ghanem <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ahmedghanem00\TempNumberClient\Exception\API;

/**
*
*/
class PaymentRequiredException extends APIException
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@
* file that was distributed with this source code.
*/

namespace ahmedghanem00\TempNumberClient\Exception\Api;

use ahmedghanem00\TempNumberClient\Exception\ClientException;
namespace ahmedghanem00\TempNumberClient\Exception\API;

/**
*
*/
class ResourceNotFoundException extends ApiException
class ResourceBadStateException extends APIException implements TemporaryErrorInterface
{
/**
*
* @return int
*/
public function __construct()
public function retryAfter(): int
{
ClientException::__construct("Resource not found");
return 10 * 60;
}
}
18 changes: 18 additions & 0 deletions src/Exception/API/ResourceNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);
/*
* This file is part of the TempNumberClient package.
*
* (c) Ahmed Ghanem <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ahmedghanem00\TempNumberClient\Exception\API;

/**
*
*/
class ResourceNotFoundException extends APIException
{
}
Loading

0 comments on commit 5b53a32

Please sign in to comment.