@@ -56,7 +56,7 @@ |
||
| 56 | 56 | * @param array $body |
| 57 | 57 | * @param array $query |
| 58 | 58 | * @param array $headers |
| 59 | - * @return resource|string |
|
| 59 | + * @return string |
|
| 60 | 60 | */ |
| 61 | 61 | protected function request($method, $url, array $body = [], array $query = [], array $headers = []) { |
| 62 | 62 | $fullUrl = trim($this->instance->getFullUrl(), '/') . '/' . $url; |
@@ -26,71 +26,71 @@ |
||
| 26 | 26 | use OCP\Http\Client\IClientService; |
| 27 | 27 | |
| 28 | 28 | class ApiBase { |
| 29 | - /** @var Instance */ |
|
| 30 | - private $instance; |
|
| 31 | - /** @var Credentials */ |
|
| 32 | - private $credentials; |
|
| 33 | - /** @var IClientService */ |
|
| 34 | - private $clientService; |
|
| 29 | + /** @var Instance */ |
|
| 30 | + private $instance; |
|
| 31 | + /** @var Credentials */ |
|
| 32 | + private $credentials; |
|
| 33 | + /** @var IClientService */ |
|
| 34 | + private $clientService; |
|
| 35 | 35 | |
| 36 | - public function __construct(Instance $instance, Credentials $credentials, IClientService $clientService) { |
|
| 37 | - $this->instance = $instance; |
|
| 38 | - $this->credentials = $credentials; |
|
| 39 | - $this->clientService = $clientService; |
|
| 40 | - } |
|
| 36 | + public function __construct(Instance $instance, Credentials $credentials, IClientService $clientService) { |
|
| 37 | + $this->instance = $instance; |
|
| 38 | + $this->credentials = $credentials; |
|
| 39 | + $this->clientService = $clientService; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - protected function getHttpClient() { |
|
| 43 | - return $this->clientService->newClient(); |
|
| 44 | - } |
|
| 42 | + protected function getHttpClient() { |
|
| 43 | + return $this->clientService->newClient(); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - protected function addDefaultHeaders(array $headers) { |
|
| 47 | - return array_merge([ |
|
| 48 | - 'OCS-APIREQUEST' => 'true', |
|
| 49 | - 'Accept' => 'application/json' |
|
| 50 | - ], $headers); |
|
| 51 | - } |
|
| 46 | + protected function addDefaultHeaders(array $headers) { |
|
| 47 | + return array_merge([ |
|
| 48 | + 'OCS-APIREQUEST' => 'true', |
|
| 49 | + 'Accept' => 'application/json' |
|
| 50 | + ], $headers); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * @param string $method |
|
| 55 | - * @param string $url |
|
| 56 | - * @param array $body |
|
| 57 | - * @param array $query |
|
| 58 | - * @param array $headers |
|
| 59 | - * @return resource|string |
|
| 60 | - */ |
|
| 61 | - protected function request($method, $url, array $body = [], array $query = [], array $headers = []) { |
|
| 62 | - $fullUrl = trim($this->instance->getFullUrl(), '/') . '/' . $url; |
|
| 63 | - $options = [ |
|
| 64 | - 'query' => $query, |
|
| 65 | - 'headers' => $this->addDefaultHeaders($headers), |
|
| 66 | - 'auth' => [$this->credentials->getUsername(), $this->credentials->getPassword()] |
|
| 67 | - ]; |
|
| 68 | - if ($body) { |
|
| 69 | - $options['body'] = $body; |
|
| 70 | - } |
|
| 53 | + /** |
|
| 54 | + * @param string $method |
|
| 55 | + * @param string $url |
|
| 56 | + * @param array $body |
|
| 57 | + * @param array $query |
|
| 58 | + * @param array $headers |
|
| 59 | + * @return resource|string |
|
| 60 | + */ |
|
| 61 | + protected function request($method, $url, array $body = [], array $query = [], array $headers = []) { |
|
| 62 | + $fullUrl = trim($this->instance->getFullUrl(), '/') . '/' . $url; |
|
| 63 | + $options = [ |
|
| 64 | + 'query' => $query, |
|
| 65 | + 'headers' => $this->addDefaultHeaders($headers), |
|
| 66 | + 'auth' => [$this->credentials->getUsername(), $this->credentials->getPassword()] |
|
| 67 | + ]; |
|
| 68 | + if ($body) { |
|
| 69 | + $options['body'] = $body; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - $client = $this->getHttpClient(); |
|
| 72 | + $client = $this->getHttpClient(); |
|
| 73 | 73 | |
| 74 | - switch ($method) { |
|
| 75 | - case 'get': |
|
| 76 | - $response = $client->get($fullUrl, $options); |
|
| 77 | - break; |
|
| 78 | - case 'post': |
|
| 79 | - $response = $client->post($fullUrl, $options); |
|
| 80 | - break; |
|
| 81 | - case 'put': |
|
| 82 | - $response = $client->put($fullUrl, $options); |
|
| 83 | - break; |
|
| 84 | - case 'delete': |
|
| 85 | - $response = $client->delete($fullUrl, $options); |
|
| 86 | - break; |
|
| 87 | - case 'options': |
|
| 88 | - $response = $client->options($fullUrl, $options); |
|
| 89 | - break; |
|
| 90 | - default: |
|
| 91 | - throw new \InvalidArgumentException('Invalid method ' . $method); |
|
| 92 | - } |
|
| 74 | + switch ($method) { |
|
| 75 | + case 'get': |
|
| 76 | + $response = $client->get($fullUrl, $options); |
|
| 77 | + break; |
|
| 78 | + case 'post': |
|
| 79 | + $response = $client->post($fullUrl, $options); |
|
| 80 | + break; |
|
| 81 | + case 'put': |
|
| 82 | + $response = $client->put($fullUrl, $options); |
|
| 83 | + break; |
|
| 84 | + case 'delete': |
|
| 85 | + $response = $client->delete($fullUrl, $options); |
|
| 86 | + break; |
|
| 87 | + case 'options': |
|
| 88 | + $response = $client->options($fullUrl, $options); |
|
| 89 | + break; |
|
| 90 | + default: |
|
| 91 | + throw new \InvalidArgumentException('Invalid method ' . $method); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - return $response->getBody(); |
|
| 95 | - } |
|
| 94 | + return $response->getBody(); |
|
| 95 | + } |
|
| 96 | 96 | } |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | * @return resource|string |
| 60 | 60 | */ |
| 61 | 61 | protected function request($method, $url, array $body = [], array $query = [], array $headers = []) { |
| 62 | - $fullUrl = trim($this->instance->getFullUrl(), '/') . '/' . $url; |
|
| 62 | + $fullUrl = trim($this->instance->getFullUrl(), '/').'/'.$url; |
|
| 63 | 63 | $options = [ |
| 64 | 64 | 'query' => $query, |
| 65 | 65 | 'headers' => $this->addDefaultHeaders($headers), |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | $response = $client->options($fullUrl, $options); |
| 89 | 89 | break; |
| 90 | 90 | default: |
| 91 | - throw new \InvalidArgumentException('Invalid method ' . $method); |
|
| 91 | + throw new \InvalidArgumentException('Invalid method '.$method); |
|
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | return $response->getBody(); |
@@ -23,31 +23,31 @@ |
||
| 23 | 23 | |
| 24 | 24 | |
| 25 | 25 | class Credentials { |
| 26 | - /** @var string */ |
|
| 27 | - private $user; |
|
| 28 | - /** @var string */ |
|
| 29 | - private $password; |
|
| 26 | + /** @var string */ |
|
| 27 | + private $user; |
|
| 28 | + /** @var string */ |
|
| 29 | + private $password; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @param string $user |
|
| 33 | - * @param string $password |
|
| 34 | - */ |
|
| 35 | - public function __construct($user, $password) { |
|
| 36 | - $this->user = $user; |
|
| 37 | - $this->password = $password; |
|
| 38 | - } |
|
| 31 | + /** |
|
| 32 | + * @param string $user |
|
| 33 | + * @param string $password |
|
| 34 | + */ |
|
| 35 | + public function __construct($user, $password) { |
|
| 36 | + $this->user = $user; |
|
| 37 | + $this->password = $password; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @return string |
|
| 42 | - */ |
|
| 43 | - public function getUsername() { |
|
| 44 | - return $this->user; |
|
| 45 | - } |
|
| 40 | + /** |
|
| 41 | + * @return string |
|
| 42 | + */ |
|
| 43 | + public function getUsername() { |
|
| 44 | + return $this->user; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * @return string |
|
| 49 | - */ |
|
| 50 | - public function getPassword() { |
|
| 51 | - return $this->password; |
|
| 52 | - } |
|
| 47 | + /** |
|
| 48 | + * @return string |
|
| 49 | + */ |
|
| 50 | + public function getPassword() { |
|
| 51 | + return $this->password; |
|
| 52 | + } |
|
| 53 | 53 | } |
@@ -28,54 +28,54 @@ |
||
| 28 | 28 | use OCP\API; |
| 29 | 29 | |
| 30 | 30 | class OCS extends ApiBase { |
| 31 | - /** |
|
| 32 | - * @param string $method |
|
| 33 | - * @param string $url |
|
| 34 | - * @param array $body |
|
| 35 | - * @param array $query |
|
| 36 | - * @param array $headers |
|
| 37 | - * @return array |
|
| 38 | - * @throws ForbiddenException |
|
| 39 | - * @throws NotFoundException |
|
| 40 | - * @throws \Exception |
|
| 41 | - */ |
|
| 42 | - protected function request($method, $url, array $body = [], array $query = [], array $headers = []) { |
|
| 43 | - try { |
|
| 44 | - $response = json_decode(parent::request($method, '/ocs/v2.php/' . $url, $body, $query, $headers), true); |
|
| 45 | - } catch (ClientException $e) { |
|
| 46 | - if ($e->getResponse()->getStatusCode() === 404) { |
|
| 47 | - throw new NotFoundException(); |
|
| 48 | - } else if ($e->getResponse()->getStatusCode() === 403 || $e->getResponse()->getStatusCode() === 401) { |
|
| 49 | - throw new ForbiddenException(); |
|
| 50 | - } else { |
|
| 51 | - throw $e; |
|
| 52 | - } |
|
| 53 | - } |
|
| 54 | - if (!isset($response['ocs']) || !isset($response['ocs']['meta'])) { |
|
| 55 | - throw new \Exception('Invalid ocs response'); |
|
| 56 | - } |
|
| 57 | - if ($response['ocs']['meta']['statuscode'] === API::RESPOND_UNAUTHORISED) { |
|
| 58 | - throw new ForbiddenException(); |
|
| 59 | - } |
|
| 60 | - if ($response['ocs']['meta']['statuscode'] === API::RESPOND_NOT_FOUND) { |
|
| 61 | - throw new NotFoundException(); |
|
| 62 | - } |
|
| 63 | - if ($response['ocs']['meta']['status'] !== 'ok') { |
|
| 64 | - throw new \Exception('Unknown ocs error ' . $response['ocs']['meta']['message']); |
|
| 65 | - } |
|
| 31 | + /** |
|
| 32 | + * @param string $method |
|
| 33 | + * @param string $url |
|
| 34 | + * @param array $body |
|
| 35 | + * @param array $query |
|
| 36 | + * @param array $headers |
|
| 37 | + * @return array |
|
| 38 | + * @throws ForbiddenException |
|
| 39 | + * @throws NotFoundException |
|
| 40 | + * @throws \Exception |
|
| 41 | + */ |
|
| 42 | + protected function request($method, $url, array $body = [], array $query = [], array $headers = []) { |
|
| 43 | + try { |
|
| 44 | + $response = json_decode(parent::request($method, '/ocs/v2.php/' . $url, $body, $query, $headers), true); |
|
| 45 | + } catch (ClientException $e) { |
|
| 46 | + if ($e->getResponse()->getStatusCode() === 404) { |
|
| 47 | + throw new NotFoundException(); |
|
| 48 | + } else if ($e->getResponse()->getStatusCode() === 403 || $e->getResponse()->getStatusCode() === 401) { |
|
| 49 | + throw new ForbiddenException(); |
|
| 50 | + } else { |
|
| 51 | + throw $e; |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | + if (!isset($response['ocs']) || !isset($response['ocs']['meta'])) { |
|
| 55 | + throw new \Exception('Invalid ocs response'); |
|
| 56 | + } |
|
| 57 | + if ($response['ocs']['meta']['statuscode'] === API::RESPOND_UNAUTHORISED) { |
|
| 58 | + throw new ForbiddenException(); |
|
| 59 | + } |
|
| 60 | + if ($response['ocs']['meta']['statuscode'] === API::RESPOND_NOT_FOUND) { |
|
| 61 | + throw new NotFoundException(); |
|
| 62 | + } |
|
| 63 | + if ($response['ocs']['meta']['status'] !== 'ok') { |
|
| 64 | + throw new \Exception('Unknown ocs error ' . $response['ocs']['meta']['message']); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - return $response['ocs']['data']; |
|
| 68 | - } |
|
| 67 | + return $response['ocs']['data']; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - public function getUser($userId) { |
|
| 71 | - return new User($this->request('get', 'cloud/users/' . $userId)); |
|
| 72 | - } |
|
| 70 | + public function getUser($userId) { |
|
| 71 | + return new User($this->request('get', 'cloud/users/' . $userId)); |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * @return array The capabilities in the form of [$appId => [$capability => $value]] |
|
| 76 | - */ |
|
| 77 | - public function getCapabilities() { |
|
| 78 | - $result = $this->request('get', 'cloud/capabilities'); |
|
| 79 | - return $result['capabilities']; |
|
| 80 | - } |
|
| 74 | + /** |
|
| 75 | + * @return array The capabilities in the form of [$appId => [$capability => $value]] |
|
| 76 | + */ |
|
| 77 | + public function getCapabilities() { |
|
| 78 | + $result = $this->request('get', 'cloud/capabilities'); |
|
| 79 | + return $result['capabilities']; |
|
| 80 | + } |
|
| 81 | 81 | } |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | */ |
| 42 | 42 | protected function request($method, $url, array $body = [], array $query = [], array $headers = []) { |
| 43 | 43 | try { |
| 44 | - $response = json_decode(parent::request($method, '/ocs/v2.php/' . $url, $body, $query, $headers), true); |
|
| 44 | + $response = json_decode(parent::request($method, '/ocs/v2.php/'.$url, $body, $query, $headers), true); |
|
| 45 | 45 | } catch (ClientException $e) { |
| 46 | 46 | if ($e->getResponse()->getStatusCode() === 404) { |
| 47 | 47 | throw new NotFoundException(); |
@@ -61,14 +61,14 @@ discard block |
||
| 61 | 61 | throw new NotFoundException(); |
| 62 | 62 | } |
| 63 | 63 | if ($response['ocs']['meta']['status'] !== 'ok') { |
| 64 | - throw new \Exception('Unknown ocs error ' . $response['ocs']['meta']['message']); |
|
| 64 | + throw new \Exception('Unknown ocs error '.$response['ocs']['meta']['message']); |
|
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | return $response['ocs']['data']; |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | public function getUser($userId) { |
| 71 | - return new User($this->request('get', 'cloud/users/' . $userId)); |
|
| 71 | + return new User($this->request('get', 'cloud/users/'.$userId)); |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | /** |
@@ -23,102 +23,102 @@ |
||
| 23 | 23 | |
| 24 | 24 | |
| 25 | 25 | class User { |
| 26 | - /** @var array */ |
|
| 27 | - private $data; |
|
| 28 | - |
|
| 29 | - public function __construct(array $data) { |
|
| 30 | - $this->data = $data; |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @return string |
|
| 36 | - */ |
|
| 37 | - public function getUserId() { |
|
| 38 | - return $this->data['id']; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @return string |
|
| 43 | - */ |
|
| 44 | - public function getEmail() { |
|
| 45 | - return $this->data['email']; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @return string |
|
| 50 | - */ |
|
| 51 | - public function getDisplayName() { |
|
| 52 | - return $this->data['displayname']; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @return string |
|
| 57 | - */ |
|
| 58 | - public function getPhone() { |
|
| 59 | - return $this->data['phone']; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @return string |
|
| 64 | - */ |
|
| 65 | - public function getAddress() { |
|
| 66 | - return $this->data['address']; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @return string |
|
| 71 | - */ |
|
| 72 | - public function getWebsite() { |
|
| 73 | - return $this->data['website']; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @return string |
|
| 78 | - */ |
|
| 79 | - public function getTwitter() { |
|
| 80 | - return isset($this->data['twitter']) ? $this->data['twitter'] : ''; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * @return string[] |
|
| 85 | - */ |
|
| 86 | - public function getGroups() { |
|
| 87 | - return $this->data['groups']; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @return string |
|
| 92 | - */ |
|
| 93 | - public function getLanguage() { |
|
| 94 | - return $this->data['language']; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * @return int |
|
| 99 | - */ |
|
| 100 | - public function getUsedSpace() { |
|
| 101 | - return $this->data['quota']['used']; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * @return int |
|
| 106 | - */ |
|
| 107 | - public function getFreeSpace() { |
|
| 108 | - return $this->data['quota']['free']; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @return int |
|
| 113 | - */ |
|
| 114 | - public function getTotalSpace() { |
|
| 115 | - return $this->data['quota']['total']; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * @return int |
|
| 120 | - */ |
|
| 121 | - public function getQuota() { |
|
| 122 | - return $this->data['quota']['quota']; |
|
| 123 | - } |
|
| 26 | + /** @var array */ |
|
| 27 | + private $data; |
|
| 28 | + |
|
| 29 | + public function __construct(array $data) { |
|
| 30 | + $this->data = $data; |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @return string |
|
| 36 | + */ |
|
| 37 | + public function getUserId() { |
|
| 38 | + return $this->data['id']; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @return string |
|
| 43 | + */ |
|
| 44 | + public function getEmail() { |
|
| 45 | + return $this->data['email']; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @return string |
|
| 50 | + */ |
|
| 51 | + public function getDisplayName() { |
|
| 52 | + return $this->data['displayname']; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @return string |
|
| 57 | + */ |
|
| 58 | + public function getPhone() { |
|
| 59 | + return $this->data['phone']; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @return string |
|
| 64 | + */ |
|
| 65 | + public function getAddress() { |
|
| 66 | + return $this->data['address']; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @return string |
|
| 71 | + */ |
|
| 72 | + public function getWebsite() { |
|
| 73 | + return $this->data['website']; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @return string |
|
| 78 | + */ |
|
| 79 | + public function getTwitter() { |
|
| 80 | + return isset($this->data['twitter']) ? $this->data['twitter'] : ''; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * @return string[] |
|
| 85 | + */ |
|
| 86 | + public function getGroups() { |
|
| 87 | + return $this->data['groups']; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @return string |
|
| 92 | + */ |
|
| 93 | + public function getLanguage() { |
|
| 94 | + return $this->data['language']; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * @return int |
|
| 99 | + */ |
|
| 100 | + public function getUsedSpace() { |
|
| 101 | + return $this->data['quota']['used']; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * @return int |
|
| 106 | + */ |
|
| 107 | + public function getFreeSpace() { |
|
| 108 | + return $this->data['quota']['free']; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @return int |
|
| 113 | + */ |
|
| 114 | + public function getTotalSpace() { |
|
| 115 | + return $this->data['quota']['total']; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * @return int |
|
| 120 | + */ |
|
| 121 | + public function getQuota() { |
|
| 122 | + return $this->data['quota']['quota']; |
|
| 123 | + } |
|
| 124 | 124 | } |
@@ -29,102 +29,102 @@ |
||
| 29 | 29 | * Provides some basic info about a remote Nextcloud instance |
| 30 | 30 | */ |
| 31 | 31 | class Instance { |
| 32 | - /** @var string */ |
|
| 33 | - private $url; |
|
| 32 | + /** @var string */ |
|
| 33 | + private $url; |
|
| 34 | 34 | |
| 35 | - /** @var ICache */ |
|
| 36 | - private $cache; |
|
| 35 | + /** @var ICache */ |
|
| 36 | + private $cache; |
|
| 37 | 37 | |
| 38 | - /** @var IClientService */ |
|
| 39 | - private $clientService; |
|
| 38 | + /** @var IClientService */ |
|
| 39 | + private $clientService; |
|
| 40 | 40 | |
| 41 | - private $status; |
|
| 41 | + private $status; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @param string $url |
|
| 45 | - * @param ICache $cache |
|
| 46 | - * @param IClientService $clientService |
|
| 47 | - */ |
|
| 48 | - public function __construct($url, ICache $cache, IClientService $clientService) { |
|
| 49 | - $url = str_replace('https://', '', $url); |
|
| 50 | - $this->url = str_replace('http://', '', $url); |
|
| 51 | - $this->cache = $cache; |
|
| 52 | - $this->clientService = $clientService; |
|
| 53 | - } |
|
| 43 | + /** |
|
| 44 | + * @param string $url |
|
| 45 | + * @param ICache $cache |
|
| 46 | + * @param IClientService $clientService |
|
| 47 | + */ |
|
| 48 | + public function __construct($url, ICache $cache, IClientService $clientService) { |
|
| 49 | + $url = str_replace('https://', '', $url); |
|
| 50 | + $this->url = str_replace('http://', '', $url); |
|
| 51 | + $this->cache = $cache; |
|
| 52 | + $this->clientService = $clientService; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * @return string The url of the remote server without protocol |
|
| 57 | - */ |
|
| 58 | - public function getUrl() { |
|
| 59 | - return $this->url; |
|
| 60 | - } |
|
| 55 | + /** |
|
| 56 | + * @return string The url of the remote server without protocol |
|
| 57 | + */ |
|
| 58 | + public function getUrl() { |
|
| 59 | + return $this->url; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * @return string The of of the remote server with protocol |
|
| 64 | - */ |
|
| 65 | - public function getFullUrl() { |
|
| 66 | - return $this->getProtocol() . '://' . $this->getUrl(); |
|
| 67 | - } |
|
| 62 | + /** |
|
| 63 | + * @return string The of of the remote server with protocol |
|
| 64 | + */ |
|
| 65 | + public function getFullUrl() { |
|
| 66 | + return $this->getProtocol() . '://' . $this->getUrl(); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * @return string The full version string in '13.1.2.3' format |
|
| 71 | - */ |
|
| 72 | - public function getVersion() { |
|
| 73 | - $status = $this->getStatus(); |
|
| 74 | - return $status['version']; |
|
| 75 | - } |
|
| 69 | + /** |
|
| 70 | + * @return string The full version string in '13.1.2.3' format |
|
| 71 | + */ |
|
| 72 | + public function getVersion() { |
|
| 73 | + $status = $this->getStatus(); |
|
| 74 | + return $status['version']; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * @return string 'http' or 'https' |
|
| 79 | - */ |
|
| 80 | - public function getProtocol() { |
|
| 81 | - $status = $this->getStatus(); |
|
| 82 | - return $status['protocol']; |
|
| 83 | - } |
|
| 77 | + /** |
|
| 78 | + * @return string 'http' or 'https' |
|
| 79 | + */ |
|
| 80 | + public function getProtocol() { |
|
| 81 | + $status = $this->getStatus(); |
|
| 82 | + return $status['protocol']; |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * Check that the remote server is installed and not in maintenance mode |
|
| 87 | - * |
|
| 88 | - * @return bool |
|
| 89 | - */ |
|
| 90 | - public function isActive() { |
|
| 91 | - $status = $this->getStatus(); |
|
| 92 | - return $status['installed'] && !$status['maintenance']; |
|
| 93 | - } |
|
| 85 | + /** |
|
| 86 | + * Check that the remote server is installed and not in maintenance mode |
|
| 87 | + * |
|
| 88 | + * @return bool |
|
| 89 | + */ |
|
| 90 | + public function isActive() { |
|
| 91 | + $status = $this->getStatus(); |
|
| 92 | + return $status['installed'] && !$status['maintenance']; |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - private function getStatus() { |
|
| 96 | - if ($this->status) { |
|
| 97 | - return $this->status; |
|
| 98 | - } |
|
| 99 | - $key = 'remote/' . $this->url . '/status'; |
|
| 100 | - $status = $this->cache->get($key); |
|
| 101 | - if (!$status) { |
|
| 102 | - $response = $this->downloadStatus('https://' . $this->getUrl() . '/status.php'); |
|
| 103 | - $protocol = 'https'; |
|
| 104 | - if (!$response) { |
|
| 105 | - $response = $this->downloadStatus('http://' . $this->getUrl() . '/status.php'); |
|
| 106 | - $protocol = 'http'; |
|
| 107 | - } |
|
| 108 | - $status = json_decode($response, true); |
|
| 109 | - if ($status) { |
|
| 110 | - $status['protocol'] = $protocol; |
|
| 111 | - } |
|
| 112 | - if ($status) { |
|
| 113 | - $this->cache->set($key, $status, 5 * 60); |
|
| 114 | - $this->status = $status; |
|
| 115 | - } else { |
|
| 116 | - throw new NotFoundException('Remote server not found at address ' . $this->url); |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - return $status; |
|
| 120 | - } |
|
| 95 | + private function getStatus() { |
|
| 96 | + if ($this->status) { |
|
| 97 | + return $this->status; |
|
| 98 | + } |
|
| 99 | + $key = 'remote/' . $this->url . '/status'; |
|
| 100 | + $status = $this->cache->get($key); |
|
| 101 | + if (!$status) { |
|
| 102 | + $response = $this->downloadStatus('https://' . $this->getUrl() . '/status.php'); |
|
| 103 | + $protocol = 'https'; |
|
| 104 | + if (!$response) { |
|
| 105 | + $response = $this->downloadStatus('http://' . $this->getUrl() . '/status.php'); |
|
| 106 | + $protocol = 'http'; |
|
| 107 | + } |
|
| 108 | + $status = json_decode($response, true); |
|
| 109 | + if ($status) { |
|
| 110 | + $status['protocol'] = $protocol; |
|
| 111 | + } |
|
| 112 | + if ($status) { |
|
| 113 | + $this->cache->set($key, $status, 5 * 60); |
|
| 114 | + $this->status = $status; |
|
| 115 | + } else { |
|
| 116 | + throw new NotFoundException('Remote server not found at address ' . $this->url); |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + return $status; |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - private function downloadStatus($url) { |
|
| 123 | - try { |
|
| 124 | - $request = $this->clientService->newClient()->get($url); |
|
| 125 | - return $request->getBody(); |
|
| 126 | - } catch (\Exception $e) { |
|
| 127 | - return false; |
|
| 128 | - } |
|
| 129 | - } |
|
| 122 | + private function downloadStatus($url) { |
|
| 123 | + try { |
|
| 124 | + $request = $this->clientService->newClient()->get($url); |
|
| 125 | + return $request->getBody(); |
|
| 126 | + } catch (\Exception $e) { |
|
| 127 | + return false; |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | 130 | } |
@@ -63,7 +63,7 @@ discard block |
||
| 63 | 63 | * @return string The of of the remote server with protocol |
| 64 | 64 | */ |
| 65 | 65 | public function getFullUrl() { |
| 66 | - return $this->getProtocol() . '://' . $this->getUrl(); |
|
| 66 | + return $this->getProtocol().'://'.$this->getUrl(); |
|
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | /** |
@@ -96,13 +96,13 @@ discard block |
||
| 96 | 96 | if ($this->status) { |
| 97 | 97 | return $this->status; |
| 98 | 98 | } |
| 99 | - $key = 'remote/' . $this->url . '/status'; |
|
| 99 | + $key = 'remote/'.$this->url.'/status'; |
|
| 100 | 100 | $status = $this->cache->get($key); |
| 101 | 101 | if (!$status) { |
| 102 | - $response = $this->downloadStatus('https://' . $this->getUrl() . '/status.php'); |
|
| 102 | + $response = $this->downloadStatus('https://'.$this->getUrl().'/status.php'); |
|
| 103 | 103 | $protocol = 'https'; |
| 104 | 104 | if (!$response) { |
| 105 | - $response = $this->downloadStatus('http://' . $this->getUrl() . '/status.php'); |
|
| 105 | + $response = $this->downloadStatus('http://'.$this->getUrl().'/status.php'); |
|
| 106 | 106 | $protocol = 'http'; |
| 107 | 107 | } |
| 108 | 108 | $status = json_decode($response, true); |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | $this->cache->set($key, $status, 5 * 60); |
| 114 | 114 | $this->status = $status; |
| 115 | 115 | } else { |
| 116 | - throw new NotFoundException('Remote server not found at address ' . $this->url); |
|
| 116 | + throw new NotFoundException('Remote server not found at address '.$this->url); |
|
| 117 | 117 | } |
| 118 | 118 | } |
| 119 | 119 | return $status; |