Skip to content
Snippets Groups Projects
BossClient.php 4.95 KiB
Newer Older
Gregor Gawol's avatar
Gregor Gawol committed
<?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;
Gregor Gawol's avatar
Gregor Gawol committed
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
Gregor Gawol's avatar
Gregor Gawol committed
 *
 * @package finc\Boss\Client
 * @author  Gregor Gawol <gawol@ub.uni-leipzig.de>
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
Gregor Gawol's avatar
Gregor Gawol committed
 */
class BossClient
Gregor Gawol's avatar
Gregor Gawol committed
{
    protected const BOSS_URL = 'https://fernleihe.boss.bsz-bw.de/Holding/Query';
    protected $baseUrl;

    /**
Gregor Gawol's avatar
Gregor Gawol committed
     * @var ClientInterface
     */
    protected $httpClient;

Gregor Gawol's avatar
Gregor Gawol committed
    /**
     * @var SerializerInterface
     */
    protected $serializer;

Gregor Gawol's avatar
Gregor Gawol committed
    /**
     * Client constructor.
     *
     * @param ClientInterface $httpClient
Gregor Gawol's avatar
Gregor Gawol committed
     * @param SerializerInterface $serializer
Gregor Gawol's avatar
Gregor Gawol committed
     */
Gregor Gawol's avatar
Gregor Gawol committed
    public function __construct(
        ClientInterface $httpClient,
        SerializerInterface $serializer
    ) {
Gregor Gawol's avatar
Gregor Gawol committed
        $this->httpClient = $httpClient;
Gregor Gawol's avatar
Gregor Gawol committed
        $this->serializer = $serializer;
Gregor Gawol's avatar
Gregor Gawol committed
        $this->baseUrl = new Uri(static::BOSS_URL);
    }

    /**
     * Retrieve Worldcat search webservice with ISBN or ISSN
     *
Gregor Gawol's avatar
Gregor Gawol committed
     * @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);
Gregor Gawol's avatar
Gregor Gawol committed
            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
     *
Gregor Gawol's avatar
Gregor Gawol committed
     * @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);
Gregor Gawol's avatar
Gregor Gawol committed
        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
     *
Gregor Gawol's avatar
Gregor Gawol committed
     * @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);
Gregor Gawol's avatar
Gregor Gawol committed
        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
     */
Gregor Gawol's avatar
Gregor Gawol committed
    private function lookupData($response): Root
    {
        /** @var Root $root */
Gregor Gawol's avatar
Gregor Gawol committed
        $root = $this->serializer->deserialize(
            (string)$response->getBody(), Root::class, 'json', [
            ObjectNormalizer::class => true,
            JsonEncoder::class => true,
        ]);
        return $root;