-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from khandurdyiev/master
Merge master to main
- Loading branch information
Showing
25 changed files
with
1,118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/.gitignore export-ignore | ||
/.gitattributes export-ignore | ||
/.github export-ignore | ||
|
||
/tests export-ignore | ||
/phpcs.xml export-ignore | ||
/phpstan.neon export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/psalm.xml export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
name: Code Style And Tests | ||
|
||
on: | ||
push: | ||
pull_request: | ||
schedule: | ||
- cron: '0 0 * * *' | ||
|
||
jobs: | ||
test: | ||
|
||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
php: [ 7.4, 8.0 ] | ||
|
||
name: P${{ matrix.php }} | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
|
||
- name: Setup PHP | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: ${{ matrix.php }} | ||
tools: composer:v2 | ||
coverage: none | ||
|
||
- name: Install dependencies | ||
run: composer update --prefer-dist --no-interaction --no-progress | ||
|
||
- name: Run tests | ||
run: ./vendor/bin/phpunit | ||
|
||
- name: PHPCS | ||
run: ./vendor/bin/phpcs | ||
|
||
- name: Psalm | ||
run: ./vendor/bin/psalm | ||
|
||
- name: PHPStan | ||
run: ./vendor/bin/phpstan a --no-progress |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
build | ||
vendor | ||
composer.lock | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
### Monobank PHP Client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"name": "khandurdyiev/monobank-php-client", | ||
"description": "PHP Client for Monobank API", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Islam Khandurdyiev", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": "^7.4 || ^8.0", | ||
"ext-json": "*", | ||
"guzzlehttp/guzzle": "^7.2", | ||
"alcohol/iso4217": "^4.0", | ||
"nesbot/carbon": "^2.44" | ||
}, | ||
"require-dev": { | ||
"phpstan/phpstan": "^0.12", | ||
"phpstan/phpstan-strict-rules": "^0.12", | ||
"phpunit/phpunit": "^9.5", | ||
"squizlabs/php_codesniffer": "^3.5", | ||
"vimeo/psalm": "^4.3" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Khandurdyiev\\MonoClient\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Khandurdyiev\\MonoClient\\Tests\\": "tests/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0"?> | ||
|
||
<ruleset name="PHPCS"> | ||
<rule ref="PSR12"/> | ||
<file>./src</file> | ||
<file>./tests</file> | ||
</ruleset> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
includes: | ||
- vendor/phpstan/phpstan-strict-rules/rules.neon | ||
parameters: | ||
level: 8 | ||
paths: | ||
- ./src | ||
- ./tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
> | ||
<coverage processUncoveredFiles="true"> | ||
<include> | ||
<directory suffix=".php">./src</directory> | ||
</include> | ||
</coverage> | ||
<testsuites> | ||
<testsuite name="Main"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0"?> | ||
<psalm errorLevel="2"> | ||
<projectFiles> | ||
<directory name="src" /> | ||
<directory name="tests/" /> | ||
<ignoreFiles> | ||
<directory name="vendor/" /> | ||
</ignoreFiles> | ||
</projectFiles> | ||
</psalm> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Khandurdyiev\MonoClient; | ||
|
||
use GuzzleHttp\Client as GuzzleClient; | ||
use GuzzleHttp\Exception\RequestException; | ||
use GuzzleHttp\Psr7\Message; | ||
use Khandurdyiev\MonoClient\Exceptions\MonobankApiException; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Throwable; | ||
|
||
class Client | ||
{ | ||
private const URL = 'https://api.monobank.ua'; | ||
|
||
private GuzzleClient $client; | ||
|
||
/** | ||
* @param array<string, mixed> $customConfig | ||
*/ | ||
public function __construct(array $customConfig) | ||
{ | ||
$config = array_merge(['base_uri' => self::URL], $customConfig); | ||
|
||
$this->client = new GuzzleClient($config); | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $customConfig | ||
* | ||
* @return Client | ||
*/ | ||
public static function create(array $customConfig = []): Client | ||
{ | ||
return new self($customConfig); | ||
} | ||
|
||
/** | ||
* @param string $uri | ||
* @param array<string, mixed> $options | ||
* | ||
* @return ResponseInterface | ||
* | ||
* @throws MonobankApiException | ||
*/ | ||
public function get(string $uri, array $options = []): ResponseInterface | ||
{ | ||
return $this->request('GET', $uri, $options); | ||
} | ||
|
||
/** | ||
* @param string $uri | ||
* @param array<string, mixed> $options | ||
* | ||
* @return ResponseInterface | ||
* | ||
* @throws MonobankApiException | ||
*/ | ||
public function head(string $uri, array $options = []): ResponseInterface | ||
{ | ||
return $this->request('HEAD', $uri, $options); | ||
} | ||
|
||
/** | ||
* @param string $uri | ||
* @param array<string, mixed> $options | ||
* | ||
* @return ResponseInterface | ||
* | ||
* @throws MonobankApiException | ||
*/ | ||
public function put(string $uri, array $options = []): ResponseInterface | ||
{ | ||
return $this->request('PUT', $uri, $options); | ||
} | ||
|
||
/** | ||
* @param string $uri | ||
* @param array<string, mixed> $options | ||
* | ||
* @return ResponseInterface | ||
* | ||
* @throws MonobankApiException | ||
*/ | ||
public function post(string $uri, array $options = []): ResponseInterface | ||
{ | ||
return $this->request('POST', $uri, $options); | ||
} | ||
|
||
/** | ||
* @param string $uri | ||
* @param array<string, mixed> $options | ||
* | ||
* @return ResponseInterface | ||
* | ||
* @throws MonobankApiException | ||
*/ | ||
public function patch(string $uri, array $options = []): ResponseInterface | ||
{ | ||
return $this->request('PATCH', $uri, $options); | ||
} | ||
|
||
/** | ||
* @param string $uri | ||
* @param array<string, mixed> $options | ||
* | ||
* @return ResponseInterface | ||
* | ||
* @throws MonobankApiException | ||
*/ | ||
public function delete(string $uri, array $options = []): ResponseInterface | ||
{ | ||
return $this->request('DELETE', $uri, $options); | ||
} | ||
|
||
/** | ||
* @param string $method | ||
* @param string $uri | ||
* @param array<string, mixed> $options | ||
* | ||
* @return ResponseInterface | ||
* | ||
* @throws MonobankApiException | ||
*/ | ||
public function request(string $method, $uri = '', array $options = []): ResponseInterface | ||
{ | ||
try { | ||
$response = $this->client->request($method, $uri, $options); | ||
} catch (Throwable $exception) { | ||
$message = $exception->getMessage(); | ||
|
||
if ($exception instanceof RequestException && $exception->hasResponse()) { | ||
/** @var ResponseInterface $response */ | ||
$response = $exception->getResponse(); | ||
|
||
$message = Message::toString($response); | ||
} | ||
|
||
throw new MonobankApiException($message, (int) $exception->getCode(), $exception); | ||
} | ||
|
||
return $response; | ||
} | ||
} |
Oops, something went wrong.