Skip to content

Commit

Permalink
Info service
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas-novotny committed Jul 19, 2022
1 parent f88c0a6 commit 811f731
Show file tree
Hide file tree
Showing 131 changed files with 3,920 additions and 4,082 deletions.
11 changes: 0 additions & 11 deletions .styleci.yml

This file was deleted.

18 changes: 11 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"keywords": [
"inspirum",
"inspishop",
"balikobot"
"balikobot",
"api-client"
],
"homepage": "https://github.com/inspirum/balikobot-php",
"license": "MIT",
Expand All @@ -17,18 +18,21 @@
}
],
"require": {
"php": ">=8.0",
"php": ">=8.1",
"ext-curl": "*",
"ext-json": "*",
"psr/http-message": "^1.0",
"guzzlehttp/psr7": "^2.0"
"guzzlehttp/psr7": "^2.0",
"inspirum/arrayable": "^1.0"
},
"require-dev": {
"inspirum/coding-standard": "^1.0",
"phpstan/phpstan": "^1.6",
"inspirum/coding-standard": "^1.1",
"phpstan/phpstan": "^1.8",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.6"
"squizlabs/php_codesniffer": "^3.7"
},
"minimum-stability": "dev",
"prefer-stable": true,
"autoload": {
"psr-4": {
"Inspirum\\Balikobot\\": "src"
Expand Down Expand Up @@ -89,7 +93,7 @@
"@composerUnused"
],
"phpunit": "./vendor/bin/phpunit",
"phpcs": "./vendor/bin/phpcs -p -s --extensions=php --colors --report-width=140 --parallel=50 || true",
"phpcs": "./vendor/bin/phpcs -p -s --extensions=php --colors --report-width=140 --parallel=50",
"phpstan": "./vendor/bin/phpstan analyse -c phpstan.neon.dist",
"phpcbf": "./vendor/bin/phpcbf -p --extensions=php --parallel=50",
"infection": "./tools/infection",
Expand Down
12 changes: 8 additions & 4 deletions src/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@

namespace Inspirum\Balikobot\Client;

use Inspirum\Balikobot\Client\Request\CarrierType;
use Inspirum\Balikobot\Client\Request\Method;
use Inspirum\Balikobot\Client\Request\Version;

interface Client
{
/**
* Call API server, parse and validate response data
*
* @param array<mixed,mixed> $data
*
* @return array<mixed,mixed>
* @return array<string,mixed>
*
* @throws \Inspirum\Balikobot\Exception\Exception
*/
public function call(
string $version,
?string $carrier,
string $request,
Version $version,
?CarrierType $carrier,
Method $request,
array $data = [],
string $path = '',
bool $shouldHaveStatus = true,
Expand Down
23 changes: 12 additions & 11 deletions src/Client/DefaultClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
namespace Inspirum\Balikobot\Client;

use GuzzleHttp\Psr7\InflateStream;
use Inspirum\Balikobot\Client\Request\CarrierType;
use Inspirum\Balikobot\Client\Request\Method;
use Inspirum\Balikobot\Client\Request\Version;
use Inspirum\Balikobot\Client\Response\Validator;
use Inspirum\Balikobot\Exception\BadRequestException;
use Inspirum\Balikobot\Response\Validator;
use JsonException;
use Psr\Http\Message\StreamInterface;
use Throwable;
Expand All @@ -24,19 +27,17 @@ public function __construct(
) {
}

/**
* @inheritDoc
*/
/** @inheritDoc */
public function call(
string $version,
?string $carrier,
string $request,
Version $version,
?CarrierType $carrier,
Method $request,
array $data = [],
string $path = '',
bool $shouldHaveStatus = true,
bool $gzip = false,
): array {
$url = $this->resolveUrl($version, $carrier, $request, $path, $gzip);
$url = $this->resolveUrl($version->getValue(), $carrier?->getValue(), $request->getValue(), $path, $gzip);

$response = $this->requester->request($url, $data);

Expand All @@ -55,14 +56,14 @@ private function resolveUrl(string $version, ?string $carrier, string $request,
$url = trim(str_replace('//', '/', $url), '/');

if ($gzip) {
$url = sprintf('%s?gzip=1', $path);
$url = sprintf('%s?gzip=1', $url);
}

return sprintf('%s/%s', $version, $url);
}

/**
* @return array<mixed,mixed>
* @return array<string,mixed>
*
* @throws \Inspirum\Balikobot\Exception\Exception
*/
Expand Down Expand Up @@ -97,7 +98,7 @@ private function getContents(StreamInterface $stream, bool $gzip): string
}

/**
* @param array<mixed,mixed> $response
* @param array<string,mixed> $response
*
* @throws \Inspirum\Balikobot\Exception\Exception
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
use const CURLOPT_SSL_VERIFYPEER;
use const CURLOPT_URL;

final class CurlRequester implements Requester
final class DefaultCurlRequester implements Requester
{
public function __construct(
private string $apiUser,
Expand All @@ -37,9 +37,7 @@ public function __construct(
) {
}

/**
* @inheritDoc
*/
/** @inheritDoc */
public function request(string $url, array $data = []): ResponseInterface
{
// init curl
Expand Down
10 changes: 10 additions & 0 deletions src/Client/Request/CarrierType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Inspirum\Balikobot\Client\Request;

interface CarrierType
{
public function getValue(): string;
}
10 changes: 10 additions & 0 deletions src/Client/Request/Method.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Inspirum\Balikobot\Client\Request;

interface Method
{
public function getValue(): string;
}
12 changes: 12 additions & 0 deletions src/Client/Request/ServiceType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Inspirum\Balikobot\Client\Request;

use Stringable;

interface ServiceType extends Stringable
{
public function getValue(): string;
}
10 changes: 10 additions & 0 deletions src/Client/Request/Version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Inspirum\Balikobot\Client\Request;

interface Version
{
public function getValue(): string;
}
28 changes: 13 additions & 15 deletions src/Services/Validator.php → src/Client/Response/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@

declare(strict_types=1);

namespace Inspirum\Balikobot\Services;
namespace Inspirum\Balikobot\Client\Response;

use Inspirum\Balikobot\Exceptions\BadRequestException;
use Inspirum\Balikobot\Exceptions\UnauthorizedException;
use function array_keys;
use function range;
use Inspirum\Balikobot\Exception\BadRequestException;
use Inspirum\Balikobot\Exception\UnauthorizedException;
use function array_is_list;
use function count;
use function max;

class Validator
final class Validator
{
/**
* Validate status code
*
* @param int $statusCode
* @param array<mixed,mixed> $response
*
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
* @throws \Inspirum\Balikobot\Exception\Exception
*/
public function validateStatus(int $statusCode, array $response = []): void
{
Expand All @@ -28,7 +29,7 @@ public function validateStatus(int $statusCode, array $response = []): void

// request error
if ($statusCode >= 400) {
throw new BadRequestException($response, (int) ($response['status'] ?? $statusCode));
throw new BadRequestException($response, (int) max($statusCode, $response['status'] ?? 0));
}
}

Expand All @@ -41,7 +42,7 @@ public function validateStatus(int $statusCode, array $response = []): void
*
* @return void
*
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
* @throws \Inspirum\Balikobot\Exception\Exception
*/
public function validateResponseStatus(
array $responseItem,
Expand All @@ -64,7 +65,7 @@ public function validateResponseStatus(
*
* @return void
*
* @throws \Inspirum\Balikobot\Exceptions\BadRequestException
* @throws \Inspirum\Balikobot\Exception\BadRequestException
*/
public function validateResponseItemHasAttribute(array $items, string $attribute, array $response): void
{
Expand All @@ -79,15 +80,12 @@ public function validateResponseItemHasAttribute(array $items, string $attribute
* Validate response array has correct indexes
*
* @param array<mixed,mixed> $response
* @param int $count
*
* @return void
*
* @throws \Inspirum\Balikobot\Exceptions\BadRequestException
* @throws \Inspirum\Balikobot\Exception\BadRequestException
*/
public function validateIndexes(array $response, int $count): void
{
if (array_keys($response) !== range(0, $count - 1)) {
if (array_is_list($response) === false || count($response) !== $count) {
throw new BadRequestException($response);
}
}
Expand Down
43 changes: 0 additions & 43 deletions src/Contracts/RequesterInterface.php

This file was deleted.

58 changes: 0 additions & 58 deletions src/Definitions/API.php

This file was deleted.

Loading

0 comments on commit 811f731

Please sign in to comment.