From dd6353b4de43b75659eaf3a56d54517fde7a0f5b Mon Sep 17 00:00:00 2001
From: Gregor Gawol <gawol@ub.uni-leipzig.de>
Date: Wed, 27 Mar 2019 14:12:49 +0100
Subject: [PATCH] added client into module

---
 composer.json                  |  13 ++-
 src/client/Client.php          | 170 +++++++++++++++++++++++++++++++++
 src/client/ClientException.php |  26 +++++
 src/client/Holding.php         |  87 +++++++++++++++++
 src/client/Root.php            |  95 ++++++++++++++++++
 5 files changed, 387 insertions(+), 4 deletions(-)
 create mode 100644 src/client/Client.php
 create mode 100644 src/client/ClientException.php
 create mode 100644 src/client/Holding.php
 create mode 100644 src/client/Root.php

diff --git a/composer.json b/composer.json
index a069746..d81dfdf 100644
--- a/composer.json
+++ b/composer.json
@@ -1,6 +1,6 @@
 {
     "name": "finc/boss-module",
-    "description": "Module for BOSS Client",
+    "description": "Module for BOSS Webservice",
     "license": "GPL-2.0",
     "authors": [
         {
@@ -10,8 +10,13 @@
     ],
     "require": {
         "php": ">=7.1",
-        "finc/boss-client": "dev-master",
-        "finc/vufindhttp-psrcompat": "dev-master"
+        "finc/vufindhttp-psrcompat": "dev-master",
+        "finc/vufindresolver-libmapper": "dev-master",
+        "psr/http-client": "^1.0",
+        "guzzlehttp/psr7": "^1.4",
+        "ext-json": "*",
+        "symfony/serializer": "^3.4",
+        "symfony/property-info": "^3.4"
     },
     "autoload": {
         "psr-4": {
@@ -29,7 +34,7 @@
     "extra": {
         "vufind": {
             "themes": {
-                "res\theme": ".boss"
+                "res\theme": "boss"
             }
         }
     }
diff --git a/src/client/Client.php b/src/client/Client.php
new file mode 100644
index 0000000..64df291
--- /dev/null
+++ b/src/client/Client.php
@@ -0,0 +1,170 @@
+<?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 Doctrine\Common\Annotations\AnnotationReader;
+use GuzzleHttp\Psr7\Request;
+use GuzzleHttp\Psr7\Uri;
+use Psr\Http\Client\ClientInterface;
+use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
+use Symfony\Component\Cache\Adapter\ArrayAdapter;
+use Symfony\Component\Serializer\Encoder\JsonEncoder;
+use Symfony\Component\Serializer\Encoder\XmlEncoder;
+use Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory;
+use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
+use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
+use Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader;
+use Symfony\Component\Serializer\Serializer;
+use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
+use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
+use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
+
+/**
+ * Class Client
+ *
+ * @package finc\Boss\Client
+ */
+class Client
+{
+    protected const BOSS_URL = 'https://fernleihe.boss.bsz-bw.de/Holding/Query';
+    protected $baseUrl;
+
+    /**
+     * @var ClientInterface
+     */
+    protected $httpClient;
+
+    /**
+     * Client constructor.
+     *
+     * @param ClientInterface $httpClient
+     */
+    public function __construct(ClientInterface $httpClient)
+    {
+        $this->httpClient = $httpClient;
+        $this->baseUrl = new Uri(static::BOSS_URL);
+    }
+
+    /**
+     * @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;
+    }
+
+    /**
+     * @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;
+    }
+
+    /**
+     * @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 [];
+    }
+
+    private function lookupData($response): Root
+    {
+        try {
+            $jsonEncoder = new JsonEncoder();
+            // TODO: caching data
+            // symfony/serializer suggests installing doctrine/cache (For using the default cached annotation reader and metadata cache.)
+            $objectNormalizers = new ObjectNormalizer(
+//                null,//new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader(null))),
+//                new CamelCaseToSnakeCaseNameConverter(),
+//                null,
+//                new ReflectionExtractor()
+            );
+            $serializer = new Serializer(
+                [
+                    $objectNormalizers//,
+//                    $attributesDenormalizer = new AttributesDenormalizer(),
+//                    new ArrayDenormalizer(),
+                ],
+                [
+                    $jsonEncoder
+                ]
+            );
+//            $attributesDenormalizer->setDenormalizer($serializer);
+            return $serializer->deserialize(
+                (string)$response->getBody(),
+                Root::class,
+                'json'
+            );
+        } catch (HttpClientException $ex) {
+            throw new ClientException($ex->getMessage(), $ex->getCode(), $ex);
+        }
+    }
+}
diff --git a/src/client/ClientException.php b/src/client/ClientException.php
new file mode 100644
index 0000000..764f013
--- /dev/null
+++ b/src/client/ClientException.php
@@ -0,0 +1,26 @@
+<?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;
+
+class ClientException extends \Exception
+{
+}
diff --git a/src/client/Holding.php b/src/client/Holding.php
new file mode 100644
index 0000000..2467fbe
--- /dev/null
+++ b/src/client/Holding.php
@@ -0,0 +1,87 @@
+<?php
+/**
+ * Copyright (C) 2018 Leipzig University Library
+ *
+ * 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>
+ * @author   Sebastian Kehr <kehr@ub.uni-leipzig.de>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU GPLv2
+ */
+
+namespace finc\Boss\Client;
+
+class Holding
+{
+    /**
+     * @var string
+     */
+    protected $isil;
+    /**
+     * @var string
+     */
+    protected $callnumber;
+    /**
+     * @var string
+     */
+    protected $issue;
+
+    /**
+     * @return string
+     */
+    public function getIsil(): string
+    {
+        return $this->isil;
+    }
+
+    /**
+     * @param string $isil
+     */
+    public function setIsil(string $isil): void
+    {
+        $this->isil = $isil;
+    }
+
+    /**
+     * @return string
+     */
+    public function getCallnumber(): string
+    {
+        return $this->callnumber;
+    }
+
+    /**
+     * @param string $callnumber
+     */
+    public function setCallnumber(string $callnumber): void
+    {
+        $this->callnumber = $callnumber;
+    }
+
+    /**
+     * @return string
+     */
+    public function getIssue(): string
+    {
+        return $this->issue;
+    }
+
+    /**
+     * @param string $issue
+     */
+    public function setIssue(string $issue): void
+    {
+        $this->issue = $issue;
+    }
+}
diff --git a/src/client/Root.php b/src/client/Root.php
new file mode 100644
index 0000000..3fff17a
--- /dev/null
+++ b/src/client/Root.php
@@ -0,0 +1,95 @@
+<?php
+/**
+ * Copyright (C) 2018 Leipzig University Library
+ *
+ * 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>
+ * @author   Sebastian Kehr <kehr@ub.uni-leipzig.de>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU GPLv2
+ */
+
+namespace finc\Boss\Client;
+
+class Root
+{
+    /**
+     * @var Holding[]
+     */
+    protected $holdings = [];
+    /**
+     * @var int
+     */
+    protected $numppn;
+    /**
+     * @var int
+     */
+    protected $numfound;
+
+    /**
+     * @param Holding $holding
+     */
+    public function addHolding(Holding $holding): void
+    {
+        $this->holdings[] = $holding;
+    }
+
+    /**
+     * @return Holding[]
+     */
+    public function getHoldings(): array
+    {
+        return $this->holdings;
+    }
+
+    /**
+     * @param Holding[] $holdings
+     */
+    public function setHoldings(array $holdings): void
+    {
+        $this->holdings = $holdings;
+    }
+
+    /**
+     * @return int
+     */
+    public function getNumppn(): int
+    {
+        return $this->numppn;
+    }
+
+    /**
+     * @param int $numppn
+     */
+    public function setNumppn(int $numppn): void
+    {
+        $this->numppn = $numppn;
+    }
+
+    /**
+     * @return int
+     */
+    public function getNumfound(): int
+    {
+        return $this->numfound;
+    }
+
+    /**
+     * @param int $numfound
+     */
+    public function setNumfound(int $numfound): void
+    {
+        $this->numfound = $numfound;
+    }
+}
-- 
GitLab