Skip to content

Commit

Permalink
Add property strict types
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkomarev committed Feb 22, 2024
1 parent 5a69be2 commit 10a05b9
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 196 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"docs": "https://github.com/cybercog/youtrack-rest-php/wiki"
},
"require": {
"php": "^8.0",
"php": "^8.1",
"ext-json": "*",
"guzzlehttp/guzzle": "^6.2"
},
Expand Down
5 changes: 0 additions & 5 deletions contracts/Authenticator/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,12 @@ interface Authenticator
/**
* Authenticate API Client.
*
* @param \Cog\Contracts\YouTrack\Rest\Client\Client $client
* @return void
*
* @throws \Cog\Contracts\YouTrack\Rest\Authenticator\Exceptions\AuthenticationException
*/
public function authenticate(ClientInterface $client): void;

/**
* Retrieve authentication token.
*
* @return string
*/
public function token(): string;
}
3 changes: 0 additions & 3 deletions contracts/Authorizer/Authorizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ interface Authorizer
{
/**
* Append authorization headers to REST client.
*
* @param \Cog\Contracts\YouTrack\Rest\Client\Client $client
* @return void
*/
public function appendHeadersTo(ClientInterface $client): void;
}
4 changes: 0 additions & 4 deletions contracts/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ public function delete(string $uri, array $params = [], array $options = []): Re

/**
* Write header value.
*
* @param string $key
* @param string $value
* @return void
*/
public function withHeader(string $key, string $value): void;

