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

Refactor lib/private/Remote #39163

Closed
52 changes: 18 additions & 34 deletions lib/private/Remote/Api/ApiBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,24 @@
*/
namespace OC\Remote\Api;

use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\Remote\ICredentials;
use OCP\Remote\IInstance;

class ApiBase {
/** @var IInstance */
private $instance;
/** @var ICredentials */
private $credentials;
/** @var IClientService */
private $clientService;

public function __construct(IInstance $instance, ICredentials $credentials, IClientService $clientService) {
$this->instance = $instance;
$this->credentials = $credentials;
$this->clientService = $clientService;
public function __construct(
private IInstance $instance,
private ICredentials $credentials,
private IClientService $clientService,
) {
}

protected function getHttpClient() {
protected function getHttpClient(): IClient {
return $this->clientService->newClient();
}

protected function addDefaultHeaders(array $headers) {
protected function addDefaultHeaders(array $headers): array {
return array_merge([
'OCS-APIREQUEST' => 'true',
'Accept' => 'application/json'
Expand All @@ -58,9 +53,9 @@ protected function addDefaultHeaders(array $headers) {
* @param array $query
* @param array $headers
* @return resource|string
* @throws \InvalidArgumentException
* @throws \InvalidArgumentException|\Exception
*/
protected function request($method, $url, array $body = [], array $query = [], array $headers = []) {
protected function request(string $method, string $url, array $body = [], array $query = [], array $headers = []) {
$fullUrl = trim($this->instance->getFullUrl(), '/') . '/' . $url;
$options = [
'query' => $query,
Expand All @@ -73,25 +68,14 @@ protected function request($method, $url, array $body = [], array $query = [], a

$client = $this->getHttpClient();

switch ($method) {
case 'get':
$response = $client->get($fullUrl, $options);
break;
case 'post':
$response = $client->post($fullUrl, $options);
break;
case 'put':
$response = $client->put($fullUrl, $options);
break;
case 'delete':
$response = $client->delete($fullUrl, $options);
break;
case 'options':
$response = $client->options($fullUrl, $options);
break;
default:
throw new \InvalidArgumentException('Invalid method ' . $method);
}
$response = match ($method) {
'get' => $client->get($fullUrl, $options),
'post' => $client->post($fullUrl, $options),
'put' => $client->put($fullUrl, $options),
'delete' => $client->delete($fullUrl, $options),
'options' => $client->options($fullUrl, $options),
default => throw new \InvalidArgumentException('Invalid method ' . $method),
};

return $response->getBody();
}
Expand Down
20 changes: 7 additions & 13 deletions lib/private/Remote/Api/ApiCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,18 @@
use OCP\Remote\IInstance;

class ApiCollection implements IApiCollection {
/** @var IInstance */
private $instance;
/** @var ICredentials */
private $credentials;
/** @var IClientService */
private $clientService;

public function __construct(IInstance $instance, ICredentials $credentials, IClientService $clientService) {
$this->instance = $instance;
$this->credentials = $credentials;
$this->clientService = $clientService;
public function __construct(
private IInstance $instance,
private ICredentials $credentials,
private IClientService $clientService,
) {
}

public function getCapabilitiesApi() {
public function getCapabilitiesApi(): OCS {
return new OCS($this->instance, $this->credentials, $this->clientService);
}

public function getUserApi() {
public function getUserApi(): OCS {
return new OCS($this->instance, $this->credentials, $this->clientService);
}
}
10 changes: 4 additions & 6 deletions lib/private/Remote/Api/ApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@
use OCP\Remote\IInstance;

class ApiFactory implements IApiFactory {
/** @var IClientService */
private $clientService;

public function __construct(IClientService $clientService) {
$this->clientService = $clientService;
public function __construct(
private IClientService $clientService,
) {
}

public function getApiCollection(IInstance $instance, ICredentials $credentials) {
public function getApiCollection(IInstance $instance, ICredentials $credentials): ApiCollection {
return new ApiCollection($instance, $credentials, $this->clientService);
}
}
15 changes: 11 additions & 4 deletions lib/private/Remote/Api/OCS.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class OCS extends ApiBase implements ICapabilitiesApi, IUserApi {
* @throws NotFoundException
* @throws \Exception
*/
protected function request($method, $url, array $body = [], array $query = [], array $headers = []) {
protected function request(string $method, string $url, array $body = [], array $query = [], array $headers = []): array {
try {
$response = json_decode(parent::request($method, 'ocs/v2.php/' . $url, $body, $query, $headers), true);
} catch (ClientException $e) {
Expand Down Expand Up @@ -77,24 +77,31 @@ protected function request($method, $url, array $body = [], array $query = [], a
* @param string[] $keys
* @throws \Exception
*/
private function checkResponseArray(array $data, $type, array $keys) {
private function checkResponseArray(array $data, string $type, array $keys): void {
foreach ($keys as $key) {
if (!array_key_exists($key, $data)) {
throw new \Exception('Invalid ' . $type . ' response, expected field ' . $key . ' not found');
}
}
}

public function getUser($userId) {
/**
* @throws ForbiddenException
* @throws NotFoundException
* @throws \Exception
*/
public function getUser($userId): User {
$result = $this->request('get', 'cloud/users/' . $userId);
$this->checkResponseArray($result, 'user', User::EXPECTED_KEYS);
return new User($result);
}

/**
* @return array The capabilities in the form of [$appId => [$capability => $value]]
* @throws ForbiddenException
* @throws NotFoundException
*/
public function getCapabilities() {
public function getCapabilities(): array {
$result = $this->request('get', 'cloud/capabilities');
return $result['capabilities'];
}
Expand Down
26 changes: 6 additions & 20 deletions lib/private/Remote/Credentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,17 @@
use OCP\Remote\ICredentials;

class Credentials implements ICredentials {
/** @var string */
private $user;
/** @var string */
private $password;

/**
* @param string $user
* @param string $password
*/
public function __construct($user, $password) {
$this->user = $user;
$this->password = $password;
public function __construct(
private string $user,
private string $password,
) {
}

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

/**
* @return string
*/
public function getPassword() {
public function getPassword(): string {
return $this->password;
}
}
37 changes: 18 additions & 19 deletions lib/private/Remote/Instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,65 +32,64 @@
*/
class Instance implements IInstance {
/** @var string */
private $url;

/** @var ICache */
private $cache;

/** @var IClientService */
private $clientService;
private string $url;

/** @var array|null */
private $status;
private ?array $status;

/**
* @param string $url
* @param ICache $cache
* @param IClientService $clientService
*/
public function __construct($url, ICache $cache, IClientService $clientService) {
public function __construct(
string $url,
private ICache $cache,
private IClientService $clientService,
) {
$url = str_replace('https://', '', $url);
$this->url = str_replace('http://', '', $url);
$this->cache = $cache;
$this->clientService = $clientService;
}

/**
* @return string The url of the remote server without protocol
*/
public function getUrl() {
public function getUrl(): string {
return $this->url;
}

/**
* @return string The of of the remote server with protocol
*/
public function getFullUrl() {
public function getFullUrl(): string {
return $this->getProtocol() . '://' . $this->getUrl();
}

/**
* @return string The full version string in '13.1.2.3' format
* @return string|null The full version string in '13.1.2.3' format
Fixed Show fixed Hide fixed
* @throws NotFoundException
*/
public function getVersion() {
public function getVersion(): ?string {
$status = $this->getStatus();
shdehnavi marked this conversation as resolved.
Show resolved Hide resolved
return $status['version'];
}

/**
* @return string 'http' or 'https'
* @throws NotFoundException
*/
public function getProtocol() {
public function getProtocol(): string {
$status = $this->getStatus();
return $status['protocol'];

Check failure on line 83 in lib/private/Remote/Instance.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

NullableReturnStatement

lib/private/Remote/Instance.php:83:10: NullableReturnStatement: The declared return type 'string' for OC\Remote\Instance::getProtocol is not nullable, but the function returns 'mixed|null' (see https://psalm.dev/139)
}

/**
* Check that the remote server is installed and not in maintenance mode
*
* @return bool
* @throws NotFoundException
*/
public function isActive() {
public function isActive(): bool {
$status = $this->getStatus();
return $status['installed'] && !$status['maintenance'];
}
Expand All @@ -100,7 +99,7 @@
* @throws NotFoundException
* @throws \Exception
*/
private function getStatus() {
private function getStatus(): ?array {
if ($this->status) {
return $this->status;
}
Expand Down Expand Up @@ -137,7 +136,7 @@
* @param string $url
* @return bool|string
*/
private function downloadStatus($url) {
private function downloadStatus(string $url): bool|string {
try {
$request = $this->clientService->newClient()->get($url);
return $request->getBody();
Expand Down
14 changes: 5 additions & 9 deletions lib/private/Remote/InstanceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,13 @@
use OCP\Remote\IInstanceFactory;

class InstanceFactory implements IInstanceFactory {
/** @var ICache */
private $cache;
/** @var IClientService */
private $clientService;

public function __construct(ICache $cache, IClientService $clientService) {
$this->cache = $cache;
$this->clientService = $clientService;
public function __construct(
private ICache $cache,
private IClientService $clientService
) {
}

public function getInstance($url) {
public function getInstance($url): Instance {
return new Instance($url, $this->cache, $this->clientService);
}
}
Loading
Loading