Skip to content

Commit 14028d9

Browse files
author
Islam
authored
Merge pull request #1 from khandurdyiev/master
Merge master to main
2 parents 830b36d + 8d7c55f commit 14028d9

25 files changed

+1118
-0
lines changed

.gitattributes

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/.gitignore export-ignore
2+
/.gitattributes export-ignore
3+
/.github export-ignore
4+
5+
/tests export-ignore
6+
/phpcs.xml export-ignore
7+
/phpstan.neon export-ignore
8+
/phpunit.xml.dist export-ignore
9+
/psalm.xml export-ignore

.github/workflows/main.yml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Code Style And Tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
schedule:
7+
- cron: '0 0 * * *'
8+
9+
jobs:
10+
test:
11+
12+
runs-on: ubuntu-latest
13+
strategy:
14+
matrix:
15+
php: [ 7.4, 8.0 ]
16+
17+
name: P${{ matrix.php }}
18+
19+
steps:
20+
- name: Checkout
21+
uses: actions/checkout@v2
22+
23+
- name: Setup PHP
24+
uses: shivammathur/setup-php@v2
25+
with:
26+
php-version: ${{ matrix.php }}
27+
tools: composer:v2
28+
coverage: none
29+
30+
- name: Install dependencies
31+
run: composer update --prefer-dist --no-interaction --no-progress
32+
33+
- name: Run tests
34+
run: ./vendor/bin/phpunit
35+
36+
- name: PHPCS
37+
run: ./vendor/bin/phpcs
38+
39+
- name: Psalm
40+
run: ./vendor/bin/psalm
41+
42+
- name: PHPStan
43+
run: ./vendor/bin/phpstan a --no-progress

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
build
2+
vendor
3+
composer.lock
4+
.phpunit.result.cache

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
### Monobank PHP Client

composer.json

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "khandurdyiev/monobank-php-client",
3+
"description": "PHP Client for Monobank API",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Islam Khandurdyiev",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": "^7.4 || ^8.0",
14+
"ext-json": "*",
15+
"guzzlehttp/guzzle": "^7.2",
16+
"alcohol/iso4217": "^4.0",
17+
"nesbot/carbon": "^2.44"
18+
},
19+
"require-dev": {
20+
"phpstan/phpstan": "^0.12",
21+
"phpstan/phpstan-strict-rules": "^0.12",
22+
"phpunit/phpunit": "^9.5",
23+
"squizlabs/php_codesniffer": "^3.5",
24+
"vimeo/psalm": "^4.3"
25+
},
26+
"autoload": {
27+
"psr-4": {
28+
"Khandurdyiev\\MonoClient\\": "src/"
29+
}
30+
},
31+
"autoload-dev": {
32+
"psr-4": {
33+
"Khandurdyiev\\MonoClient\\Tests\\": "tests/"
34+
}
35+
}
36+
}

phpcs.xml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0"?>
2+
3+
<ruleset name="PHPCS">
4+
<rule ref="PSR12"/>
5+
<file>./src</file>
6+
<file>./tests</file>
7+
</ruleset>

phpstan.neon

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
includes:
2+
- vendor/phpstan/phpstan-strict-rules/rules.neon
3+
parameters:
4+
level: 8
5+
paths:
6+
- ./src
7+
- ./tests

phpunit.xml.dist

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit convertErrorsToExceptions="true"
3+
convertNoticesToExceptions="true"
4+
convertWarningsToExceptions="true"
5+
bootstrap="vendor/autoload.php"
6+
colors="true"
7+
>
8+
<coverage processUncoveredFiles="true">
9+
<include>
10+
<directory suffix=".php">./src</directory>
11+
</include>
12+
</coverage>
13+
<testsuites>
14+
<testsuite name="Main">
15+
<directory>./tests</directory>
16+
</testsuite>
17+
</testsuites>
18+
</phpunit>

psalm.xml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<psalm errorLevel="2">
3+
<projectFiles>
4+
<directory name="src" />
5+
<directory name="tests/" />
6+
<ignoreFiles>
7+
<directory name="vendor/" />
8+
</ignoreFiles>
9+
</projectFiles>
10+
</psalm>

