From a2182cde9060b81ea8332598e68ecbdb654d569e Mon Sep 17 00:00:00 2001 From: Lukas Reschke Date: Tue, 7 Apr 2015 13:48:33 +0200 Subject: Deduplicate code --- lib/private/ocsclient.php | 75 ++++++++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 44 deletions(-) (limited to 'lib/private/ocsclient.php') 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; } -- cgit v1.2.3