Skip to content

Commit

Permalink
Follow 307 redirect, psalm, code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
gladyshev committed Jan 16, 2022
1 parent 45618fa commit b8b3188
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 67 deletions.
4 changes: 0 additions & 4 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
<directory name="src/appendix" />
</ignoreFiles>
</projectFiles>

<issueHandlers>
Expand Down
20 changes: 14 additions & 6 deletions src/AbstractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
namespace Promopult\TikTokMarketingApi;

use GuzzleHttp\Psr7\Request;
use Promopult\TikTokMarketingApi\Exception\ErrorResponse;
use Promopult\TikTokMarketingApi\Exception\MalformedResponse;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

abstract class AbstractService implements ServiceInterface
{
use RequestSenderTrait;

protected CredentialsInterface $credentials;
protected ClientInterface $httpClient;

Expand Down Expand Up @@ -55,7 +61,7 @@ public function requestApi(
$body
);

$response = $this->httpClient->sendRequest($request);
$response = $this->sendRequest($request);

return $this->handleResponse($response, $request);
}
Expand All @@ -64,8 +70,8 @@ public function requestApi(
* @throws \JsonException
*/
protected function handleResponse(
\Psr\Http\Message\ResponseInterface $response,
\Psr\Http\Message\RequestInterface $request
ResponseInterface $response,
RequestInterface $request
): array {
/** @var array $decodedJson */
$decodedJson = \json_decode(
Expand All @@ -76,14 +82,14 @@ protected function handleResponse(
);

if (!isset($decodedJson['code'])) {
throw new \Promopult\TikTokMarketingApi\Exception\MalformedResponse(
throw new MalformedResponse(
$request,
$response
);
}

if ($decodedJson['code'] != 0) {
throw new \Promopult\TikTokMarketingApi\Exception\ErrorResponse(
throw new ErrorResponse(
(int) $decodedJson['code'],
(string) ($decodedJson['message'] ?? 'Unknown error.'),
$request,
Expand All @@ -98,7 +104,9 @@ protected function prepareGetParams(array $args): string
{
$formedArgs = [];

/** @var mixed $value */
/**
* @psalm-suppress MixedAssignment
*/
foreach ($args as $arg => $value) {
if (is_scalar($value)) {
$formedArgs[$arg] = $value;
Expand Down
44 changes: 30 additions & 14 deletions src/Appendix/RegionIDs.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

final class RegionIDs
{
public const TYPE_DMA = "DMA";
public const TYPE_CITIES = "cities";
public const TYPE_COUNTRIES = "countries";
public const TYPE_PROVINCES = "provinces";
public const TYPE_DMA = 'DMA';
public const TYPE_CITIES = 'cities';
public const TYPE_COUNTRIES = 'countries';
public const TYPE_PROVINCES = 'provinces';

public const JSON = [
'DMA' => [
Expand Down Expand Up @@ -1480,16 +1480,12 @@ final class RegionIDs
];

/**
* Кешированный списко регионов. Ключ Id
*
* @var array
* @var array<int,array>
*/
private static $region_cache = [];
private static array $region_cache = [];

/**
* Возвращает список всех регионов
*
* @return array
* @return array<int,array>
*/
public static function getRegionsList(): array
{
Expand Down Expand Up @@ -1521,7 +1517,11 @@ public static function getRegionsByType(string $type): array

$ids = array_column($items, 'id');
$items = array_combine($ids, $items);
return $items ?? [];

return $items === false
? []
: $items
;
}

/**
Expand All @@ -1536,8 +1536,13 @@ public static function getRegionsByLevel(int $level): array
if ($level < 1 || $level > 3) {
return [];
}

$list = self::getRegionsList();

return array_filter($list, function ($region) use ($level) {
/**
* @psalm-suppress MixedArrayAccess
*/
return ($region['level'] ?? null) == $level;
});
}
Expand All @@ -1553,6 +1558,9 @@ public static function getRegionsByParentId(int $parent_id): array
{
$list = self::getRegionsList();
return array_filter($list, function ($region) use ($parent_id) {
/**
* @psalm-suppress MixedArrayAccess
*/
return ($region['parent_id'] ?? null) == $parent_id;
});
}
Expand All @@ -1562,11 +1570,12 @@ public static function getRegionsByParentId(int $parent_id): array
*
* @param int $id Id региона
*
* @return array|null
* @return ?array
*/
public static function getRegionById(int $id): ?array
{
self::getRegionsList();

return self::$region_cache[$id] ?? null;
}

Expand Down Expand Up @@ -1605,13 +1614,20 @@ public static function getRegionTree(int $id): array
$region = self::getRegionById($id);
$type = self::getRegionType($id);

if ($region === null || $type === null) {
throw new \InvalidArgumentException('Invalid region ID.');
}

$tree = [$type => $region];

if (isset($region['parent_id'])) {
/**
* @psalm-suppress MixedArgument
*/
$tree = array_merge($tree, self::getRegionTree($region['parent_id']));
}

uasort($tree, function ($a, $b) {
uasort($tree, function (array $a, array $b) {
if ($a['level'] == $b['level']) {
return 0;
}
Expand Down
18 changes: 2 additions & 16 deletions src/Credentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,8 @@

final class Credentials implements CredentialsInterface
{
/**
* @var string
*/
private $accessToken;

/**
* @var string
*/
private $apiBaseUrl;

private string $accessToken;
private string $apiBaseUrl;

/**
* Credentials constructor.
Expand Down Expand Up @@ -47,17 +39,11 @@ public static function fromAccessTokenSandbox(string $accessToken): CredentialsI
);
}

/**
* @return string
*/
public function getAccessToken(): string
{
return $this->accessToken;
}

/**
* @return string
*/
public function getApiBaseUrl(): string
{
return $this->apiBaseUrl;
Expand Down
31 changes: 14 additions & 17 deletions src/Exception/ErrorResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,53 @@

namespace Promopult\TikTokMarketingApi\Exception;

use GuzzleHttp\Psr7\Message;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class ErrorResponse extends \RuntimeException
{
/**
* @var \Psr\Http\Message\RequestInterface
*/
private $request;

/**
* @var \Psr\Http\Message\ResponseInterface
*/
private $response;
private RequestInterface $request;
private ResponseInterface $response;

public function __construct(
int $code,
string $message,
\Psr\Http\Message\RequestInterface $request,
\Psr\Http\Message\ResponseInterface $response,
RequestInterface $request,
ResponseInterface $response,
\Throwable $previous = null
) {
parent::__construct($message, $code, $previous);
$this->request = $request;
$this->response = $response;
}

public function getRequest(): \Psr\Http\Message\RequestInterface
public function getRequest(): RequestInterface
{
return $this->request;
}

public function getResponse(): \Psr\Http\Message\ResponseInterface
public function getResponse(): ResponseInterface
{
return $this->response;
}

public function getResponseAsString(): string
{
return \GuzzleHttp\Psr7\Message::toString($this->response);
return Message::toString($this->response);
}

public function getRequestAsString(): string
{
return \GuzzleHttp\Psr7\Message::toString($this->request);
return Message::toString($this->request);
}

public function __toString(): string
{
return parent::__toString() . PHP_EOL
. 'Http log:' . PHP_EOL
. '>>>' . $this->getRequestAsString() . PHP_EOL
. '<<<' . $this->getResponseAsString()
. '>>>' . PHP_EOL . $this->getRequestAsString() . PHP_EOL
. '<<<' . PHP_EOL . $this->getResponseAsString()
;
}
}
9 changes: 5 additions & 4 deletions src/Exception/MalformedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Promopult\TikTokMarketingApi\Exception;

use GuzzleHttp\Psr7\Message;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

Expand Down Expand Up @@ -34,20 +35,20 @@ public function getResponse(): ResponseInterface

public function getResponseAsString(): string
{
return \GuzzleHttp\Psr7\Message::toString($this->response);
return Message::toString($this->response);
}

public function getRequestAsString(): string
{
return \GuzzleHttp\Psr7\Message::toString($this->request);
return Message::toString($this->request);
}

public function __toString(): string
{
return parent::__toString() . PHP_EOL
. 'Http log:' . PHP_EOL
. '>>>' . $this->getRequestAsString() . PHP_EOL
. '<<<' . $this->getResponseAsString()
. '>>>' . PHP_EOL . $this->getRequestAsString() . PHP_EOL
. '<<<' . PHP_EOL . $this->getResponseAsString()
;
}
}
Loading

0 comments on commit b8b3188

Please sign in to comment.