src/Client.php

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Khandurdyiev\MonoClient;
6+
7+
use GuzzleHttp\Client as GuzzleClient;
8+
use GuzzleHttp\Exception\RequestException;
9+
use GuzzleHttp\Psr7\Message;
10+
use Khandurdyiev\MonoClient\Exceptions\MonobankApiException;
11+
use Psr\Http\Message\ResponseInterface;
12+
use Throwable;
13+
14+
class Client
15+
{
16+
private const URL = 'https://api.monobank.ua';
17+
18+
private GuzzleClient $client;
19+
20+
/**
21+
* @param array<string, mixed> $customConfig
22+
*/
23+
public function __construct(array $customConfig)
24+
{
25+
$config = array_merge(['base_uri' => self::URL], $customConfig);
26+
27+
$this->client = new GuzzleClient($config);
28+
}
29+
30+
/**
31+
* @param array<string, mixed> $customConfig
32+
*
33+
* @return Client
34+
*/
35+
public static function create(array $customConfig = []): Client
36+
{
37+
return new self($customConfig);
38+
}
39+
40+
/**
41+
* @param string $uri
42+
* @param array<string, mixed> $options
43+
*
44+
* @return ResponseInterface
45+
*
46+
* @throws MonobankApiException
47+
*/
48+
public function get(string $uri, array $options = []): ResponseInterface
49+
{
50+
return $this->request('GET', $uri, $options);
51+
}
52+
53+
/**
54+
* @param string $uri
55+
* @param array<string, mixed> $options
56+
*
57+
* @return ResponseInterface
58+
*
59+
* @throws MonobankApiException
60+
*/
61+
public function head(string $uri, array $options = []): ResponseInterface
62+
{
63+
return $this->request('HEAD', $uri, $options);
64+
}
65+
66+
/**
67+
* @param string $uri
68+
* @param array<string, mixed> $options
69+
*
70+
* @return ResponseInterface
71+
*
72+
* @throws MonobankApiException
73+
*/
74+
public function put(string $uri, array $options = []): ResponseInterface
75+
{
76+
return $this->request('PUT', $uri, $options);
77+
}
78+
79+
/**
80+
* @param string $uri
81+
* @param array<string, mixed> $options
82+
*
83+
* @return ResponseInterface
84+
*
85+
* @throws MonobankApiException
86+
*/
87+
public function post(string $uri, array $options = []): ResponseInterface
88+
{
89+
return $this->request('POST', $uri, $options);
90+
}
91+
92+
/**
93+
* @param string $uri
94+
* @param array<string, mixed> $options
95+
*
96+
* @return ResponseInterface
97+
*
98+
* @throws MonobankApiException
99+
*/
100+
public function patch(string $uri, array $options = []): ResponseInterface
101+
{
102+
return $this->request('PATCH', $uri, $options);
103+
}
104+
105+
/**
106+
* @param string $uri
107+
* @param array<string, mixed> $options
108+
*
109+
* @return ResponseInterface
110+
*
111+
* @throws MonobankApiException
112+
*/
113+
public function delete(string $uri, array $options = []): ResponseInterface
114+
{
115+
return $this->request('DELETE', $uri, $options);
116+
}
117+
118+
/**
119+
* @param string $method
120+
* @param string $uri
121+
* @param array<string, mixed> $options
122+
*
123+
* @return ResponseInterface
124+
*
125+
* @throws MonobankApiException
126+
*/
127+
public function request(string $method, $uri = '', array $options = []): ResponseInterface
128+
{
129+
try {
130+
$response = $this->client->request($method, $uri, $options);
131+
} catch (Throwable $exception) {
132+
$message = $exception->getMessage();
133+
134+
if ($exception instanceof RequestException && $exception->hasResponse()) {
135+
/** @var ResponseInterface $response */
136+
$response = $exception->getResponse();
137+
138+
$message = Message::toString($response);
139+
}
140+
141+
throw new MonobankApiException($message, (int) $exception->getCode(), $exception);
142+
}
143+
144+
return $response;
145+
}
146+
}

0 commit comments

Comments
 (0)