From 40d2ed9ffe2f3aefcc830cd0e83488c43827839b Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Fri, 21 Dec 2012 10:58:22 -0500 Subject: [PATCH] HTTP client injection for AuthorInfo module. --- module/VuFind/config/module.config.php | 3 +- .../src/VuFind/Recommend/AuthorInfo.php | 28 +++++++++++-------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php index cbef5774424..f8d206435d9 100644 --- a/module/VuFind/config/module.config.php +++ b/module/VuFind/config/module.config.php @@ -237,7 +237,8 @@ $config = array( 'factories' => array( 'authorinfo' => function ($sm) { return new \VuFind\Recommend\AuthorInfo( - $sm->getServiceLocator()->get('SearchManager') + $sm->getServiceLocator()->get('SearchManager'), + $sm->getServiceLocator()->get('VuFind\Http')->createClient() ); }, 'worldcatidentities' => function ($sm) { diff --git a/module/VuFind/src/VuFind/Recommend/AuthorInfo.php b/module/VuFind/src/VuFind/Recommend/AuthorInfo.php index b4271d3926e..34d2705732d 100644 --- a/module/VuFind/src/VuFind/Recommend/AuthorInfo.php +++ b/module/VuFind/src/VuFind/Recommend/AuthorInfo.php @@ -26,7 +26,7 @@ * @link http://vufind.org/wiki/building_a_recommendations_module Wiki */ namespace VuFind\Recommend; -use VuFind\Config\Reader as ConfigReader, VuFind\Http\Client as HttpClient, +use VuFind\Config\Reader as ConfigReader, VuFind\I18n\Translator\TranslatorAwareInterface; /** @@ -44,6 +44,13 @@ use VuFind\Config\Reader as ConfigReader, VuFind\Http\Client as HttpClient, */ class AuthorInfo implements RecommendInterface, TranslatorAwareInterface { + /** + * HTTP client + * + * @var \Zend\Http\Client + */ + protected $client; + /** * Translator (or null if unavailable) * @@ -83,10 +90,13 @@ class AuthorInfo implements RecommendInterface, TranslatorAwareInterface * Constructor * * @param \VuFind\Search\Manager $searchManager Search manager + * @param \Zend\Http\Client $client HTTP client */ - public function __construct(\VuFind\Search\Manager $searchManager) - { + public function __construct(\VuFind\Search\Manager $searchManager, + \Zend\Http\Client $client + ) { $this->searchManager = $searchManager; + $this->client = $client; } /** @@ -213,10 +223,7 @@ class AuthorInfo implements RecommendInterface, TranslatorAwareInterface '?action=query&prop=revisions&rvprop=content&format=php' . '&list=allpages&titles=' . urlencode($author); - $client = new HttpClient(); - $client->setUri($uri); - $response = $client->setMethod('GET')->send(); - + $response = $this->client->setUri($uri)->setMethod('GET')->send(); if ($response->isSuccess()) { return $this->parseWikipedia(unserialize($response->getBody())); } @@ -471,8 +478,7 @@ class AuthorInfo implements RecommendInterface, TranslatorAwareInterface { $param = urlencode("LC|$lccn"); $url = "http://viaf.org/viaf/sourceID/{$param}/justlinks.json"; - $client = new \VuFind\Http\Client(); - $result = $client->setUri($url)->setMethod('GET')->send(); + $result = $this->client->setUri($url)->setMethod('GET')->send(); if (!$result->isSuccess()) { return false; } @@ -541,10 +547,8 @@ class AuthorInfo implements RecommendInterface, TranslatorAwareInterface '?prop=imageinfo&action=query&iiprop=url&iiurlwidth=150&format=php' . '&titles=Image:' . urlencode($imageName); - $client = new HttpClient(); try { - $client->setUri($url); - $result = $client->setMethod('GET')->send(); + $result = $this->client->setUri($url)->setMethod('GET')->send(); } catch (\Exception $e) { return false; } -- GitLab