Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce readonly modifier on class properties #61

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ All notable changes to `cybercog/youtrack-rest-php` will be documented in this f
- ([#58]) Bumped minimum Guzzle version to v7
- ([#59]) Raised minimum PHP version to v8.1
- ([#60]) Enforced class properties strict types
- ([#61]) Enforced readonly modifier on class properties

## [7.0.0] - 2022-10-02

Expand Down Expand Up @@ -155,6 +156,7 @@ All notable changes to `cybercog/youtrack-rest-php` will be documented in this f
[3.0.0]: https://github.com/cybercog/youtrack-rest-php/compare/2.0.1...3.0.0
[2.0.1]: https://github.com/cybercog/youtrack-rest-php/compare/1.0.0...2.0.1

[#61]: https://github.com/cybercog/youtrack-rest-php/pull/61
[#60]: https://github.com/cybercog/youtrack-rest-php/pull/60
[#59]: https://github.com/cybercog/youtrack-rest-php/pull/59
[#58]: https://github.com/cybercog/youtrack-rest-php/pull/58
Expand Down
4 changes: 3 additions & 1 deletion contracts/Authenticator/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ interface Authenticator
*
* @throws \Cog\Contracts\YouTrack\Rest\Authenticator\Exceptions\AuthenticationException
*/
public function authenticate(ClientInterface $client): void;
public function authenticate(
ClientInterface $client,
): void;

/**
* Retrieve authentication token.
Expand Down
4 changes: 3 additions & 1 deletion contracts/Authorizer/Authorizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ interface Authorizer
/**
* Append authorization headers to REST client.
*/
public function appendHeadersTo(ClientInterface $client): void;
public function appendHeadersTo(
ClientInterface $client,
): void;
}
40 changes: 33 additions & 7 deletions contracts/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ interface Client
* @throws \Cog\Contracts\YouTrack\Rest\Authorizer\Exceptions\InvalidAuthorizationToken
* @throws \Cog\Contracts\YouTrack\Rest\Client\Exceptions\ClientException
*/
public function request(string $method, string $uri, array $params = [], array $options = []): ResponseInterface;
public function request(
string $method,
string $uri,
array $params = [],
array $options = [],
): ResponseInterface;

/**
* Create and send an GET HTTP request.
Expand All @@ -49,7 +54,11 @@ public function request(string $method, string $uri, array $params = [], array $
* @throws \Cog\Contracts\YouTrack\Rest\Authorizer\Exceptions\InvalidAuthorizationToken
* @throws \Cog\Contracts\YouTrack\Rest\Client\Exceptions\ClientException
*/
public function get(string $uri, array $params = [], array $options = []): ResponseInterface;
public function get(
string $uri,
array $params = [],
array $options = [],
): ResponseInterface;

/**
* Create and send an POST HTTP request.
Expand All @@ -63,7 +72,11 @@ public function get(string $uri, array $params = [], array $options = []): Respo
* @throws \Cog\Contracts\YouTrack\Rest\Authorizer\Exceptions\InvalidAuthorizationToken
* @throws \Cog\Contracts\YouTrack\Rest\Client\Exceptions\ClientException
*/
public function post(string $uri, array $params = [], array $options = []): ResponseInterface;
public function post(
string $uri,
array $params = [],
array $options = [],
): ResponseInterface;

/**
* Create and send an PUT HTTP request.
Expand All @@ -77,7 +90,11 @@ public function post(string $uri, array $params = [], array $options = []): Resp
* @throws \Cog\Contracts\YouTrack\Rest\Authorizer\Exceptions\InvalidAuthorizationToken
* @throws \Cog\Contracts\YouTrack\Rest\Client\Exceptions\ClientException
*/
public function put(string $uri, array $params = [], array $options = []): ResponseInterface;
public function put(
string $uri,
array $params = [],
array $options = [],
): ResponseInterface;

/**
* Create and send an DELETE HTTP request.
Expand All @@ -91,18 +108,27 @@ public function put(string $uri, array $params = [], array $options = []): Respo
* @throws \Cog\Contracts\YouTrack\Rest\Authorizer\Exceptions\InvalidAuthorizationToken
* @throws \Cog\Contracts\YouTrack\Rest\Client\Exceptions\ClientException
*/
public function delete(string $uri, array $params = [], array $options = []): ResponseInterface;
public function delete(
string $uri,
array $params = [],
array $options = [],
): ResponseInterface;

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

/**
* Write header values.
*
* @param array $headers
* @return void
*/
public function withHeaders(array $headers): void;
public function withHeaders(
array $headers,
): void;
}
6 changes: 5 additions & 1 deletion contracts/HttpClient/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,9 @@ interface HttpClient
*
* @throws \Cog\Contracts\YouTrack\Rest\HttpClient\Exceptions\HttpClientException
*/
public function request(string $method, string $uri, array $options = []): PsrResponseInterface;
public function request(
string $method,
string $uri,
array $options = [],
): PsrResponseInterface;
}
8 changes: 6 additions & 2 deletions contracts/Response/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public function statusCode(): int;
/**
* Retrieves a comma-separated string of the values for a single header.
*/
public function header(string $header): string;
public function header(
string $header,
): string;

/**
* Transform response cookie headers to string.
Expand All @@ -60,7 +62,9 @@ public function toArray(): array;
/**
* Assert the status code of the response.
*/
public function isStatusCode(int $code): bool;
public function isStatusCode(
int $code,
): bool;

/**
* Determine if response has successful status code.
Expand Down
9 changes: 5 additions & 4 deletions src/Authenticator/CookieAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class CookieAuthenticator implements
private string $cookie = '';

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

Expand All @@ -37,8 +37,9 @@ public function __construct(
*
* @throws \Cog\Contracts\YouTrack\Rest\Authenticator\Exceptions\AuthenticationException
*/
public function authenticate(ClientInterface $client): void
{
public function authenticate(
ClientInterface $client,
): void {
if ($this->cookie !== '' || $this->isAuthenticating) {
return;
}
Expand Down
7 changes: 4 additions & 3 deletions src/Authorizer/CookieAuthorizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CookieAuthorizer implements
AuthorizerInterface
{
public function __construct(
private AuthenticatorInterface $authenticator,
private readonly AuthenticatorInterface $authenticator,
) {
}

Expand All @@ -30,8 +30,9 @@ public function __construct(
*
* @throws \Cog\Contracts\YouTrack\Rest\Authenticator\Exceptions\AuthenticationException
*/
public function appendHeadersTo(ClientInterface $client): void
{
public function appendHeadersTo(
ClientInterface $client,
): void {
$this->authenticator->authenticate($client);

$client->withHeader('Cookie', $this->authenticator->token());
Expand Down
7 changes: 4 additions & 3 deletions src/Authorizer/TokenAuthorizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ class TokenAuthorizer implements
AuthorizerInterface
{
public function __construct(
private string $token,
private readonly string $token,
) {
}

/**
* Append authorization headers to REST client.
*/
public function appendHeadersTo(ClientInterface $client): void
{
public function appendHeadersTo(
ClientInterface $client,
): void {
$client->withHeader('Authorization', "Bearer {$this->token}");
}
}
64 changes: 43 additions & 21 deletions src/Client/YouTrackClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ class YouTrackClient implements
/**
* Request headers.
*
* @var array
* @var array<string, string>
*/
private array $headers = [];

public function __construct(
private HttpClientInterface $httpClient,
private AuthorizerInterface $authorizer,
private readonly HttpClientInterface $httpClient,
private readonly AuthorizerInterface $authorizer,
string | null $endpointPathPrefix = null,
) {
$this->endpointPathPrefix = $endpointPathPrefix !== null
Expand All @@ -66,8 +66,12 @@ public function __construct(
* @throws \Cog\Contracts\YouTrack\Rest\Authorizer\Exceptions\InvalidAuthorizationToken
* @throws \Cog\Contracts\YouTrack\Rest\Client\Exceptions\ClientException
*/
public function request(string $method, string $uri, array $params = [], array $options = []): ResponseInterface
{
public function request(
string $method,
string $uri,
array $params = [],
array $options = [],
): ResponseInterface {
try {
$response = $this->httpClient->request(
$method,
Expand Down Expand Up @@ -100,8 +104,11 @@ public function request(string $method, string $uri, array $params = [], array $
* @throws \Cog\Contracts\YouTrack\Rest\Authorizer\Exceptions\InvalidAuthorizationToken
* @throws \Cog\Contracts\YouTrack\Rest\Client\Exceptions\ClientException
*/
public function get(string $uri, array $params = [], array $options = []): ResponseInterface
{
public function get(
string $uri,
array $params = [],
array $options = [],
): ResponseInterface {
return $this->request('GET', $uri, $params, $options);
}

Expand All @@ -117,8 +124,11 @@ public function get(string $uri, array $params = [], array $options = []): Respo
* @throws \Cog\Contracts\YouTrack\Rest\Authorizer\Exceptions\InvalidAuthorizationToken
* @throws \Cog\Contracts\YouTrack\Rest\Client\Exceptions\ClientException
*/
public function post(string $uri, array $params = [], array $options = []): ResponseInterface
{
public function post(
string $uri,
array $params = [],
array $options = [],
): ResponseInterface {
return $this->request('POST', $uri, $params, $options);
}

Expand All @@ -134,8 +144,11 @@ public function post(string $uri, array $params = [], array $options = []): Resp
* @throws \Cog\Contracts\YouTrack\Rest\Authorizer\Exceptions\InvalidAuthorizationToken
* @throws \Cog\Contracts\YouTrack\Rest\Client\Exceptions\ClientException
*/
public function put(string $uri, array $params = [], array $options = []): ResponseInterface
{
public function put(
string $uri,
array $params = [],
array $options = [],
): ResponseInterface {
return $this->request('PUT', $uri, $params, $options);
}

Expand All @@ -151,16 +164,21 @@ public function put(string $uri, array $params = [], array $options = []): Respo
* @throws \Cog\Contracts\YouTrack\Rest\Authorizer\Exceptions\InvalidAuthorizationToken
* @throws \Cog\Contracts\YouTrack\Rest\Client\Exceptions\ClientException
*/
public function delete(string $uri, array $params = [], array $options = []): ResponseInterface
{
public function delete(
string $uri,
array $params = [],
array $options = [],
): ResponseInterface {
return $this->request('DELETE', $uri, $params, $options);
}

/**
* Write header value.
*/
public function withHeader(string $key, string $value): void
{
public function withHeader(
string $key,
string $value,
): void {
$this->headers[$key] = $value;
}

Expand All @@ -170,16 +188,18 @@ public function withHeader(string $key, string $value): void
* @param array $headers
* @return void
*/
public function withHeaders(array $headers): void
{
public function withHeaders(
array $headers,
): void {
$this->headers = array_merge_recursive($this->headers, $headers);
}

/**
* Build endpoint URI.
*/
protected function buildUri(string $uri): string
{
protected function buildUri(
string $uri,
): string {
return $this->endpointPathPrefix . '/' . ltrim($uri, '/');
}

Expand All @@ -190,8 +210,10 @@ protected function buildUri(string $uri): string
* @param array $options
* @return array
*/
protected function buildOptions(array $params = [], array $options = []): array
{
protected function buildOptions(
array $params = [],
array $options = [],
): array {
$defaultOptions = [
'form_params' => $params,
'headers' => $this->buildHeaders(),
Expand Down
19 changes: 12 additions & 7 deletions src/HttpClient/GuzzleHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class GuzzleHttpClient implements
HttpClientInterface
{
public function __construct(
private ClientInterface $httpClient,
private readonly ClientInterface $httpClient,
) {
}

Expand All @@ -40,8 +40,11 @@ public function __construct(
*
* @throws \Cog\Contracts\YouTrack\Rest\HttpClient\Exceptions\HttpClientException
*/
public function request(string $method, string $uri, array $options = []): ResponseInterface
{
public function request(
string $method,
string $uri,
array $options = [],
): ResponseInterface {
try {
return $this->httpClient->request($method, $uri, $this->buildOptions($options));
} catch (BadResponseException | RequestException $e) {
Expand All @@ -61,8 +64,9 @@ public function request(string $method, string $uri, array $options = []): Respo
* @param array $options
* @return array
*/
private function buildOptions(array $options): array
{
private function buildOptions(
array $options,
): array {
return $this->appendUserAgent($options);
}

Expand All @@ -72,8 +76,9 @@ private function buildOptions(array $options): array
* @param array $options
* @return array
*/
private function appendUserAgent(array $options): array
{
private function appendUserAgent(
array $options,
): array {
$defaultAgent = Utils::defaultUserAgent();
if (extension_loaded('curl') && function_exists('curl_version')) {
$curlVersion = \curl_version();
Expand Down
Loading
Loading