Newer
Older
<?php
/**
* Copyright (C) Leipzig University Library 2019.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* @author Gregor Gawol <gawol@ub.uni-leipzig.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU GPLv2
*/
namespace finc\Boss\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Client\ClientInterface;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\SerializerInterface;
* "Get Boss data" AJAX Handler
*
* This service will retrieve the data of webservice BSZ One Stop Search (BOSS)
* to display avaiabiblity of a certain record
* @author Gregor Gawol <gawol@ub.uni-leipzig.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
{
protected const BOSS_URL = 'https://fernleihe.boss.bsz-bw.de/Holding/Query';
protected $baseUrl;
/**
* @var ClientInterface
*/
protected $httpClient;
/**
* @var SerializerInterface
*/
protected $serializer;
/**
* Client constructor.
*
* @param ClientInterface $httpClient
public function __construct(
ClientInterface $httpClient,
SerializerInterface $serializer
) {
$this->baseUrl = new Uri(static::BOSS_URL);
}
/**
* Retrieve Worldcat search webservice with ISBN or ISSN
*
* @param $isxns
* @param $network
* @return array
* @throws ClientException
* @throws \Psr\Http\Client\ClientExceptionInterface
*/
public function getRequestISBN($isxns, $network)
{
$retval = [];
foreach ($isxns as $isxn) {
$uri = $this->baseUrl->withQuery("isxn[]=$isxn&network=$network");
$request = new Request('GET', $uri);
$response = $this->httpClient->sendRequest($request);
if ($response->getStatusCode() == "200") {
$data = $this->lookupData($response);
if (!empty($data->getHoldings())) {
$retval['data'] = $data->getHoldings();
$retval['param'] = $isxn;
return $retval;
}
}
}
return $retval;
}
/**
* Retrieve Worldcat search webservice with zdb id
*
* @param $zdb
* @param $network
* @return array|void
* @throws ClientException
* @throws \Psr\Http\Client\ClientExceptionInterface
*/
public function getRequestZDB($zdb, $network)
{
$retval = [];
$uri = $this->baseUrl->withQuery("zdb=$zdb&network=$network");
$request = new Request('GET', $uri);
$response = $this->httpClient->sendRequest($request);
if ($response->getStatusCode() == "200") {
$retval['data'] = $this->lookupData($response)->getHoldings();
$retval['param'] = $zdb;
return $retval;
}
return $retval;
}
/**
* Retrieve Worldcat search webservice with query params
* author, title, year
*
* @param $author
* @param $title
* @param $year
* @param $network
* @return array
* @throws ClientException
* @throws \Psr\Http\Client\ClientExceptionInterface
*/
public function getRequestQuery($author, $title, $year, $network)
{
$uri = $this->baseUrl->withQuery("author=$author&title=$title&year=$year&network=$network");
$request = new Request('GET', $uri);
$response = $this->httpClient->sendRequest($request);
if ($response->getStatusCode() == "200") {
$retval['data'] = $this->lookupData($response)->getHoldings();
$retval['param'] = '';
return $retval;
}
return [];
}
/**
* Transform retrieving Response data (JSON Object) to PHP classes
*
* @param $response
* @return Root
*/
private function lookupData($response): Root
{
$root = $this->serializer->deserialize(
(string)$response->getBody(), Root::class, 'json', [
ObjectNormalizer::class => true,
JsonEncoder::class => true,
]);
return $root;