Skip to content

Commit

Permalink
Merge pull request #1 from khandurdyiev/master
Browse files Browse the repository at this point in the history
Merge master to main
  • Loading branch information
Islam authored Feb 28, 2021
2 parents 830b36d + 8d7c55f commit 14028d9
Show file tree
Hide file tree
Showing 25 changed files with 1,118 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitattributes
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
43 changes: 43 additions & 0 deletions .github/workflows/main.yml
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
build
vendor
composer.lock
.phpunit.result.cache
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
### Monobank PHP Client
36 changes: 36 additions & 0 deletions composer.json
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/"
}
}
}
7 changes: 7 additions & 0 deletions phpcs.xml
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>
7 changes: 7 additions & 0 deletions phpstan.neon
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
18 changes: 18 additions & 0 deletions phpunit.xml.dist
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>
10 changes: 10 additions & 0 deletions psalm.xml
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>
146 changes: 146 additions & 0 deletions src/Client.php
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;
}
}
Loading

0 comments on commit 14028d9

Please sign in to comment.