From: Lukas Reschke Date: Tue, 7 Apr 2015 11:48:33 +0000 (+0200) Subject: Deduplicate code X-Git-Tag: v8.1.0alpha1~59^2~4 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a2182cde9060b81ea8332598e68ecbdb654d569e;p=nextcloud-server.git Deduplicate code --- diff --git a/lib/private/ocsclient.php b/lib/private/ocsclient.php index df988d7d3a9..84469cb5e0d 100644 --- a/lib/private/ocsclient.php +++ b/lib/private/ocsclient.php @@ -80,6 +80,29 @@ class OCSClient { return $this->config->getSystemValue('appstoreurl', 'https://api.owncloud.com/v1'); } + /** + * @param string $body + * @param string $action + * @return null|\SimpleXMLElement + */ + private function loadData($body, $action) { + $loadEntities = libxml_disable_entity_loader(true); + $data = @simplexml_load_string($body); + libxml_disable_entity_loader($loadEntities); + + if($data === false) { + $this->logger->error( + sprintf('Could not get %s, content was no valid XML', $action), + [ + 'app' => 'core', + ] + ); + return null; + } + + return $data; + } + /** * Get all the categories from the OCS server * @@ -110,17 +133,8 @@ class OCSClient { return null; } - $loadEntities = libxml_disable_entity_loader(true); - $data = @simplexml_load_string($response->getBody()); - libxml_disable_entity_loader($loadEntities); - - if($data === false) { - $this->logger->error( - 'Could not get categories, content was no valid XML', - [ - 'app' => 'core', - ] - ); + $data = $this->loadData($response->getBody(), 'categories'); + if($data === null) { return null; } @@ -175,17 +189,8 @@ class OCSClient { return []; } - $loadEntities = libxml_disable_entity_loader(true); - $data = @simplexml_load_string($response->getBody()); - libxml_disable_entity_loader($loadEntities); - - if($data === false) { - $this->logger->error( - 'Could not get applications, content was no valid XML', - [ - 'app' => 'core', - ] - ); + $data = $this->loadData($response->getBody(), 'applications'); + if($data === null) { return []; } @@ -251,17 +256,8 @@ class OCSClient { return null; } - $loadEntities = libxml_disable_entity_loader(true); - $data = @simplexml_load_string($response->getBody()); - libxml_disable_entity_loader($loadEntities); - - if($data === false) { - $this->logger->error( - 'Could not get application, content was no valid XML', - [ - 'app' => 'core', - ] - ); + $data = $this->loadData($response->getBody(), 'application'); + if($data === null) { return null; } @@ -316,17 +312,8 @@ class OCSClient { return null; } - $loadEntities = libxml_disable_entity_loader(true); - $data = @simplexml_load_string($response->getBody()); - libxml_disable_entity_loader($loadEntities); - - if($data === false) { - $this->logger->error( - 'Could not get application download URL, content was no valid XML', - [ - 'app' => 'core', - ] - ); + $data = $this->loadData($response->getBody(), 'application download URL'); + if($data === null) { return null; }