Expand Down
24 changes: 0 additions & 24 deletions contracts/Response/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ interface Response
{
/**
* Returns original HTTP client response.
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function httpResponse(): PsrResponseInterface;

Expand All @@ -29,37 +27,26 @@ public function httpResponse(): PsrResponseInterface;
*
* The status code is a 3-digit integer result code of the server's attempt
* to understand and satisfy the request.
*
* @return int
*/
public function statusCode(): int;

/**
* Retrieves a comma-separated string of the values for a single header.
*
* @param string $header
* @return string
*/
public function header(string $header): string;

/**
* Transform response cookie headers to string.
*
* @return string
*/
public function cookie(): string;

/**
* Returns response location header.
*
* @return string
*/
public function location(): string;

/**
* Returns body of the response.
*
* @return string
*/
public function body(): string;

Expand All @@ -72,37 +59,26 @@ public function toArray(): array;

/**
* Assert the status code of the response.
*
* @param int $code
* @return bool
*/
public function isStatusCode(int $code): bool;

/**
* Determine if response has successful status code.
*
* @return bool
*/
public function isSuccess(): bool;

/**
* Determine if response has redirect status code.
*
* @return bool
*/
public function isRedirect(): bool;

/**
* Determine if response has client error status code.
*
* @return bool
*/
public function isClientError(): bool;

/**
* Determine if response has server error status code.
*
* @return bool
*/
public function isServerError(): bool;
}
40 changes: 7 additions & 33 deletions src/Authenticator/CookieAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,22 @@
class CookieAuthenticator implements
AuthenticatorInterface
{
/**
* @var string
*/
private $username = '';

/**
* @var string
*/
private $password = '';

/**
* @var string
*/
private $cookie = '';

/**
* Determine is trying to authenticate.
*
* @var bool
*/
private $isAuthenticating = false;
private bool $isAuthenticating = false;

/**
* CookieAuthenticator constructor.
*
* @param string $username
* @param string $password
*/
public function __construct(string $username, string $password)
{
$this->username = $username;
$this->password = $password;
private string $cookie = '';

public function __construct(
private string $username,
private string $password,
) {
}

/**
* Authenticate client and returns cookie on success login.
*
* @param \Cog\Contracts\YouTrack\Rest\Client\Client $client
* @return void
*
* @throws \Cog\Contracts\YouTrack\Rest\Authenticator\Exceptions\AuthenticationException
*/
public function authenticate(ClientInterface $client): void
Expand All @@ -81,8 +57,6 @@ public function authenticate(ClientInterface $client): void

/**
* Retrieve authentication token.
*
* @return string
*/
public function token(): string
{
Expand Down
19 changes: 3 additions & 16 deletions src/Authorizer/CookieAuthorizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,14 @@
class CookieAuthorizer implements
AuthorizerInterface
{
/**
* @var \Cog\Contracts\YouTrack\Rest\Authenticator\Authenticator
*/
private $authenticator;

/**
* CookieAuthorizer constructor.
*
* @param \Cog\Contracts\YouTrack\Rest\Authenticator\Authenticator $authenticator
*/
public function __construct(AuthenticatorInterface $authenticator)
{
$this->authenticator = $authenticator;
public function __construct(
private AuthenticatorInterface $authenticator,
) {
}

/**
* Append authorization headers to REST client.
*
* @param \Cog\Contracts\YouTrack\Rest\Client\Client $client
* @return void
*
* @throws \Cog\Contracts\YouTrack\Rest\Authenticator\Exceptions\AuthenticationException
*/
public function appendHeadersTo(ClientInterface $client): void
Expand Down
19 changes: 3 additions & 16 deletions src/Authorizer/TokenAuthorizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,13 @@
class TokenAuthorizer implements
AuthorizerInterface
{
/**
* @var string
*/
private $token;

/**
* TokenAuthorizer constructor.
*
* @param string $token
*/
public function __construct(string $token)
{
$this->token = $token;
public function __construct(
private string $token,
) {
}

/**
* Append authorization headers to REST client.
*
* @param \Cog\Contracts\YouTrack\Rest\Client\Client $client
* @return void
*/
public function appendHeadersTo(ClientInterface $client): void
{
Expand Down
56 changes: 17 additions & 39 deletions src/Client/YouTrackClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,48 +29,29 @@
class YouTrackClient implements
RestClientInterface
{
/**
* HTTP Client.
*
* @var \Cog\Contracts\YouTrack\Rest\HttpClient\HttpClient
*/
private $httpClient;

/**
* Authorization driver.
*
* @var \Cog\Contracts\YouTrack\Rest\Authorizer\Authorizer
*/
private $authorizer;

/**
* Endpoint path prefix.
*
* @todo test it
*
* @var string
*/
private $endpointPathPrefix;
private string $endpointPathPrefix;

/**
* Request headers.
*
* @var array
*/
private $headers = [];

/**
* YouTrackClient constructor.
*
* @param \Cog\Contracts\YouTrack\Rest\HttpClient\HttpClient $httpClient
* @param \Cog\Contracts\YouTrack\Rest\Authorizer\Authorizer $authorizer
* @param string $endpointPathPrefix
*/
public function __construct(HttpClientInterface $httpClient, AuthorizerInterface $authorizer, string $endpointPathPrefix = null)
{
$this->httpClient = $httpClient;
$this->authorizer = $authorizer;
$this->endpointPathPrefix = $endpointPathPrefix !== null ? $endpointPathPrefix : 'api';
private array $headers = [];

public function __construct(
private HttpClientInterface $httpClient,
private AuthorizerInterface $authorizer,
// @todo test it
string | null $endpointPathPrefix = null,
) {
$this->endpointPathPrefix = $endpointPathPrefix !== null
? $endpointPathPrefix
: 'api';
}

/**
Expand All @@ -89,7 +70,11 @@ public function __construct(HttpClientInterface $httpClient, AuthorizerInterface
public function request(string $method, string $uri, array $params = [], array $options = []): ResponseInterface
{
try {
$response = $this->httpClient->request($method, $this->buildUri($uri), $this->buildOptions($params, $options));
$response = $this->httpClient->request(
$method,
$this->buildUri($uri),
$this->buildOptions($params, $options),
);
} catch (HttpClientException $e) {
switch ($e->getCode()) {
case 401:
Expand Down Expand Up @@ -174,10 +159,6 @@ public function delete(string $uri, array $params = [], array $options = []): Re

/**
* Write header value.
*
* @param string $key
* @param string $value
* @return void
*/
public function withHeader(string $key, string $value): void
{
Expand All @@ -197,9 +178,6 @@ public function withHeaders(array $headers): void

/**
* Build endpoint URI.
*
* @param string $uri
* @return string
*/
protected function buildUri(string $uri): string
{
Expand Down
18 changes: 3 additions & 15 deletions src/HttpClient/GuzzleHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,9 @@
class GuzzleHttpClient implements
HttpClientInterface
{
/**
* GuzzleHttp client.
*
* @var \GuzzleHttp\ClientInterface
*/
protected $httpClient;

/**
* Create a new GuzzleHttpClient instance.
*
* @param \GuzzleHttp\ClientInterface $httpClient
*/
public function __construct(ClientInterface $httpClient)
{
$this->httpClient = $httpClient;
public function __construct(
private ClientInterface $httpClient,
) {
}

/**
Expand Down
Loading

0 comments on commit 10a05b9

Please sign in to comment.