diff --git a/.gitignore b/.gitignore index c772037..9b29957 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ .idea/ vendor/ build/ +runtime/ +cache/ composer.lock \ No newline at end of file diff --git a/composer.json b/composer.json index e34e893..2f0bb82 100644 --- a/composer.json +++ b/composer.json @@ -13,18 +13,18 @@ "minimum-stability": "dev", "prefer-stable": true, "require": { - "php": "^7.2", + "php": "^7.4|^8.0", "ext-mbstring": "*", "ext-json":"*", - "psr/http-client": "*", - "psr/http-message": "*", - "guzzlehttp/psr7": "~1.7.0" + "psr/http-client": "1.0.3", + "guzzlehttp/psr7": "2.7.0" }, "require-dev": { - "phpunit/phpunit": "~8.5.13", - "squizlabs/php_codesniffer": "~3.5.8", - "guzzlehttp/guzzle": "~7.2.0", - "psr/log": "~1.0" + "phpunit/phpunit": "8.5.41", + "squizlabs/php_codesniffer": "3.5.8", + "vimeo/psalm": "3.18.2", + "guzzlehttp/guzzle": "7.9.0", + "psr/log": "1.1" }, "autoload": { "psr-4": { diff --git a/examples/regions.php b/examples/regions.php new file mode 100644 index 0000000..fc8337d --- /dev/null +++ b/examples/regions.php @@ -0,0 +1,16 @@ +dictionaries->get(['GeoRegions']); + +print_r($resp); diff --git a/src/AbstractService.php b/src/AbstractService.php index 4427559..ddeae82 100644 --- a/src/AbstractService.php +++ b/src/AbstractService.php @@ -1,30 +1,24 @@ serviceName = $serviceName; $this->credentials = $credentials; @@ -33,7 +27,7 @@ public function __construct( public function call(array $params = []): array { - $request = new \GuzzleHttp\Psr7\Request( + $request = new Request( 'POST', $this->getUri(), $this->getHeaders(), @@ -50,22 +44,16 @@ protected function getServiceName(): string return $this->serviceName; } - protected function getCredentials(): \Gladyshev\Yandex\Direct\CredentialsInterface + protected function getCredentials(): CredentialsInterface { return $this->credentials; } - /** - * @return string - */ protected function getUri(): string { return $this->getCredentials()->getBaseUrl() . '/json/v5/' . mb_strtolower($this->getServiceName()); } - /** - * @return array - */ protected function getHeaders(): array { $headers = [ @@ -85,14 +73,10 @@ protected function getHeaders(): array return $headers; } - /** - * @param array $params - * @return string - */ protected function getBody(array $params): string { if (empty($params['params'])) { - $params = new \StdClass(); + $params = new StdClass(); } else { $params['params'] = array_filter($params['params']); } @@ -100,20 +84,15 @@ protected function getBody(array $params): string return json_encode($params); } - /** - * @param \Psr\Http\Message\RequestInterface $request - * @param \Psr\Http\Message\ResponseInterface $response - * @return array - */ protected function handleResponse( - \Psr\Http\Message\RequestInterface $request, - \Psr\Http\Message\ResponseInterface $response + RequestInterface $request, + ResponseInterface $response ): array { $contents = $response->getBody()->getContents(); $parsedBody = json_decode($contents, true); if (!is_array($parsedBody)) { - throw new \Gladyshev\Yandex\Direct\Exception\ErrorResponseException( + throw new ErrorResponseException( 'Unexpected API response.', $contents, 0, @@ -123,7 +102,7 @@ protected function handleResponse( } if (!empty($parsedBody['error'])) { - throw new \Gladyshev\Yandex\Direct\Exception\ErrorResponseException( + throw new ErrorResponseException( $parsedBody['error']['error_string'], $parsedBody['error']['error_detail'], (int) $parsedBody['error']['error_code'], diff --git a/src/Client.php b/src/Client.php index 4a33b50..fffd83e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -1,68 +1,81 @@ credentials = $credentials; $this->httpClient = $httpClient; } - public function createService(string $serviceName): \Gladyshev\Yandex\Direct\ServiceInterface + public function createService(string $serviceName): ServiceInterface { if (!isset($this->services[$serviceName])) { $className = self::SERVICE_NAMESPACE . ucfirst($serviceName); if (!class_exists($className)) { - throw new \Gladyshev\Yandex\Direct\Exception\ServiceNotFoundException( + throw new ServiceNotFoundException( $serviceName, "Class '{$className}' is not found." ); @@ -70,8 +83,8 @@ public function createService(string $serviceName): \Gladyshev\Yandex\Direct\Ser $classInstance = new $className($serviceName, $this->credentials, $this->httpClient); - if (!$classInstance instanceof \Gladyshev\Yandex\Direct\ServiceInterface) { - throw new \Gladyshev\Yandex\Direct\Exception\ServiceNotFoundException( + if (!$classInstance instanceof ServiceInterface) { + throw new ServiceNotFoundException( $serviceName, "Class '{$className}' must be an instance of '\Gladyshev\Yandex\Direct\ServiceInterface'." ); @@ -83,7 +96,7 @@ public function createService(string $serviceName): \Gladyshev\Yandex\Direct\Ser return $this->services[$serviceName]; } - public function __get(string $serviceName): \Gladyshev\Yandex\Direct\ServiceInterface + public function __get(string $serviceName): ServiceInterface { return $this->createService($serviceName); } diff --git a/src/Credentials.php b/src/Credentials.php index ba98ed1..154b2cb 100644 --- a/src/Credentials.php +++ b/src/Credentials.php @@ -6,17 +6,15 @@ final class Credentials implements \Gladyshev\Yandex\Direct\CredentialsInterface { - private $token; - private $masterToken; - private $clientLogin; - private $useOperatorUnits; - private $isAgency; - private $language; - private $baseUrl; + private string $token; + private ?string $masterToken; + private ?string $clientLogin; + private ?bool $useOperatorUnits; + private bool $isAgency; + private string $language; + private string $baseUrl; /** - * Credentials constructor. - * * @param string $token # OAuth2 токен доступа * @param string|null $masterToken # Токен для финансовых операций (не поддерживается API V5) * @param string|null $clientLogin # Логин клиента Агентства (если isAgency = true, иначе NULL) @@ -111,57 +109,36 @@ public static function clientSandbox( ); } - /** - * @return string - */ public function getToken(): string { return $this->token; } - /** - * @return string|null - */ public function getMasterToken(): ?string { return $this->masterToken; } - /** - * @return string|null - */ public function getClientLogin(): ?string { return $this->clientLogin; } - /** - * @return bool|null - */ public function getUseOperatorUnits(): ?bool { return $this->useOperatorUnits; } - /** - * @return string - */ public function getLanguage(): string { return $this->language; } - /** - * @return string - */ public function getBaseUrl(): string { return $this->baseUrl; } - /** - * @return bool - */ public function isAgency(): bool { return $this->isAgency; diff --git a/src/CredentialsInterface.php b/src/CredentialsInterface.php index 3f609ea..fb77133 100644 --- a/src/CredentialsInterface.php +++ b/src/CredentialsInterface.php @@ -1,12 +1,7 @@ - */ interface ServiceFactoryInterface { /** * Create a Service instance by name - * - * @param $serviceName - * @return AbstractService */ public function createService(string $serviceName): ServiceInterface; } diff --git a/src/ServiceInterface.php b/src/ServiceInterface.php index 43acc2f..dd7396d 100644 --- a/src/ServiceInterface.php +++ b/src/ServiceInterface.php @@ -1,7 +1,5 @@ getParameters(); + $refParams = (new ReflectionMethod($class, $method))->getParameters(); $paramNames = [];